Skip to content

Commit b85cd95

Browse files
committed
Update Swift Glue scenario with missing bits
This PR updates the Glue scenario to work on Swift 6, as well as to include an example for UpdateFunctionConfiguration. it also adds SoS tagging for DeleteFunction. Other minor improvements are made as well and comments updated.
1 parent e23dec7 commit b85cd95

File tree

4 files changed

+90
-108
lines changed

4 files changed

+90
-108
lines changed

swift/example_code/lambda/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ 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#L536)
45+
- [GetFunction](basics/lambda-basics/Sources/entry.swift#L141)
46+
- [Invoke](basics/lambda-basics/Sources/entry.swift#L353)
47+
- [ListFunctions](basics/lambda-basics/Sources/entry.swift#L323)
48+
- [UpdateFunctionCode](basics/lambda-basics/Sources/entry.swift#L237)
4849

4950

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

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 environment variable: \(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/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 environment variable: \(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/entry.swift

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@ struct ExampleCommand: ParsableCommand {
165165
///
166166
/// - Parameters:
167167
/// - lambdaClient: The `LambdaClient` to use.
168-
/// - name: The name of the AWS Lambda function to create.
168+
/// - functionName: The name of the AWS Lambda function to create.
169169
/// - roleArn: The ARN of the role to apply to the function.
170170
/// - path: The path of the Zip archive containing the function.
171171
///
172172
/// - Returns: `true` if the AWS Lambda was successfully created; `false`
173173
/// if it wasn't.
174-
func createFunction(lambdaClient: LambdaClient, name: String,
174+
func createFunction(lambdaClient: LambdaClient, functionName: String,
175175
roleArn: String?, path: String) async throws -> Bool {
176176
// snippet-start:[swift.lambda-basics.CreateFunction]
177177
do {
@@ -187,7 +187,7 @@ struct ExampleCommand: ParsableCommand {
187187
_ = try await lambdaClient.createFunction(
188188
input: CreateFunctionInput(
189189
code: LambdaClientTypes.FunctionCode(zipFile: zipData),
190-
functionName: name,
190+
functionName: functionName,
191191
handler: "handle",
192192
role: roleArn,
193193
runtime: .providedal2
@@ -208,7 +208,7 @@ struct ExampleCommand: ParsableCommand {
208208
minDelay: 0.5,
209209
maxDelay: 2
210210
),
211-
input: GetFunctionInput(functionName: name)
211+
input: GetFunctionInput(functionName: functionName)
212212
)
213213

214214
switch output.result {
@@ -226,7 +226,7 @@ struct ExampleCommand: ParsableCommand {
226226
///
227227
/// - Parameters:
228228
/// - lambdaClient: The `LambdaClient` to use.
229-
/// - name: The name of the AWS Lambda function to update.
229+
/// - functionName: The name of the AWS Lambda function to update.
230230
/// - path: The pathname of the Zip file containing the packaged Lambda
231231
/// function.
232232
/// - Throws: `ExampleError.zipFileReadError`
@@ -281,6 +281,20 @@ struct ExampleCommand: ParsableCommand {
281281
}
282282
// snippet-end:[swift.lambda-basics.UpdateFunctionCode.wait]
283283

284+
// snippet-start:[lambda.swift.UpdateFunctionConfiguration]
285+
/// Tell the server-side component to log debug output by setting its
286+
/// environment's `LOG_LEVEL` to `DEBUG`.
287+
///
288+
/// - Parameters:
289+
/// - lambdaClient: The `LambdaClient` to use.
290+
/// - functionName: The name of the AWS Lambda function to enable debug
291+
/// logging for.
292+
///
293+
/// - Throws: `ExampleError.environmentResponseMissingError`,
294+
/// `ExampleError.updateFunctionConfigurationError`,
295+
/// `ExampleError.environmentVariablesMissingError`,
296+
/// `ExampleError.logLevelIncorrectError`,
297+
/// `ExampleError.updateFunctionConfigurationError`
284298
func enableDebugLogging(lambdaClient: LambdaClient, functionName: String) async throws {
285299
let envVariables = [
286300
"LOG_LEVEL": "DEBUG"
@@ -300,8 +314,6 @@ struct ExampleCommand: ParsableCommand {
300314
}
301315

302316
if response.error != nil {
303-
print("Response has an error section:")
304-
dump(response.error)
305317
throw ExampleError.updateFunctionConfigurationError
306318
}
307319

@@ -319,6 +331,7 @@ struct ExampleCommand: ParsableCommand {
319331
throw ExampleError.updateFunctionConfigurationError
320332
}
321333
}
334+
// snippet-end:[lambda.swift.UpdateFunctionConfiguration]
322335

323336
// snippet-start:[swift.lambda-basics.ListFunctionsPaginated]
324337
/// Returns an array containing the names of all AWS Lambda functions
@@ -476,7 +489,7 @@ struct ExampleCommand: ParsableCommand {
476489
// function.
477490

478491
print("Creating the increment Lambda function...")
479-
if try await createFunction(lambdaClient: lambdaClient, name: basicsFunctionName,
492+
if try await createFunction(lambdaClient: lambdaClient, functionName: basicsFunctionName,
480493
roleArn: iamRole.arn, path: incpath) {
481494
print("Running increment function calls...")
482495
for number in 0...4 {

0 commit comments

Comments
 (0)