Skip to content

Commit e23dec7

Browse files
committed
Checkpoint commit
1 parent 87720b2 commit e23dec7

File tree

7 files changed

+90
-14
lines changed

7 files changed

+90
-14
lines changed

.doc_gen/metadata/lambda_metadata.yaml

Lines changed: 9 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:

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct CalculatorLambda: LambdaHandler {
6666
init(context: LambdaInitializationContext) async throws {
6767
// Display the `LOG_LEVEL` configuration for this process.
6868
context.logger.info(
69-
"Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info" )"
69+
"LOG_LEVEL environment variable: \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info" )"
7070
)
7171
}
7272
// snippet-end:[lambda.swift.calculator.handler.init]

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct IncrementLambda: LambdaHandler {
4545
init(context: LambdaInitializationContext) async throws {
4646
// Display the `LOG_LEVEL` configuration for this process.
4747
context.logger.info(
48-
"Log Level env var : \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info" )"
48+
"LOG_LEVEL environment variable: \(ProcessInfo.processInfo.environment["LOG_LEVEL"] ?? "info" )"
4949
)
5050
}
5151
// snippet-end:[lambda.swift.increment.handler.init]

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
}

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

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
// SPDX-License-Identifier: Apache-2.0
33
//
4-
/// An example that demonstrates how to watch an transcribe event stream to
5-
/// transcribe audio from a file to the console.
4+
/// An example demonstrating a variety of important AWS Lambda functions.
65

76
// snippet-start:[swift.lambda-basics.imports-all]
87
import ArgumentParser
@@ -121,6 +120,10 @@ struct ExampleCommand: ParsableCommand {
121120
)
122121
)
123122

123+
guard let role = output.role else {
124+
throw ExampleError.roleCreateError
125+
}
126+
124127
// Wait for the role to be ready for use.
125128

126129
_ = try await iamClient.waitUntilRoleExists(
@@ -132,10 +135,6 @@ struct ExampleCommand: ParsableCommand {
132135
input: GetRoleInput(roleName: roleName)
133136
)
134137

135-
guard let role = output.role else {
136-
throw ExampleError.roleCreateError
137-
}
138-
139138
return role
140139
}
141140

@@ -195,6 +194,8 @@ struct ExampleCommand: ParsableCommand {
195194
)
196195
)
197196
} catch {
197+
print("*** Error creating Lambda function:")
198+
dump(error)
198199
return false
199200
}
200201
// snippet-end:[swift.lambda-basics.CreateFunction]
@@ -231,7 +232,7 @@ struct ExampleCommand: ParsableCommand {
231232
/// - Throws: `ExampleError.zipFileReadError`
232233
/// - Returns: `true` if the function's code is updated successfully.
233234
/// Otherwise, returns `false`.
234-
func updateFunctionCode(lambdaClient: LambdaClient, name: String,
235+
func updateFunctionCode(lambdaClient: LambdaClient, functionName: String,
235236
path: String) async throws -> Bool {
236237
// snippet-start:[swift.lambda-basics.UpdateFunctionCode]
237238
let zipUrl = URL(fileURLWithPath: path)
@@ -251,7 +252,7 @@ struct ExampleCommand: ParsableCommand {
251252
do {
252253
_ = try await lambdaClient.updateFunctionCode(
253254
input: UpdateFunctionCodeInput(
254-
functionName: name,
255+
functionName: functionName,
255256
zipFile: zipData
256257
)
257258
)
@@ -267,7 +268,7 @@ struct ExampleCommand: ParsableCommand {
267268
maxDelay: 2
268269
),
269270
input: GetFunctionInput(
270-
functionName: name
271+
functionName: functionName
271272
)
272273
)
273274

@@ -280,6 +281,45 @@ struct ExampleCommand: ParsableCommand {
280281
}
281282
// snippet-end:[swift.lambda-basics.UpdateFunctionCode.wait]
282283

284+
func enableDebugLogging(lambdaClient: LambdaClient, functionName: String) async throws {
285+
let envVariables = [
286+
"LOG_LEVEL": "DEBUG"
287+
]
288+
let environment = LambdaClientTypes.Environment(variables: envVariables)
289+
290+
do {
291+
let output = try await lambdaClient.updateFunctionConfiguration(
292+
input: UpdateFunctionConfigurationInput(
293+
environment: environment,
294+
functionName: functionName
295+
)
296+
)
297+
298+
guard let response = output.environment else {
299+
throw ExampleError.environmentResponseMissingError
300+
}
301+
302+
if response.error != nil {
303+
print("Response has an error section:")
304+
dump(response.error)
305+
throw ExampleError.updateFunctionConfigurationError
306+
}
307+
308+
guard let retVariables = response.variables else {
309+
throw ExampleError.environmentVariablesMissingError
310+
}
311+
312+
for envVar in retVariables {
313+
if envVar.key == "LOG_LEVEL" && envVar.value != "DEBUG" {
314+
print("*** Log level is not set to DEBUG!")
315+
throw ExampleError.logLevelIncorrectError
316+
}
317+
}
318+
} catch {
319+
throw ExampleError.updateFunctionConfigurationError
320+
}
321+
}
322+
283323
// snippet-start:[swift.lambda-basics.ListFunctionsPaginated]
284324
/// Returns an array containing the names of all AWS Lambda functions
285325
/// available to the user.
@@ -438,6 +478,7 @@ struct ExampleCommand: ParsableCommand {
438478
print("Creating the increment Lambda function...")
439479
if try await createFunction(lambdaClient: lambdaClient, name: basicsFunctionName,
440480
roleArn: iamRole.arn, path: incpath) {
481+
print("Running increment function calls...")
441482
for number in 0...4 {
442483
do {
443484
let answer = try await invokeIncrement(lambdaClient: lambdaClient, number: number)
@@ -446,15 +487,23 @@ struct ExampleCommand: ParsableCommand {
446487
print("Error incrementing \(number): ", error.localizedDescription)
447488
}
448489
}
490+
} else {
491+
print("*** Failed to create the increment function.")
449492
}
450493

494+
// Enable debug logging.
495+
496+
print("\nEnabling debug logging...")
497+
try await enableDebugLogging(lambdaClient: lambdaClient, functionName: basicsFunctionName)
498+
451499
// Change it to a basic arithmetic calculator. Then invoke it a few
452500
// times.
453501

454502
print("\nReplacing the Lambda function with a calculator...")
455503

456-
if try await updateFunctionCode(lambdaClient: lambdaClient, name: "lambda-basics-function",
504+
if try await updateFunctionCode(lambdaClient: lambdaClient, functionName: basicsFunctionName,
457505
path: calcpath) {
506+
print("Running calculator function calls...")
458507
for x in [6, 10] {
459508
for y in [2, 4] {
460509
for action in ["plus", "minus", "times", "divided-by"] {

0 commit comments

Comments
 (0)