AWS code-library documentation change
Summary
Updated Swift Lambda code examples to use LambdaRuntime structure, added debug logging configuration function, improved error handling, and parameter naming consistency
Security assessment
The change adds explicit documentation for configuring LOG_LEVEL environment variable to DEBUG via new enableDebugLogging() function. While logging configuration is a security-related practice, there's no evidence this addresses a specific vulnerability. The change demonstrates security-aware logging configuration as a feature.
Diff
diff --git a/code-library/latest/ug/swift_1_lambda_code_examples.md b/code-library/latest/ug/swift_1_lambda_code_examples.md index 64d1cb384..7e774c699 100644 --- a//code-library/latest/ug/swift_1_lambda_code_examples.md +++ b//code-library/latest/ug/swift_1_lambda_code_examples.md @@ -76 +76 @@ Define the first Lambda function, which simply increments the specified value. - from: "1.0.0-alpha"), + branch: "main"), @@ -113,18 +113 @@ Define the first Lambda function, which simply increments the specified value. - /// 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 { - - /// 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" )" - ) - } - - /// The Lambda function's entry point. Called by the Lambda runtime. + /// The Lambda function body. @@ -140 +123,2 @@ Define the first Lambda function, which simply increments the specified value. - func handle(_ event: Request, context: LambdaContext) async throws -> Response { + let incrementLambdaRuntime = LambdaRuntime { + (event: Request, context: LambdaContext) -> Response in @@ -154 +138,4 @@ Define the first Lambda function, which simply increments the specified value. - } + + // Run the Lambda runtime code. + + try await incrementLambdaRuntime.run() @@ -180 +167 @@ Define the second Lambda function, which performs an arithmetic operation on two - from: "1.0.0-alpha"), + branch: "main"), @@ -236,17 +222,0 @@ Define the second Lambda function, which performs an arithmetic operation on two - /// 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 { - - /// 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" )" - ) - } - @@ -263 +233,2 @@ Define the second Lambda function, which performs an arithmetic operation on two - func handle(_ event: Request, context: LambdaContext) async throws -> Response { + let calculatorLambdaRuntime = LambdaRuntime { + (_ event: Request, context: LambdaContext) -> Response in @@ -270 +241 @@ Define the second Lambda function, which performs an arithmetic operation on two - actionFunc = actions[action] + actionFunc = await actions[action] @@ -288 +259,2 @@ Define the second Lambda function, which performs an arithmetic operation on two - } + + try await calculatorLambdaRuntime.run() @@ -337,2 +309 @@ Define the main program that will invoke the two Lambda functions. - /// An example that demonstrates how to watch an transcribe event stream to - /// transcribe audio from a file to the console. + /// An example demonstrating a variety of important AWS Lambda functions. @@ -450,0 +422,4 @@ Define the main program that will invoke the two Lambda functions. + guard let role = output.role else { + throw ExampleError.roleCreateError + } + @@ -462,4 +436,0 @@ Define the main program that will invoke the two Lambda functions. - guard let role = output.role else { - throw ExampleError.roleCreateError - } - @@ -493 +464 @@ Define the main program that will invoke the two Lambda functions. - /// - name: The name of the AWS Lambda function to create. + /// - functionName: The name of the AWS Lambda function to create. @@ -499 +470 @@ Define the main program that will invoke the two Lambda functions. - func createFunction(lambdaClient: LambdaClient, name: String, + func createFunction(lambdaClient: LambdaClient, functionName: String, @@ -514 +485 @@ Define the main program that will invoke the two Lambda functions. - functionName: name, + functionName: functionName, @@ -520,0 +492,2 @@ Define the main program that will invoke the two Lambda functions. + print("*** Error creating Lambda function:") + dump(error) @@ -532 +505 @@ Define the main program that will invoke the two Lambda functions. - input: GetFunctionInput(functionName: name) + input: GetFunctionInput(functionName: functionName) @@ -548 +521 @@ Define the main program that will invoke the two Lambda functions. - /// - name: The name of the AWS Lambda function to update. + /// - functionName: The name of the AWS Lambda function to update. @@ -554 +527 @@ Define the main program that will invoke the two Lambda functions. - func updateFunctionCode(lambdaClient: LambdaClient, name: String, + func updateFunctionCode(lambdaClient: LambdaClient, functionName: String, @@ -573 +546 @@ Define the main program that will invoke the two Lambda functions. - functionName: name, + functionName: functionName, @@ -588 +561 @@ Define the main program that will invoke the two Lambda functions. - functionName: name + functionName: functionName @@ -599,0 +573,50 @@ Define the main program that will invoke the two Lambda functions. + /// Tell the server-side component to log debug output by setting its + /// environment's `LOG_LEVEL` to `DEBUG`. + /// + /// - Parameters: + /// - lambdaClient: The `LambdaClient` to use. + /// - functionName: The name of the AWS Lambda function to enable debug + /// logging for. + /// + /// - Throws: `ExampleError.environmentResponseMissingError`, + /// `ExampleError.updateFunctionConfigurationError`, + /// `ExampleError.environmentVariablesMissingError`, + /// `ExampleError.logLevelIncorrectError`, + /// `ExampleError.updateFunctionConfigurationError` + func enableDebugLogging(lambdaClient: LambdaClient, functionName: String) async throws { + let envVariables = [ + "LOG_LEVEL": "DEBUG" + ] + let environment = LambdaClientTypes.Environment(variables: envVariables) + + do { + let output = try await lambdaClient.updateFunctionConfiguration( + input: UpdateFunctionConfigurationInput( + environment: environment, + functionName: functionName + ) + ) + + guard let response = output.environment else { + throw ExampleError.environmentResponseMissingError + } + + if response.error != nil { + throw ExampleError.updateFunctionConfigurationError + } + + guard let retVariables = response.variables else { + throw ExampleError.environmentVariablesMissingError + } + + for envVar in retVariables { + if envVar.key == "LOG_LEVEL" && envVar.value != "DEBUG" { + print("*** Log level is not set to DEBUG!") + throw ExampleError.logLevelIncorrectError + } + } + } catch { + throw ExampleError.updateFunctionConfigurationError + } + } + @@ -750 +773 @@ Define the main program that will invoke the two Lambda functions. - if try await createFunction(lambdaClient: lambdaClient, name: basicsFunctionName, + if try await createFunction(lambdaClient: lambdaClient, functionName: basicsFunctionName, @@ -751,0 +775 @@ Define the main program that will invoke the two Lambda functions. + print("Running increment function calls...") @@ -759,0 +784,2 @@ Define the main program that will invoke the two Lambda functions. + } else { + print("*** Failed to create the increment function.") @@ -761,0 +788,5 @@ Define the main program that will invoke the two Lambda functions. + // Enable debug logging. + + print("\nEnabling debug logging...") + try await enableDebugLogging(lambdaClient: lambdaClient, functionName: basicsFunctionName) + @@ -767 +798 @@ Define the main program that will invoke the two Lambda functions. - if try await updateFunctionCode(lambdaClient: lambdaClient, name: "lambda-basics-function", + if try await updateFunctionCode(lambdaClient: lambdaClient, functionName: basicsFunctionName, @@ -768,0 +800 @@ Define the main program that will invoke the two Lambda functions. + print("Running calculator function calls...") @@ -871,0 +904,10 @@ Define the main program that will invoke the two Lambda functions. + /// 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 @@ -898,0 +941,8 @@ Define the main program that will invoke the two Lambda functions. + 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."