選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

Lambda examples using SDK for Swift - AWS SDK for Swift
此頁面尚未翻譯為您的語言。 請求翻譯

Lambda examples using SDK for Swift

The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for Swift with Lambda.

Basics are code examples that show you how to perform the essential operations within a service.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Basics

The following code example shows how to:

  • Create an IAM role and Lambda function, then upload handler code.

  • Invoke the function with a single parameter and get results.

  • Update the function code and configure with an environment variable.

  • Invoke the function with new parameters and get results. Display the returned execution log.

  • List the functions for your account, then clean up resources.

For more information, see Create a Lambda function with the console.

SDK for Swift
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

Define the first Lambda function, which simply increments the specified value.

// swift-tools-version: 5.9 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 // // The swift-tools-version declares the minimum version of Swift required to // build this package. import PackageDescription let package = Package( name: "increment", // Let Xcode know the minimum Apple platforms supported. platforms: [ .macOS(.v13) ], dependencies: [ // Dependencies declare other packages that this package depends on. .package( url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha"), ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. // Targets can depend on other targets in this package and products // from dependencies. .executableTarget( name: "increment", dependencies: [ .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), ], path: "Sources" ) ] ) import Foundation import AWSLambdaRuntime /// Represents the contents of the requests being received from the client. /// This structure must be `Decodable` to indicate that its initializer /// converts an external representation into this type. struct Request: Decodable, Sendable { /// The action to perform. let action: String /// The number to act upon. let number: Int } /// The contents of the response sent back to the client. This must be /// `Encodable`. struct Response: Encodable, Sendable { /// The resulting value after performing the action. let answer: Int? } /// 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. /// /// - Parameters: /// - event: The `Request` describing the request made by the /// client. /// - context: A `LambdaContext` describing the context in /// which the lambda function is running. /// /// - Returns: A `Response` object that will be encoded to JSON and sent /// to the client by the Lambda runtime. func handle(_ event: Request, context: LambdaContext) async throws -> Response { let action = event.action var answer: Int? if action != "increment" { context.logger.error("Unrecognized operation: \"\(action)\". The only supported action is \"increment\".") } else { answer = event.number + 1 context.logger.info("The calculated answer is \(answer!).") } let response = Response(answer: answer) return response } }

Define the second Lambda function, which performs an arithmetic operation on two numbers.

// swift-tools-version: 5.9 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 // // The swift-tools-version declares the minimum version of Swift required to // build this package. import PackageDescription let package = Package( name: "calculator", // Let Xcode know the minimum Apple platforms supported. platforms: [ .macOS(.v13) ], dependencies: [ // Dependencies declare other packages that this package depends on. .package( url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha"), ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. // Targets can depend on other targets in this package and products // from dependencies. .executableTarget( name: "calculator", dependencies: [ .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), ], path: "Sources" ) ] ) import Foundation import AWSLambdaRuntime /// Represents the contents of the requests being received from the client. /// This structure must be `Decodable` to indicate that its initializer /// converts an external representation into this type. struct Request: Decodable, Sendable { /// The action to perform. let action: String /// The first number to act upon. let x: Int /// The second number to act upon. let y: Int } /// A dictionary mapping operation names to closures that perform that /// operation and return the result. let actions = [ "plus": { (x: Int, y: Int) -> Int in return x + y }, "minus": { (x: Int, y: Int) -> Int in return x - y }, "times": { (x: Int, y: Int) -> Int in return x * y }, "divided-by": { (x: Int, y: Int) -> Int in return x / y } ] /// The contents of the response sent back to the client. This must be /// `Encodable`. struct Response: Encodable, Sendable { /// The resulting value after performing the action. let answer: Int? } /// 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" )" ) } /// The Lambda function's entry point. Called by the Lambda runtime. /// /// - Parameters: /// - event: The `Request` describing the request made by the /// client. /// - context: A `LambdaContext` describing the context in /// which the lambda function is running. /// /// - Returns: A `Response` object that will be encoded to JSON and sent /// to the client by the Lambda runtime. func handle(_ event: Request, context: LambdaContext) async throws -> Response { let action = event.action var answer: Int? var actionFunc: ((Int, Int) -> Int)? // Get the closure to run to perform the calculation. actionFunc = actions[action] guard let actionFunc else { context.logger.error("Unrecognized operation '\(action)\'") return Response(answer: nil) } // Perform the calculation and return the answer. answer = actionFunc(event.x, event.y) guard let answer else { context.logger.error("Error computing \(event.x) \(action) \(event.y)") } context.logger.info("\(event.x) \(action) \(event.y) = \(answer)") return Response(answer: answer) } }

Define the main program that will invoke the two Lambda functions.

