Skip to content

Commit 5762d3c

Browse files
authored
Update Swift Lambda scenario with missing actions, etc. (#7499)
* Checkpoint commit * g
1 parent 35324ff commit 5762d3c

File tree

8 files changed

+186
-118
lines changed

8 files changed

+186
-118
lines changed

.doc_gen/metadata/lambda_metadata.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,15 @@ lambda_DeleteFunction:
284284
excerpts:
285285
- snippet_tags:
286286
- lambda.rust.scenario.delete_function
287+
Swift:
288+
versions:
289+
- sdk_version: 1
290+
github: swift/example_code/lambda/basics
291+
excerpts:
292+
- description:
293+
snippet_tags:
294+
- swift.lambda-basics.imports
295+
- swift.lambda-basics.DeleteFunction
287296
services:
288297
lambda: {DeleteFunction}
289298
lambda_Invoke:
@@ -699,6 +708,15 @@ lambda_UpdateFunctionConfiguration:
699708
excerpts:
700709
- snippet_tags:
701710
- lambda.rust.scenario.update_function_configuration
711+
Swift:
712+
versions:
713+
- sdk_version: 1
714+
github: swift/example_code/lambda/basics
715+
excerpts:
716+
- description:
717+
snippet_tags:
718+
- swift.lambda-basics.imports
719+
- swift.lambda-basics.UpdateFunctionConfiguration
702720
services:
703721
lambda: {UpdateFunctionConfiguration}
704722
lambda_ListFunctions:

swift/example_code/lambda/README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ Code examples that show you how to perform the essential operations within a ser
4040

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

43-
- [CreateFunction](basics/lambda-basics/Sources/entry.swift#L177)
44-
- [GetFunction](basics/lambda-basics/Sources/entry.swift#L142)
45-
- [Invoke](basics/lambda-basics/Sources/entry.swift#L313)
46-
- [ListFunctions](basics/lambda-basics/Sources/entry.swift#L283)
47-
- [UpdateFunctionCode](basics/lambda-basics/Sources/entry.swift#L236)
43+
- [CreateFunction](basics/lambda-basics/Sources/entry.swift#L176)
44+
- [DeleteFunction](basics/lambda-basics/Sources/entry.swift#L549)
45+
- [GetFunction](basics/lambda-basics/Sources/entry.swift#L141)
46+
- [Invoke](basics/lambda-basics/Sources/entry.swift#L366)
47+
- [ListFunctions](basics/lambda-basics/Sources/entry.swift#L336)
48+
- [UpdateFunctionCode](basics/lambda-basics/Sources/entry.swift#L237)
49+
- [UpdateFunctionConfiguration](basics/lambda-basics/Sources/entry.swift#L284)
4850

4951

5052
<!--custom.examples.start-->

swift/example_code/lambda/basics/calculator/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let package = Package(
1717
// Dependencies declare other packages that this package depends on.
1818
.package(
1919
url: "https://github.com/swift-server/swift-aws-lambda-runtime.git",
20-
from: "1.0.0-alpha"),
20+
branch: "main"),
2121
],
2222
targets: [
2323
// Targets are the basic building blocks of a package, defining a module or a test suite.

swift/example_code/lambda/basics/calculator/Sources/calculator.swift

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -51,63 +51,46 @@ struct Response: Encodable, Sendable {
5151

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

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

61-
// snippet-start:[lambda.swift.calculator.handler.init]
62-
/// Initialize the AWS Lambda runtime.
63-
///
64-
/// ^ The logger is a standard Swift logger. You can control the verbosity
65-
/// by setting the `LOG_LEVEL` environment variable.
66-
init(context: LambdaInitializationContext) async throws {
67-
// Display the `LOG_LEVEL` configuration for this process.
68-
context.logger.info(
69-
"Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info" )"
70-
)
71-
}
72-
// snippet-end:[lambda.swift.calculator.handler.init]
73-
74-
// snippet-start:[lambda.swift.calculator.handler.handle]
75-
/// The Lambda function's entry point. Called by the Lambda runtime.
76-
///
77-
/// - Parameters:
78-
/// - event: The `Request` describing the request made by the
79-
/// client.
80-
/// - context: A `LambdaContext` describing the context in
81-
/// which the lambda function is running.
82-
///
83-
/// - Returns: A `Response` object that will be encoded to JSON and sent
84-
/// to the client by the Lambda runtime.
85-
func handle(_ event: Request, context: LambdaContext) async throws -> Response {
86-
let action = event.action
87-
var answer: Int?
88-
var actionFunc: ((Int, Int) -> Int)?
89-
90-
// Get the closure to run to perform the calculation.
91-
92-
actionFunc = actions[action]
71+
// Get the closure to run to perform the calculation.
9372

94-
guard let actionFunc else {
95-
context.logger.error("Unrecognized operation '\(action)\'")
96-
return Response(answer: nil)
97-
}
73+
actionFunc = await actions[action]
9874

99-
// Perform the calculation and return the answer.
75+
guard let actionFunc else {
76+
context.logger.error("Unrecognized operation '\(action)\'")
77+
return Response(answer: nil)
78+
}
10079

101-
answer = actionFunc(event.x, event.y)
80+
// Perform the calculation and return the answer.
10281

103-
guard let answer else {
104-
context.logger.error("Error computing \(event.x) \(action) \(event.y)")
105-
}
106-
context.logger.info("\(event.x) \(action) \(event.y) = \(answer)")
82+
answer = actionFunc(event.x, event.y)
10783

108-
return Response(answer: answer)
84+
guard let answer else {
85+
context.logger.error("Error computing \(event.x) \(action) \(event.y)")
10986
}
110-
// snippet-end:[lambda.swift.calculator.handler.handle]
87+
context.logger.info("\(event.x) \(action) \(event.y) = \(answer)")
88+
89+
return Response(answer: answer)
11190
}
112-
// snippet-end:[lambda.swift.calculator.handler]
91+
// snippet-end:[lambda.swift.calculator.runtime]
92+
93+
// snippet-start:[lambda.swift.calculator.run]
94+
try await calculatorLambdaRuntime.run()
95+
// snippet-end:[lambda.swift.calculator.run]
11396
// snippet-end:[lambda.swift.calculator.complete]

swift/example_code/lambda/basics/increment/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ let package = Package(
1717
// Dependencies declare other packages that this package depends on.
1818
.package(
1919
url: "https://github.com/swift-server/swift-aws-lambda-runtime.git",
20-
from: "1.0.0-alpha"),
20+
branch: "main"),
2121
],
2222
targets: [
2323
// Targets are the basic building blocks of a package, defining a module or a test suite.

swift/example_code/lambda/basics/increment/Sources/increment.swift

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -30,52 +30,37 @@ struct Response: Encodable, Sendable {
3030

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

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

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

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

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

75-
let response = Response(answer: answer)
76-
return response
77-
}
78-
// snippet-end:[lambda.swift.increment.handler.handle]
79-
}
80-
// snippet-end:[lambda.swift.increment.handler]
81-
// snippet-end:[lambda.swift.increment.complete]
63+
// snippet-start:[lambda.swift.increment.run]
64+
try await incrementLambdaRuntime.run()
65+
// snippet-end:[lambda.swift.increment.run]
66+
// snippet-end:[lambda.swift.increment.complete]

swift/example_code/lambda/basics/lambda-basics/Sources/ExampleError.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ enum ExampleError: Error {
2626
case listFunctionsError
2727
/// Unable to update the AWS Lambda function.
2828
case updateFunctionError
29+
/// Unable to update the function configuration.
30+
case updateFunctionConfigurationError
31+
/// The environment response is missing after an
32+
/// UpdateEnvironmentConfiguration attempt.
33+
case environmentResponseMissingError
34+
/// The environment variables are missing from the EnvironmentResponse and
35+
/// no errors occurred.
36+
case environmentVariablesMissingError
37+
/// The log level is incorrect after attempting to set it.
38+
case logLevelIncorrectError
2939
/// Unable to load the AWS Lambda function's Zip file.
3040
case zipFileReadError
3141

@@ -53,6 +63,14 @@ enum ExampleError: Error {
5363
return "Unable to list the AWS Lambda functions."
5464
case .updateFunctionError:
5565
return "Unable to update the AWS lambda function."
66+
case .updateFunctionConfigurationError:
67+
return "Unable to update the AWS lambda function configuration."
68+
case .environmentResponseMissingError:
69+
return "The environment is missing from the response after updating the function configuration."
70+
case .environmentVariablesMissingError:
71+
return "While no error occurred, no environment variables were returned following function configuration."
72+
case .logLevelIncorrectError:
73+
return "The log level is incorrect after attempting to set it to DEBUG."
5674
case .zipFileReadError:
5775
return "Unable to read the AWS Lambda function."
5876
}

0 commit comments

Comments
 (0)