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]
87import 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 ( " \n Enabling 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 ( " \n Replacing 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