// swift-tools-version: 5.9 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 // // The swift-tools-version declares the minimum version of Swift required to // build this package. import PackageDescription let package = Package( name: "lambda-basics", // Let Xcode know the minimum Apple platforms supported. platforms: [ .macOS(.v13) ], dependencies: [ // Dependencies declare other packages that this package depends on. .package( url: "https://github.com/awslabs/aws-sdk-swift", from: "1.0.0"), .package( url: "https://github.com/apple/swift-argument-parser.git", branch: "main" ) ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. // Targets can depend on other targets in this package and products // from dependencies. .executableTarget( name: "lambda-basics", dependencies: [ .product(name: "AWSLambda", package: "aws-sdk-swift"), .product(name: "AWSIAM", package: "aws-sdk-swift"), .product(name: "ArgumentParser", package: "swift-argument-parser") ], path: "Sources" ) ] ) // /// An example that demonstrates how to watch an transcribe event stream to /// transcribe audio from a file to the console. import ArgumentParser import AWSIAM import SmithyWaitersAPI import AWSClientRuntime import AWSLambda import Foundation /// Represents the contents of the requests being received from the client. /// This structure must be `Decodable` to indicate that its initializer /// converts an external representation into this type. struct IncrementRequest: Encodable, Decodable, Sendable { /// The action to perform. let action: String /// The number to act upon. let number: Int } struct Response: Encodable, Decodable, Sendable { /// The resulting value after performing the action. let answer: Int? } struct CalculatorRequest: Encodable, Decodable, Sendable { /// The action to perform. let action: String /// The first number to act upon. let x: Int /// The second number to act upon. let y: Int } let exampleName = "SwiftLambdaRoleExample" let basicsFunctionName = "lambda-basics-function" /// The ARN of the standard IAM policy for execution of Lambda functions. let policyARN = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" struct ExampleCommand: ParsableCommand { // -MARK: Command arguments @Option(help: "Name of the IAM Role to use for the Lambda functions") var role = exampleName @Option(help: "Zip archive containing the 'increment' lambda function") var incpath: String @Option(help: "Zip archive containing the 'calculator' lambda function") var calcpath: String @Option(help: "Name of the Amazon S3 Region to use (default: us-east-1)") var region = "us-east-1" static var configuration = CommandConfiguration( commandName: "lambda-basics", abstract: """ This example demonstrates several common operations using AWS Lambda. """, discussion: """ """ ) /// Returns the specified IAM role object. /// /// - Parameters: /// - iamClient: `IAMClient` to use when looking for the role. /// - roleName: The name of the role to check. /// /// - Returns: The `IAMClientTypes.Role` representing the specified role. func getRole(iamClient: IAMClient, roleName: String) async throws -> IAMClientTypes.Role { do { let roleOutput = try await iamClient.getRole( input: GetRoleInput( roleName: roleName ) ) guard let role = roleOutput.role else { throw ExampleError.roleNotFound } return role } catch { throw ExampleError.roleNotFound } } /// Create the AWS IAM role that will be used to access AWS Lambda. /// /// - Parameters: /// - iamClient: The AWS `IAMClient` to use. /// - roleName: The name of the AWS IAM role to use for Lambda. /// /// - Throws: `ExampleError.roleCreateError` /// /// - Returns: The `IAMClientTypes.Role` struct that describes the new role. func createRoleForLambda(iamClient: IAMClient, roleName: String) async throws -> IAMClientTypes.Role { let output = try await iamClient.createRole( input: CreateRoleInput( assumeRolePolicyDocument: """ { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole" } ] } """, roleName: roleName ) ) // Wait for the role to be ready for use. _ = try await iamClient.waitUntilRoleExists( options: WaiterOptions( maxWaitTime: 20, minDelay: 0.5, maxDelay: 2 ), input: GetRoleInput(roleName: roleName) ) guard let role = output.role else { throw ExampleError.roleCreateError } return role } /// Detect whether or not the AWS Lambda function with the specified name /// exists, by requesting its function information. /// /// - Parameters: /// - lambdaClient: The `LambdaClient` to use. /// - name: The name of the AWS Lambda function to find. /// /// - Returns: `true` if the Lambda function exists. Otherwise `false`. func doesLambdaFunctionExist(lambdaClient: LambdaClient, name: String) async -> Bool { do { _ = try await lambdaClient.getFunction( input: GetFunctionInput(functionName: name) ) } catch { return false } return true } /// Create the specified AWS Lambda function. /// /// - Parameters: /// - lambdaClient: The `LambdaClient` to use. /// - name: The name of the AWS Lambda function to create. /// - roleArn: The ARN of the role to apply to the function. /// - path: The path of the Zip archive containing the function. /// /// - Returns: `true` if the AWS Lambda was successfully created; `false` /// if it wasn't. func createFunction(lambdaClient: LambdaClient, name: String, roleArn: String?, path: String) async throws -> Bool { do { // Read the Zip archive containing the AWS Lambda function. let zipUrl = URL(fileURLWithPath: path) let zipData = try Data(contentsOf: zipUrl) // Create the AWS Lambda function that runs the specified code, // using the name given on the command line. The Lambda function // will run using the Amazon Linux 2 runtime. _ = try await lambdaClient.createFunction( input: CreateFunctionInput( code: LambdaClientTypes.FunctionCode(zipFile: zipData), functionName: name, handler: "handle", role: roleArn, runtime: .providedal2 ) ) } catch { return false } // Wait for a while to be sure the function is done being created. let output = try await lambdaClient.waitUntilFunctionActiveV2( options: WaiterOptions( maxWaitTime: 20, minDelay: 0.5, maxDelay: 2 ), input: GetFunctionInput(functionName: name) ) switch output.result { case .success: return true case .failure: return false } } /// Update the AWS Lambda function with new code to run when the function /// is invoked. /// /// - Parameters: /// - lambdaClient: The `LambdaClient` to use. /// - name: The name of the AWS Lambda function to update. /// - path: The pathname of the Zip file containing the packaged Lambda /// function. /// - Throws: `ExampleError.zipFileReadError` /// - Returns: `true` if the function's code is updated successfully. /// Otherwise, returns `false`. func updateFunctionCode(lambdaClient: LambdaClient, name: String, path: String) async throws -> Bool { let zipUrl = URL(fileURLWithPath: path) let zipData: Data // Read the function's Zip file. do { zipData = try Data(contentsOf: zipUrl) } catch { throw ExampleError.zipFileReadError } // Update the function's code and wait for the updated version to be // ready for use. do { _ = try await lambdaClient.updateFunctionCode( input: UpdateFunctionCodeInput( functionName: name, zipFile: zipData ) ) } catch { return false } let output = try await lambdaClient.waitUntilFunctionUpdatedV2( options: WaiterOptions( maxWaitTime: 20, minDelay: 0.5, maxDelay: 2 ), input: GetFunctionInput( functionName: name ) ) switch output.result { case .success: return true case .failure: return false } } /// Returns an array containing the names of all AWS Lambda functions /// available to the user. /// /// - Parameter lambdaClient: The `IAMClient` to use. /// /// - Throws: `ExampleError.listFunctionsError` /// /// - Returns: An array of lambda function name strings. func getFunctionNames(lambdaClient: LambdaClient) async throws -> [String] { let pages = lambdaClient.listFunctionsPaginated( input: ListFunctionsInput() ) var functionNames: [String] = [] for try await page in pages { guard let functions = page.functions else { throw ExampleError.listFunctionsError } for function in functions { functionNames.append(function.functionName ?? "<unknown>") } } return functionNames } /// Invoke the Lambda function to increment a value. /// /// - Parameters: /// - lambdaClient: The `IAMClient` to use. /// - number: The number to increment. /// /// - Throws: `ExampleError.noAnswerReceived`, `ExampleError.invokeError` /// /// - Returns: An integer number containing the incremented value. func invokeIncrement(lambdaClient: LambdaClient, number: Int) async throws -> Int { do { let incRequest = IncrementRequest(action: "increment", number: number) let incData = try! JSONEncoder().encode(incRequest) // Invoke the lambda function. let invokeOutput = try await lambdaClient.invoke( input: InvokeInput( functionName: "lambda-basics-function", payload: incData ) ) let response = try! JSONDecoder().decode(Response.self, from:invokeOutput.payload!) guard let answer = response.answer else { throw ExampleError.noAnswerReceived } return answer } catch { throw ExampleError.invokeError } } /// Invoke the calculator Lambda function. /// /// - Parameters: /// - lambdaClient: The `IAMClient` to use. /// - action: Which arithmetic operation to perform: "plus", "minus", /// "times", or "divided-by". /// - x: The first number to use in the computation. /// - y: The second number to use in the computation. /// /// - Throws: `ExampleError.noAnswerReceived`, `ExampleError.invokeError` /// /// - Returns: The computed answer as an `Int`. func invokeCalculator(lambdaClient: LambdaClient, action: String, x: Int, y: Int) async throws -> Int { do { let calcRequest = CalculatorRequest(action: action, x: x, y: y) let calcData = try! JSONEncoder().encode(calcRequest) // Invoke the lambda function. let invokeOutput = try await lambdaClient.invoke( input: InvokeInput( functionName: "lambda-basics-function", payload: calcData ) ) let response = try! JSONDecoder().decode(Response.self, from:invokeOutput.payload!) guard let answer = response.answer else { throw ExampleError.noAnswerReceived } return answer } catch { throw ExampleError.invokeError } } /// Perform the example's tasks. func basics() async throws { let iamClient = try await IAMClient( config: IAMClient.IAMClientConfiguration(region: region) ) let lambdaClient = try await LambdaClient( config: LambdaClient.LambdaClientConfiguration(region: region) ) /// The IAM role to use for the example. var iamRole: IAMClientTypes.Role // Look for the specified role. If it already exists, use it. If not, // create it and attach the desired policy to it. do { iamRole = try await getRole(iamClient: iamClient, roleName: role) } catch ExampleError.roleNotFound { // The role wasn't found, so create it and attach the needed // policy. iamRole = try await createRoleForLambda(iamClient: iamClient, roleName: role) do { _ = try await iamClient.attachRolePolicy( input: AttachRolePolicyInput(policyArn: policyARN, roleName: role) ) } catch { throw ExampleError.policyError } } // Give the policy time to attach to the role. sleep(5) // Look to see if the function already exists. If it does, throw an // error. if await doesLambdaFunctionExist(lambdaClient: lambdaClient, name: basicsFunctionName) { throw ExampleError.functionAlreadyExists } // Create, then invoke, the "increment" version of the calculator // function. print("Creating the increment Lambda function...") if try await createFunction(lambdaClient: lambdaClient, name: basicsFunctionName, roleArn: iamRole.arn, path: incpath) { for number in 0...4 { do { let answer = try await invokeIncrement(lambdaClient: lambdaClient, number: number) print("Increment \(number) = \(answer)") } catch { print("Error incrementing \(number): ", error.localizedDescription) } } } // Change it to a basic arithmetic calculator. Then invoke it a few // times. print("\nReplacing the Lambda function with a calculator...") if try await updateFunctionCode(lambdaClient: lambdaClient, name: "lambda-basics-function", path: calcpath) { for x in [6, 10] { for y in [2, 4] { for action in ["plus", "minus", "times", "divided-by"] { do { let answer = try await invokeCalculator(lambdaClient: lambdaClient, action: action, x: x, y: y) print("\(x) \(action) \(y) = \(answer)") } catch { print("Error calculating \(x) \(action) \(y): ", error.localizedDescription) } } } } } // List all lambda functions. let functionNames = try await getFunctionNames(lambdaClient: lambdaClient) if functionNames.count > 0 { print("\nAWS Lambda functions available on your account:") for name in functionNames { print(" \(name)") } } // Delete the lambda function. print("Deleting lambda function...") do { _ = try await lambdaClient.deleteFunction( input: DeleteFunctionInput( functionName: "lambda-basics-function" ) ) } catch { print("Error: Unable to delete the function.") } // Detach the role from the policy, then delete the role. print("Deleting the AWS IAM role...") do { _ = try await iamClient.detachRolePolicy( input: DetachRolePolicyInput( policyArn: policyARN, roleName: role ) ) _ = try await iamClient.deleteRole( input: DeleteRoleInput( roleName: role ) ) } catch { throw ExampleError.deleteRoleError } } } // -MARK: - Entry point /// The program's asynchronous entry point. @main struct Main { static func main() async { let args = Array(CommandLine.arguments.dropFirst()) do { let command = try ExampleCommand.parse(args) try await command.basics() } catch { ExampleCommand.exit(withError: error) } } } /// Errors thrown by the example's functions. enum ExampleError: Error { /// An AWS Lambda function with the specified name already exists. case functionAlreadyExists /// The specified role doesn't exist. case roleNotFound /// Unable to create the role. case roleCreateError /// Unable to delete the role. case deleteRoleError /// Unable to attach a policy to the role. case policyError /// Unable to get the executable directory. case executableNotFound /// An error occurred creating a lambda function. case createLambdaError /// An error occurred invoking the lambda function. case invokeError /// No answer received from the invocation. case noAnswerReceived /// Unable to list the AWS Lambda functions. case listFunctionsError /// Unable to update the AWS Lambda function. case updateFunctionError /// Unable to load the AWS Lambda function's Zip file. case zipFileReadError var errorDescription: String? { switch self { case .functionAlreadyExists: return "An AWS Lambda function with that name already exists." case .roleNotFound: return "The specified role doesn't exist." case .deleteRoleError: return "Unable to delete the AWS IAM role." case .roleCreateError: return "Unable to create the specified role." case .policyError: return "An error occurred attaching the policy to the role." case .executableNotFound: return "Unable to find the executable program directory." case .createLambdaError: return "An error occurred creating a lambda function." case .invokeError: return "An error occurred invoking a lambda function." case .noAnswerReceived: return "No answer received from the lambda function." case .listFunctionsError: return "Unable to list the AWS Lambda functions." case .updateFunctionError: return "Unable to update the AWS lambda function." case .zipFileReadError: return "Unable to read the AWS Lambda function." } } }

