Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .doc_gen/metadata/lambda_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,15 @@ lambda_DeleteFunction:
excerpts:
- snippet_tags:
- lambda.rust.scenario.delete_function
Swift:
versions:
- sdk_version: 1
github: swift/example_code/lambda/basics
excerpts:
- description:
snippet_tags:
- swift.lambda-basics.imports
- swift.lambda-basics.DeleteFunction
services:
lambda: {DeleteFunction}
lambda_Invoke:
Expand Down Expand Up @@ -699,6 +708,15 @@ lambda_UpdateFunctionConfiguration:
excerpts:
- snippet_tags:
- lambda.rust.scenario.update_function_configuration
Swift:
versions:
- sdk_version: 1
github: swift/example_code/lambda/basics
excerpts:
- description:
snippet_tags:
- swift.lambda-basics.imports
- swift.lambda-basics.UpdateFunctionConfiguration
services:
lambda: {UpdateFunctionConfiguration}
lambda_ListFunctions:
Expand Down
12 changes: 7 additions & 5 deletions swift/example_code/lambda/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ Code examples that show you how to perform the essential operations within a ser

Code excerpts that show you how to call individual service functions.

- [CreateFunction](basics/lambda-basics/Sources/entry.swift#L177)
- [GetFunction](basics/lambda-basics/Sources/entry.swift#L142)
- [Invoke](basics/lambda-basics/Sources/entry.swift#L313)
- [ListFunctions](basics/lambda-basics/Sources/entry.swift#L283)
- [UpdateFunctionCode](basics/lambda-basics/Sources/entry.swift#L236)
- [CreateFunction](basics/lambda-basics/Sources/entry.swift#L176)
- [DeleteFunction](basics/lambda-basics/Sources/entry.swift#L549)
- [GetFunction](basics/lambda-basics/Sources/entry.swift#L141)
- [Invoke](basics/lambda-basics/Sources/entry.swift#L366)
- [ListFunctions](basics/lambda-basics/Sources/entry.swift#L336)
- [UpdateFunctionCode](basics/lambda-basics/Sources/entry.swift#L237)
- [UpdateFunctionConfiguration](basics/lambda-basics/Sources/entry.swift#L284)


<!--custom.examples.start-->
Expand Down
2 changes: 1 addition & 1 deletion swift/example_code/lambda/basics/calculator/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
// Dependencies declare other packages that this package depends on.
.package(
url: "https://github.com/swift-server/swift-aws-lambda-runtime.git",
from: "1.0.0-alpha"),
branch: "main"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,63 +51,46 @@ struct Response: Encodable, Sendable {

// snippet-end:[lambda.swift.calculator.types]

// snippet-start:[lambda.swift.calculator.handler]
/// A Swift AWS Lambda Runtime `LambdaHandler` lets you both perform needed
/// initialization and handle AWS Lambda requests. There are other handler
/// protocols available for other use cases.
@main
struct CalculatorLambda: LambdaHandler {
// snippet-start:[lambda.swift.calculator.runtime]
/// The Lambda function's entry point. Called by the Lambda runtime.
///
/// - Parameters:
/// - event: The `Request` describing the request made by the
/// client.
/// - context: A `LambdaContext` describing the context in
/// which the lambda function is running.
///
/// - Returns: A `Response` object that will be encoded to JSON and sent
/// to the client by the Lambda runtime.
let calculatorLambdaRuntime = LambdaRuntime {
(_ event: Request, context: LambdaContext) -> Response in
let action = event.action
var answer: Int?
var actionFunc: ((Int, Int) -> Int)?

// snippet-start:[lambda.swift.calculator.handler.init]
/// Initialize the AWS Lambda runtime.
///
/// ^ The logger is a standard Swift logger. You can control the verbosity
/// by setting the `LOG_LEVEL` environment variable.
init(context: LambdaInitializationContext) async throws {
// Display the `LOG_LEVEL` configuration for this process.
context.logger.info(
"Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info" )"
)
}
// snippet-end:[lambda.swift.calculator.handler.init]

// snippet-start:[lambda.swift.calculator.handler.handle]
/// The Lambda function's entry point. Called by the Lambda runtime.
///
/// - Parameters:
/// - event: The `Request` describing the request made by the
/// client.
/// - context: A `LambdaContext` describing the context in
/// which the lambda function is running.
///
/// - Returns: A `Response` object that will be encoded to JSON and sent
/// to the client by the Lambda runtime.
func handle(_ event: Request, context: LambdaContext) async throws -> Response {
let action = event.action
var answer: Int?
var actionFunc: ((Int, Int) -> Int)?

// Get the closure to run to perform the calculation.

actionFunc = actions[action]
// Get the closure to run to perform the calculation.

guard let actionFunc else {
context.logger.error("Unrecognized operation '\(action)\'")
return Response(answer: nil)
}
actionFunc = await actions[action]

// Perform the calculation and return the answer.
guard let actionFunc else {
context.logger.error("Unrecognized operation '\(action)\'")
return Response(answer: nil)
}

answer = actionFunc(event.x, event.y)
// Perform the calculation and return the answer.

guard let answer else {
context.logger.error("Error computing \(event.x) \(action) \(event.y)")
}
context.logger.info("\(event.x) \(action) \(event.y) = \(answer)")
answer = actionFunc(event.x, event.y)

return Response(answer: answer)
guard let answer else {
context.logger.error("Error computing \(event.x) \(action) \(event.y)")
}
// snippet-end:[lambda.swift.calculator.handler.handle]
context.logger.info("\(event.x) \(action) \(event.y) = \(answer)")

return Response(answer: answer)
}
// snippet-end:[lambda.swift.calculator.handler]
// snippet-end:[lambda.swift.calculator.runtime]

// snippet-start:[lambda.swift.calculator.run]
try await calculatorLambdaRuntime.run()
// snippet-end:[lambda.swift.calculator.run]
// snippet-end:[lambda.swift.calculator.complete]
2 changes: 1 addition & 1 deletion swift/example_code/lambda/basics/increment/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let package = Package(
// Dependencies declare other packages that this package depends on.
.package(
url: "https://github.com/swift-server/swift-aws-lambda-runtime.git",
from: "1.0.0-alpha"),
branch: "main"),
],
targets: [
// Targets are the basic building blocks of a package, defining a module or a test suite.
Expand Down
73 changes: 29 additions & 44 deletions swift/example_code/lambda/basics/increment/Sources/increment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,37 @@ struct Response: Encodable, Sendable {

// snippet-end:[lambda.swift.increment.types]

// snippet-start:[lambda.swift.increment.handler]
/// A Swift AWS Lambda Runtime `LambdaHandler` lets you both perform needed
/// initialization and handle AWS Lambda requests. There are other handler
/// protocols available for other use cases.
@main
struct IncrementLambda: LambdaHandler {
// snippet-start:[lambda.swift.increment.runtime]
/// The Lambda function body.
///
/// - Parameters:
/// - event: The `Request` describing the request made by the
/// client.
/// - context: A `LambdaContext` describing the context in
/// which the lambda function is running.
///
/// - Returns: A `Response` object that will be encoded to JSON and sent
/// to the client by the Lambda runtime.
let incrementLambdaRuntime = LambdaRuntime {
(event: Request, context: LambdaContext) -> Response in
let action = event.action
var answer: Int?

// snippet-start:[lambda.swift.increment.handler.init]
/// Initialize the AWS Lambda runtime.
///
/// ^ The logger is a standard Swift logger. You can control the verbosity
/// by setting the `LOG_LEVEL` environment variable.
init(context: LambdaInitializationContext) async throws {
// Display the `LOG_LEVEL` configuration for this process.
context.logger.info(
"Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info" )"
)
if action != "increment" {
context.logger.error("Unrecognized operation: \"\(action)\". The only supported action is \"increment\".")
} else {
answer = event.number + 1
context.logger.info("The calculated answer is \(answer!).")
}
// snippet-end:[lambda.swift.increment.handler.init]

// snippet-start:[lambda.swift.increment.handler.handle]
/// The Lambda function's entry point. Called by the Lambda runtime.
///
/// - Parameters:
/// - event: The `Request` describing the request made by the
/// client.
/// - context: A `LambdaContext` describing the context in
/// which the lambda function is running.
///
/// - Returns: A `Response` object that will be encoded to JSON and sent
/// to the client by the Lambda runtime.
func handle(_ event: Request, context: LambdaContext) async throws -> Response {
let action = event.action
var answer: Int?
let response = Response(answer: answer)
return response
}
// snippet-end:[lambda.swift.increment.runtime]

if action != "increment" {
context.logger.error("Unrecognized operation: \"\(action)\". The only supported action is \"increment\".")
} else {
answer = event.number + 1
context.logger.info("The calculated answer is \(answer!).")
}
// Run the Lambda runtime code.

let response = Response(answer: answer)
return response
}
// snippet-end:[lambda.swift.increment.handler.handle]
}
// snippet-end:[lambda.swift.increment.handler]
// snippet-end:[lambda.swift.increment.complete]
// snippet-start:[lambda.swift.increment.run]
try await incrementLambdaRuntime.run()
// snippet-end:[lambda.swift.increment.run]
// snippet-end:[lambda.swift.increment.complete]
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ enum ExampleError: Error {
case listFunctionsError
/// Unable to update the AWS Lambda function.
case updateFunctionError
/// Unable to update the function configuration.
case updateFunctionConfigurationError
/// The environment response is missing after an
/// UpdateEnvironmentConfiguration attempt.
case environmentResponseMissingError
/// The environment variables are missing from the EnvironmentResponse and
/// no errors occurred.
case environmentVariablesMissingError
/// The log level is incorrect after attempting to set it.
case logLevelIncorrectError
/// Unable to load the AWS Lambda function's Zip file.
case zipFileReadError

Expand Down Expand Up @@ -53,6 +63,14 @@ enum ExampleError: Error {
return "Unable to list the AWS Lambda functions."
case .updateFunctionError:
return "Unable to update the AWS lambda function."
case .updateFunctionConfigurationError:
return "Unable to update the AWS lambda function configuration."
case .environmentResponseMissingError:
return "The environment is missing from the response after updating the function configuration."
case .environmentVariablesMissingError:
return "While no error occurred, no environment variables were returned following function configuration."
case .logLevelIncorrectError:
return "The log level is incorrect after attempting to set it to DEBUG."
case .zipFileReadError:
return "Unable to read the AWS Lambda function."
}
Expand Down
Loading
Loading