The following code example shows how to:

  • Create an IAM role and Lambda function, then upload handler code.

  • Invoke the function with a single parameter and get results.

  • Update the function code and configure with an environment variable.

  • Invoke the function with new parameters and get results. Display the returned execution log.

  • List the functions for your account, then clean up resources.

For more information, see Create a Lambda function with the console.

SDK for Swift
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

Define the first Lambda function, which simply increments the specified value.

// swift-tools-version: 5.9 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 // // The swift-tools-version declares the minimum version of Swift required to // build this package. import PackageDescription let package = Package( name: "increment", // Let Xcode know the minimum Apple platforms supported. platforms: [ .macOS(.v13) ], dependencies: [ // Dependencies declare other packages that this package depends on. .package( url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha"), ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. // Targets can depend on other targets in this package and products // from dependencies. .executableTarget( name: "increment", dependencies: [ .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), ], path: "Sources" ) ] ) import Foundation import AWSLambdaRuntime /// Represents the contents of the requests being received from the client. /// This structure must be `Decodable` to indicate that its initializer /// converts an external representation into this type. struct Request: Decodable, Sendable { /// The action to perform. let action: String /// The number to act upon. let number: Int } /// The contents of the response sent back to the client. This must be /// `Encodable`. struct Response: Encodable, Sendable { /// The resulting value after performing the action. let answer: Int? } /// 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. /// /// - Parameters: /// - event: The `Request` describing the request made by the /// client. /// - context: A `LambdaContext` describing the context in /// which the lambda function is running. /// /// - Returns: A `Response` object that will be encoded to JSON and sent /// to the client by the Lambda runtime. func handle(_ event: Request, context: LambdaContext) async throws -> Response { let action = event.action var answer: Int? if action != "increment" { context.logger.error("Unrecognized operation: \"\(action)\". The only supported action is \"increment\".") } else { answer = event.number + 1 context.logger.info("The calculated answer is \(answer!).") } let response = Response(answer: answer) return response } }

Define the second Lambda function, which performs an arithmetic operation on two numbers.

// swift-tools-version: 5.9 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 // // The swift-tools-version declares the minimum version of Swift required to // build this package. import PackageDescription let package = Package( name: "calculator", // Let Xcode know the minimum Apple platforms supported. platforms: [ .macOS(.v13) ], dependencies: [ // Dependencies declare other packages that this package depends on. .package( url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha"), ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. // Targets can depend on other targets in this package and products // from dependencies. .executableTarget( name: "calculator", dependencies: [ .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"), ], path: "Sources" ) ] ) import Foundation import AWSLambdaRuntime /// Represents the contents of the requests being received from the client. /// This structure must be `Decodable` to indicate that its initializer /// converts an external representation into this type. struct Request: Decodable, Sendable { /// The action to perform. let action: String /// The first number to act upon. let x: Int /// The second number to act upon. let y: Int } /// A dictionary mapping operation names to closures that perform that /// operation and return the result. let actions = [ "plus": { (x: Int, y: Int) -> Int in return x + y }, "minus": { (x: Int, y: Int) -> Int in return x - y }, "times": { (x: Int, y: Int) -> Int in return x * y }, "divided-by": { (x: Int, y: Int) -> Int in return x / y } ] /// The contents of the response sent back to the client. This must be /// `Encodable`. struct Response: Encodable, Sendable { /// The resulting value after performing the action. let answer: Int? } /// 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" )" ) } /// The Lambda function's entry point. Called by the Lambda runtime. /// /// - Parameters: /// - event: The `Request` describing the request made by the /// client. /// - context: A `LambdaContext` describing the context in /// which the lambda function is running. /// /// - Returns: A `Response` object that will be encoded to JSON and sent /// to the client by the Lambda runtime. func handle(_ event: Request, context: LambdaContext) async throws -> Response { let action = event.action var answer: Int? var actionFunc: ((Int, Int) -> Int)? // Get the closure to run to perform the calculation. actionFunc = actions[action] guard let actionFunc else { context.logger.error("Unrecognized operation '\(action)\'") return Response(answer: nil) } // Perform the calculation and return the answer. answer = actionFunc(event.x, event.y) guard let answer else { context.logger.error("Error computing \(event.x) \(action) \(event.y)") } context.logger.info("\(event.x) \(action) \(event.y) = \(answer)") return Response(answer: answer) } }

Define the main program that will invoke the two Lambda functions.

// swift-tools-version: 5.9 // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 // // The swift-tools-version declares the minimum version of Swift required to // build this package. import PackageDescription let package = Package( name: "lambda-basics", // Let Xcode know the minimum Apple platforms supported. platforms: [ .macOS(.v13) ], dependencies: [ // Dependencies declare other packages that this package depends on. .package( url: "https://github.com/awslabs/aws-sdk-swift", from: "1.0.0"), .package( url: "https://github.com/apple/swift-argument-parser.git", branch: "main" ) ], targets: [ // Targets are the basic building blocks of a package, defining a module or a test suite. // Targets can depend on other targets in this package and products // from dependencies. .executableTarget( name: "lambda-basics", dependencies: [ .product(name: "AWSLambda", package: "aws-sdk-swift"), .product(name: "AWSIAM", package: "aws-sdk-swift"), .product(name: "ArgumentParser", package: "swift-argument-parser") ], path: "Sources" ) ] ) // /// An example that demonstrates how to watch an transcribe event stream to /// transcribe audio from a file to the console. import ArgumentParser import AWSIAM import SmithyWaitersAPI import AWSClientRuntime import AWSLambda import Foundation /// Represents the contents of the requests being received from the client. /// This structure must be `Decodable` to indicate that its initializer /// converts an external representation into this type. struct IncrementRequest: Encodable, Decodable, Sendable { /// The action to perform. let action: String /// The number to act upon. let number: Int } struct Response: Encodable, Decodable, Sendable { /// The resulting value after performing the action. let answer: Int? } struct CalculatorRequest: Encodable, Decodable, Sendable { /// The action to perform. let action: String /// The first number to act upon. let x: Int /// The second number to act upon. let y: Int } let exampleName = "SwiftLambdaRoleExample" let basicsFunctionName = "lambda-basics-function" /// The ARN of the standard IAM policy for execution of Lambda functions. let policyARN = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" struct ExampleCommand: ParsableCommand { // -MARK: Command arguments @Option(help: "Name of the IAM Role to use for the Lambda functions") var role = exampleName @Option(help: "Zip archive containing the 'increment' lambda function") var incpath: String @Option(help: "Zip archive containing the 'calculator' lambda function") var calcpath: String @Option(help: "Name of the Amazon S3 Region to use (default: us-east-1)") var region = "us-east-1" static var configuration = CommandConfiguration( commandName: "lambda-basics", abstract: """ This example demonstrates several common operations using AWS Lambda. """, discussion: """ """ ) /// Returns the specified IAM role object. /// /// - Parameters: /// - iamClient: `IAMClient` to use when looking for the role. /// - roleName: The name of the role to check. /// /// - Returns: The `IAMClientTypes.Role` representing the specified role. func getRole(iamClient: IAMClient, roleName: String) async throws -> IAMClientTypes.Role { do { let roleOutput = try await iamClient.getRole( input: GetRoleInput( roleName: roleName ) ) guard let role = roleOutput.role else { throw ExampleError.roleNotFound } return role } catch { throw ExampleError.roleNotFound } } /// Create the AWS IAM role that will be used to access AWS Lambda. /// /// - Parameters: /// - iamClient: The AWS `IAMClient` to use. /// - roleName: The name of the AWS IAM role to use for Lambda. /// /// - Throws: `ExampleError.roleCreateError` /// /// - Returns: The `IAMClientTypes.Role` struct that describes the new role. func createRoleForLambda(iamClient: IAMClient, roleName: String) async throws -> IAMClientTypes.Role { let output = try await iamClient.createRole( input: CreateRoleInput( assumeRolePolicyDocument: """ { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": {"Service": "lambda.amazonaws.com"}, "Action": "sts:AssumeRole" } ] } """, roleName: roleName ) ) // Wait for the role to be ready for use. _ = try await iamClient.waitUntilRoleExists( options: WaiterOptions( maxWaitTime: 20, minDelay: 0.5, maxDelay: 2 ), input: GetRoleInput(roleName: roleName) ) guard let role = output.role else { throw ExampleError.roleCreateError } return role } /// Detect whether or not the AWS Lambda function with the specified name /// exists, by requesting its function information. /// /// - Parameters: /// - lambdaClient: The `LambdaClient` to use. /// - name: The name of the AWS Lambda function to find. /// /// - Returns: `true` if the Lambda function exists. Otherwise `false`. func doesLambdaFunctionExist(lambdaClient: LambdaClient, name: String) async -> Bool { do { _ = try await lambdaClient.getFunction( input: GetFunctionInput(functionName: name) ) } catch { return false } return true } /// Create the specified AWS Lambda function. /// /// - Parameters: /// - lambdaClient: The `LambdaClient` to use. /// - name: The name of the AWS Lambda function to create. /// - roleArn: The ARN of the role to apply to the function. /// - path: The path of the Zip archive containing the function. /// /// - Returns: `true` if the AWS Lambda was successfully created; `false` /// if it wasn't. func createFunction(lambdaClient: LambdaClient, name: String, roleArn: String?, path: String) async throws -> Bool { do { // Read the Zip archive containing the AWS Lambda function. let zipUrl = URL(fileURLWithPath: path) let zipData = try Data(contentsOf: zipUrl) // Create the AWS Lambda function that runs the specified code, // using the name given on the command line. The Lambda function // will run using the Amazon Linux 2 runtime. _ = try await lambdaClient.createFunction( input: CreateFunctionInput( code: LambdaClientTypes.FunctionCode(zipFile: zipData), functionName: name, handler: "handle", role: roleArn, runtime: .providedal2 ) ) } catch { return false } // Wait for a while to be sure the function is done being created. let output = try await lambdaClient.waitUntilFunctionActiveV2( options: WaiterOptions( maxWaitTime: 20, minDelay: 0.5, maxDelay: 2 ), input: GetFunctionInput(functionName: name) ) switch output.result { case .success: return true case .failure: return false } } /// Update the AWS Lambda function with new code to run when the function /// is invoked. /// /// - Parameters: /// - lambdaClient: The `LambdaClient` to use. /// - name: The name of the AWS Lambda function to update. /// - path: The pathname of the Zip file containing the packaged Lambda /// function. /// - Throws: `ExampleError.zipFileReadError` /// - Returns: `true` if the function's code is updated successfully. /// Otherwise, returns `false`. func updateFunctionCode(lambdaClient: LambdaClient, name: String, path: String) async throws -> Bool { let zipUrl = URL(fileURLWithPath: path) let zipData: Data // Read the function's Zip file. do { zipData = try Data(contentsOf: zipUrl) } catch { throw ExampleError.zipFileReadError } // Update the function's code and wait for the updated version to be // ready for use. do { _ = try await lambdaClient.updateFunctionCode( input: UpdateFunctionCodeInput( functionName: name, zipFile: zipData ) ) } catch { return false } let output = try await lambdaClient.waitUntilFunctionUpdatedV2( options: WaiterOptions( maxWaitTime: 20, minDelay: 0.5, maxDelay: 2 ), input: GetFunctionInput( functionName: name ) ) switch output.result { case .success: return true case .failure: return false } } /// Returns an array containing the names of all AWS Lambda functions /// available to the user. /// /// - Parameter lambdaClient: The `IAMClient` to use. /// /// - Throws: `ExampleError.listFunctionsError` /// /// - Returns: An array of lambda function name strings. func getFunctionNames(lambdaClient: LambdaClient) async throws -> [String] { let pages = lambdaClient.listFunctionsPaginated( input: ListFunctionsInput() ) var functionNames: [String] = [] for try await page in pages { guard let functions = page.functions else { throw ExampleError.listFunctionsError } for function in functions { functionNames.append(function.functionName ?? "<unknown>") } } return functionNames } /// Invoke the Lambda function to increment a value. /// /// - Parameters: /// - lambdaClient: The `IAMClient` to use. /// - number: The number to increment. /// /// - Throws: `ExampleError.noAnswerReceived`, `ExampleError.invokeError` /// /// - Returns: An integer number containing the incremented value. func invokeIncrement(lambdaClient: LambdaClient, number: Int) async throws -> Int { do { let incRequest = IncrementRequest(action: "increment", number: number) let incData = try! JSONEncoder().encode(incRequest) // Invoke the lambda function. let invokeOutput = try await lambdaClient.invoke( input: InvokeInput( functionName: "lambda-basics-function", payload: incData ) ) let response = try! JSONDecoder().decode(Response.self, from:invokeOutput.payload!) guard let answer = response.answer else { throw ExampleError.noAnswerReceived } return answer } catch { throw ExampleError.invokeError } } /// Invoke the calculator Lambda function. /// /// - Parameters: /// - lambdaClient: The `IAMClient` to use. /// - action: Which arithmetic operation to perform: "plus", "minus", /// "times", or "divided-by". /// - x: The first number to use in the computation. /// - y: The second number to use in the computation. /// /// - Throws: `ExampleError.noAnswerReceived`, `ExampleError.invokeError` /// /// - Returns: The computed answer as an `Int`. func invokeCalculator(lambdaClient: LambdaClient, action: String, x: Int, y: Int) async throws -> Int { do { let calcRequest = CalculatorRequest(action: action, x: x, y: y) let calcData = try! JSONEncoder().encode(calcRequest) // Invoke the lambda function. let invokeOutput = try await lambdaClient.invoke( input: InvokeInput( functionName: "lambda-basics-function", payload: calcData ) ) let response = try! JSONDecoder().decode(Response.self, from:invokeOutput.payload!) guard let answer = response.answer else { throw ExampleError.noAnswerReceived } return answer } catch { throw ExampleError.invokeError } } /// Perform the example's tasks. func basics() async throws { let iamClient = try await IAMClient( config: IAMClient.IAMClientConfiguration(region: region) ) let lambdaClient = try await LambdaClient( config: LambdaClient.LambdaClientConfiguration(region: region) ) /// The IAM role to use for the example. var iamRole: IAMClientTypes.Role // Look for the specified role. If it already exists, use it. If not, // create it and attach the desired policy to it. do { iamRole = try await getRole(iamClient: iamClient, roleName: role) } catch ExampleError.roleNotFound { // The role wasn't found, so create it and attach the needed // policy. iamRole = try await createRoleForLambda(iamClient: iamClient, roleName: role) do { _ = try await iamClient.attachRolePolicy( input: AttachRolePolicyInput(policyArn: policyARN, roleName: role) ) } catch { throw ExampleError.policyError } } // Give the policy time to attach to the role. sleep(5) // Look to see if the function already exists. If it does, throw an // error. if await doesLambdaFunctionExist(lambdaClient: lambdaClient, name: basicsFunctionName) { throw ExampleError.functionAlreadyExists } // Create, then invoke, the "increment" version of the calculator // function. print("Creating the increment Lambda function...") if try await createFunction(lambdaClient: lambdaClient, name: basicsFunctionName, roleArn: iamRole.arn, path: incpath) { for number in 0...4 { do { let answer = try await invokeIncrement(lambdaClient: lambdaClient, number: number) print("Increment \(number) = \(answer)") } catch { print("Error incrementing \(number): ", error.localizedDescription) } } } // Change it to a basic arithmetic calculator. Then invoke it a few // times. print("\nReplacing the Lambda function with a calculator...") if try await updateFunctionCode(lambdaClient: lambdaClient, name: "lambda-basics-function", path: calcpath) { for x in [6, 10] { for y in [2, 4] { for action in ["plus", "minus", "times", "divided-by"] { do { let answer = try await invokeCalculator(lambdaClient: lambdaClient, action: action, x: x, y: y) print("\(x) \(action) \(y) = \(answer)") } catch { print("Error calculating \(x) \(action) \(y): ", error.localizedDescription) } } } } } // List all lambda functions. let functionNames = try await getFunctionNames(lambdaClient: lambdaClient) if functionNames.count > 0 { print("\nAWS Lambda functions available on your account:") for name in functionNames { print(" \(name)") } } // Delete the lambda function. print("Deleting lambda function...") do { _ = try await lambdaClient.deleteFunction( input: DeleteFunctionInput( functionName: "lambda-basics-function" ) ) } catch { print("Error: Unable to delete the function.") } // Detach the role from the policy, then delete the role. print("Deleting the AWS IAM role...") do { _ = try await iamClient.detachRolePolicy( input: DetachRolePolicyInput( policyArn: policyARN, roleName: role ) ) _ = try await iamClient.deleteRole( input: DeleteRoleInput( roleName: role ) ) } catch { throw ExampleError.deleteRoleError } } } // -MARK: - Entry point /// The program's asynchronous entry point. @main struct Main { static func main() async { let args = Array(CommandLine.arguments.dropFirst()) do { let command = try ExampleCommand.parse(args) try await command.basics() } catch { ExampleCommand.exit(withError: error) } } } /// Errors thrown by the example's functions. enum ExampleError: Error { /// An AWS Lambda function with the specified name already exists. case functionAlreadyExists /// The specified role doesn't exist. case roleNotFound /// Unable to create the role. case roleCreateError /// Unable to delete the role. case deleteRoleError /// Unable to attach a policy to the role. case policyError /// Unable to get the executable directory. case executableNotFound /// An error occurred creating a lambda function. case createLambdaError /// An error occurred invoking the lambda function. case invokeError /// No answer received from the invocation. case noAnswerReceived /// Unable to list the AWS Lambda functions. case listFunctionsError /// Unable to update the AWS Lambda function. case updateFunctionError /// Unable to load the AWS Lambda function's Zip file. case zipFileReadError var errorDescription: String? { switch self { case .functionAlreadyExists: return "An AWS Lambda function with that name already exists." case .roleNotFound: return "The specified role doesn't exist." case .deleteRoleError: return "Unable to delete the AWS IAM role." case .roleCreateError: return "Unable to create the specified role." case .policyError: return "An error occurred attaching the policy to the role." case .executableNotFound: return "Unable to find the executable program directory." case .createLambdaError: return "An error occurred creating a lambda function." case .invokeError: return "An error occurred invoking a lambda function." case .noAnswerReceived: return "No answer received from the lambda function." case .listFunctionsError: return "Unable to list the AWS Lambda functions." case .updateFunctionError: return "Unable to update the AWS lambda function." case .zipFileReadError: return "Unable to read the AWS Lambda function." } } }

Actions

The following code example shows how to use CreateFunction.

SDK for Swift
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import AWSClientRuntime import AWSLambda import Foundation do { // Read the Zip archive containing the AWS Lambda function. let zipUrl = URL(fileURLWithPath: path) let zipData = try Data(contentsOf: zipUrl) // Create the AWS Lambda function that runs the specified code, // using the name given on the command line. The Lambda function // will run using the Amazon Linux 2 runtime. _ = try await lambdaClient.createFunction( input: CreateFunctionInput( code: LambdaClientTypes.FunctionCode(zipFile: zipData), functionName: name, handler: "handle", role: roleArn, runtime: .providedal2 ) ) } catch { return false }
  • For API details, see CreateFunction in AWS SDK for Swift API reference.

The following code example shows how to use CreateFunction.

SDK for Swift
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import AWSClientRuntime import AWSLambda import Foundation do { // Read the Zip archive containing the AWS Lambda function. let zipUrl = URL(fileURLWithPath: path) let zipData = try Data(contentsOf: zipUrl) // Create the AWS Lambda function that runs the specified code, // using the name given on the command line. The Lambda function // will run using the Amazon Linux 2 runtime. _ = try await lambdaClient.createFunction( input: CreateFunctionInput( code: LambdaClientTypes.FunctionCode(zipFile: zipData), functionName: name, handler: "handle", role: roleArn, runtime: .providedal2 ) ) } catch { return false }
  • For API details, see CreateFunction in AWS SDK for Swift API reference.

The following code example shows how to use GetFunction.

SDK for Swift
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import AWSClientRuntime import AWSLambda import Foundation /// Detect whether or not the AWS Lambda function with the specified name /// exists, by requesting its function information. /// /// - Parameters: /// - lambdaClient: The `LambdaClient` to use. /// - name: The name of the AWS Lambda function to find. /// /// - Returns: `true` if the Lambda function exists. Otherwise `false`. func doesLambdaFunctionExist(lambdaClient: LambdaClient, name: String) async -> Bool { do { _ = try await lambdaClient.getFunction( input: GetFunctionInput(functionName: name) ) } catch { return false } return true }
  • For API details, see GetFunction in AWS SDK for Swift API reference.

The following code example shows how to use GetFunction.

SDK for Swift
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import AWSClientRuntime import AWSLambda import Foundation /// Detect whether or not the AWS Lambda function with the specified name /// exists, by requesting its function information. /// /// - Parameters: /// - lambdaClient: The `LambdaClient` to use. /// - name: The name of the AWS Lambda function to find. /// /// - Returns: `true` if the Lambda function exists. Otherwise `false`. func doesLambdaFunctionExist(lambdaClient: LambdaClient, name: String) async -> Bool { do { _ = try await lambdaClient.getFunction( input: GetFunctionInput(functionName: name) ) } catch { return false } return true }
  • For API details, see GetFunction in AWS SDK for Swift API reference.

The following code example shows how to use Invoke.

SDK for Swift
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import AWSClientRuntime import AWSLambda import Foundation /// Invoke the Lambda function to increment a value. /// /// - Parameters: /// - lambdaClient: The `IAMClient` to use. /// - number: The number to increment. /// /// - Throws: `ExampleError.noAnswerReceived`, `ExampleError.invokeError` /// /// - Returns: An integer number containing the incremented value. func invokeIncrement(lambdaClient: LambdaClient, number: Int) async throws -> Int { do { let incRequest = IncrementRequest(action: "increment", number: number) let incData = try! JSONEncoder().encode(incRequest) // Invoke the lambda function. let invokeOutput = try await lambdaClient.invoke( input: InvokeInput( functionName: "lambda-basics-function", payload: incData ) ) let response = try! JSONDecoder().decode(Response.self, from:invokeOutput.payload!) guard let answer = response.answer else { throw ExampleError.noAnswerReceived } return answer } catch { throw ExampleError.invokeError } }
  • For API details, see Invoke in AWS SDK for Swift API reference.

The following code example shows how to use Invoke.

SDK for Swift
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import AWSClientRuntime import AWSLambda import Foundation /// Invoke the Lambda function to increment a value. /// /// - Parameters: /// - lambdaClient: The `IAMClient` to use. /// - number: The number to increment. /// /// - Throws: `ExampleError.noAnswerReceived`, `ExampleError.invokeError` /// /// - Returns: An integer number containing the incremented value. func invokeIncrement(lambdaClient: LambdaClient, number: Int) async throws -> Int { do { let incRequest = IncrementRequest(action: "increment", number: number) let incData = try! JSONEncoder().encode(incRequest) // Invoke the lambda function. let invokeOutput = try await lambdaClient.invoke( input: InvokeInput( functionName: "lambda-basics-function", payload: incData ) ) let response = try! JSONDecoder().decode(Response.self, from:invokeOutput.payload!) guard let answer = response.answer else { throw ExampleError.noAnswerReceived } return answer } catch { throw ExampleError.invokeError } }
  • For API details, see Invoke in AWS SDK for Swift API reference.

The following code example shows how to use ListFunctions.

SDK for Swift
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import AWSClientRuntime import AWSLambda import Foundation /// Returns an array containing the names of all AWS Lambda functions /// available to the user. /// /// - Parameter lambdaClient: The `IAMClient` to use. /// /// - Throws: `ExampleError.listFunctionsError` /// /// - Returns: An array of lambda function name strings. func getFunctionNames(lambdaClient: LambdaClient) async throws -> [String] { let pages = lambdaClient.listFunctionsPaginated( input: ListFunctionsInput() ) var functionNames: [String] = [] for try await page in pages { guard let functions = page.functions else { throw ExampleError.listFunctionsError } for function in functions { functionNames.append(function.functionName ?? "<unknown>") } } return functionNames }
  • For API details, see ListFunctions in AWS SDK for Swift API reference.

The following code example shows how to use ListFunctions.

SDK for Swift
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import AWSClientRuntime import AWSLambda import Foundation /// Returns an array containing the names of all AWS Lambda functions /// available to the user. /// /// - Parameter lambdaClient: The `IAMClient` to use. /// /// - Throws: `ExampleError.listFunctionsError` /// /// - Returns: An array of lambda function name strings. func getFunctionNames(lambdaClient: LambdaClient) async throws -> [String] { let pages = lambdaClient.listFunctionsPaginated( input: ListFunctionsInput() ) var functionNames: [String] = [] for try await page in pages { guard let functions = page.functions else { throw ExampleError.listFunctionsError } for function in functions { functionNames.append(function.functionName ?? "<unknown>") } } return functionNames }
  • For API details, see ListFunctions in AWS SDK for Swift API reference.

The following code example shows how to use UpdateFunctionCode.

SDK for Swift
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import AWSClientRuntime import AWSLambda import Foundation let zipUrl = URL(fileURLWithPath: path) let zipData: Data // Read the function's Zip file. do { zipData = try Data(contentsOf: zipUrl) } catch { throw ExampleError.zipFileReadError } // Update the function's code and wait for the updated version to be // ready for use. do { _ = try await lambdaClient.updateFunctionCode( input: UpdateFunctionCodeInput( functionName: name, zipFile: zipData ) ) } catch { return false }

The following code example shows how to use UpdateFunctionCode.

SDK for Swift
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

import AWSClientRuntime import AWSLambda import Foundation let zipUrl = URL(fileURLWithPath: path) let zipData: Data // Read the function's Zip file. do { zipData = try Data(contentsOf: zipUrl) } catch { throw ExampleError.zipFileReadError } // Update the function's code and wait for the updated version to be // ready for use. do { _ = try await lambdaClient.updateFunctionCode( input: UpdateFunctionCodeInput( functionName: name, zipFile: zipData ) ) } catch { return false }

下一個主題:

Amazon S3

上一個主題:

IAM
隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。