diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 640f6dc2..8146bb4f 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -35,7 +35,7 @@ jobs: # We pass the list of examples here, but we can't pass an array as argument # Instead, we pass a String with a valid JSON array. # The workaround is mentioned here https://github.com/orgs/community/discussions/11692 - examples: "[ 'HelloWorld', 'APIGateway','S3_AWSSDK', 'S3_Soto', 'Streaming', 'BackgroundTasks' ]" + examples: "[ 'APIGateway', 'BackgroundTasks', 'HelloJSON', 'HelloWorld', 'S3_AWSSDK', 'S3_Soto', 'Streaming' ]" archive_plugin_enabled: true diff --git a/.gitignore b/.gitignore index a5baf2c5..f7a26a78 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .DS_Store *.build +*.index-build /.xcodeproj *.pem .podspecs @@ -10,3 +11,4 @@ Package.resolved .serverless .vscode Makefile +.devcontainer \ No newline at end of file diff --git a/Examples/HelloJSON/.gitignore b/Examples/HelloJSON/.gitignore new file mode 100644 index 00000000..e41d0be5 --- /dev/null +++ b/Examples/HelloJSON/.gitignore @@ -0,0 +1,4 @@ +response.json +samconfig.toml +template.yaml +Makefile diff --git a/Examples/HelloJSON/Package.swift b/Examples/HelloJSON/Package.swift new file mode 100644 index 00000000..506f0678 --- /dev/null +++ b/Examples/HelloJSON/Package.swift @@ -0,0 +1,59 @@ +// swift-tools-version:6.0 + +import PackageDescription + +// needed for CI to test the local version of the library +import struct Foundation.URL + +#if os(macOS) +let platforms: [PackageDescription.SupportedPlatform]? = [.macOS(.v15)] +#else +let platforms: [PackageDescription.SupportedPlatform]? = nil +#endif + +let package = Package( + name: "swift-aws-lambda-runtime-example", + platforms: platforms, + products: [ + .executable(name: "HelloJSON", targets: ["HelloJSON"]) + ], + dependencies: [ + // during CI, the dependency on local version of swift-aws-lambda-runtime is added dynamically below + .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", branch: "main") + ], + targets: [ + .executableTarget( + name: "HelloJSON", + dependencies: [ + .product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime") + ] + ) + ] +) + +if let localDepsPath = Context.environment["LAMBDA_USE_LOCAL_DEPS"], + localDepsPath != "", + let v = try? URL(fileURLWithPath: localDepsPath).resourceValues(forKeys: [.isDirectoryKey]), + v.isDirectory == true +{ + // when we use the local runtime as deps, let's remove the dependency added above + let indexToRemove = package.dependencies.firstIndex { dependency in + if case .sourceControl( + name: _, + location: "https://github.com/swift-server/swift-aws-lambda-runtime.git", + requirement: _ + ) = dependency.kind { + return true + } + return false + } + if let indexToRemove { + package.dependencies.remove(at: indexToRemove) + } + + // then we add the dependency on LAMBDA_USE_LOCAL_DEPS' path (typically ../..) + print("[INFO] Compiling against swift-aws-lambda-runtime located at \(localDepsPath)") + package.dependencies += [ + .package(name: "swift-aws-lambda-runtime", path: localDepsPath) + ] +} diff --git a/Examples/HelloJSON/README.md b/Examples/HelloJSON/README.md new file mode 100644 index 00000000..ef0569df --- /dev/null +++ b/Examples/HelloJSON/README.md @@ -0,0 +1,80 @@ +# Hello JSON + +This is a simple example of an AWS Lambda function that takes a JSON structure as input parameter and returns a JSON structure as response. + +The runtime takes care of decoding the input and encoding the output. + +## Code + +The code defines a `HelloRequest` and `HelloResponse` data structure to represent the input and outpout payload. These structures are typically shared with a client project, such as an iOS application. + +The code creates a `LambdaRuntime` struct. In it's simplest form, the initializer takes a function as argument. The function is the handler that will be invoked when an event triggers the Lambda function. + +The handler is `(event: HelloRequest, context: LambdaContext)`. The function takes two arguments: +- the event argument is a `HelloRequest`. It is the parameter passed when invoking the function. +- the context argument is a `Lambda Context`. It is a description of the runtime context. + +The function return value will be encoded to an `HelloResponse` as your Lambda function response. + +## Build & Package + +To build & archive the package, type the following commands. + +```bash +swift package archive --allow-network-connections docker +``` + +If there is no error, there is a ZIP file ready to deploy. +The ZIP file is located at `.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/HelloJSON/HelloJSON.zip` + +## Deploy + +Here is how to deploy using the `aws` command line. + +```bash +# Replace with your AWS Account ID +AWS_ACCOUNT_ID=012345678901 + +aws lambda create-function \ +--function-name HelloJSON \ +--zip-file fileb://.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/HelloJSON/HelloJSON.zip \ +--runtime provided.al2 \ +--handler provided \ +--architectures arm64 \ +--role arn:aws:iam::${AWS_ACCOUNT_ID}:role/lambda_basic_execution +``` + +The `--architectures` flag is only required when you build the binary on an Apple Silicon machine (Apple M1 or more recent). It defaults to `x64`. + +Be sure to define the `AWS_ACCOUNT_ID` environment variable with your actual AWS account ID (for example: 012345678901). + +## Invoke your Lambda function + +To invoke the Lambda function, use this `aws` command line. + +```bash +aws lambda invoke \ +--function-name HelloJSON \ +--payload $(echo '{ "name" : "Seb", "age" : 50 }' | base64) \ +out.txt && cat out.txt && rm out.txt +``` + +Note that the payload is expected to be a valid JSON string. + +This should output the following result. + +``` +{ + "StatusCode": 200, + "ExecutedVersion": "$LATEST" +} +{"greetings":"Hello Seb. You look younger than your age."} +``` + +## Undeploy + +When done testing, you can delete the Lambda function with this command. + +```bash +aws lambda delete-function --function-name HelloJSON +``` \ No newline at end of file diff --git a/Examples/HelloJSON/Sources/main.swift b/Examples/HelloJSON/Sources/main.swift new file mode 100644 index 00000000..547f8a13 --- /dev/null +++ b/Examples/HelloJSON/Sources/main.swift @@ -0,0 +1,40 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftAWSLambdaRuntime open source project +// +// Copyright (c) 2024 Apple Inc. and the SwiftAWSLambdaRuntime project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +import AWSLambdaRuntime + +// in this example we are receiving and responding with a JSON structure + +// the data structure to represent the input parameter +struct HelloRequest: Decodable { + let name: String + let age: Int +} + +// the data structure to represent the output response +struct HelloResponse: Encodable { + let greetings: String +} + +// the Lambda runtime +let runtime = LambdaRuntime { + (event: HelloRequest, context: LambdaContext) in + + HelloResponse( + greetings: "Hello \(event.name). You look \(event.age > 30 ? "younger" : "older") than your age." + ) +} + +// start the loop +try await runtime.run() diff --git a/Examples/README.md b/Examples/README.md index 4e597305..06c75a16 100644 --- a/Examples/README.md +++ b/Examples/README.md @@ -18,6 +18,8 @@ This directory contains example code for Lambda functions. - **[BackgroundTasks](BackgroundTasks/README.md)**: a Lambda function that continues to run background tasks after having sent the response (requires [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)). +- **[HelloJSON](HelloJSON/README.md)**: a Lambda function that accepts a JSON as input parameter and responds with a JSON output (requires [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)). + - **[HelloWorld](HelloWorld/README.md)**: a simple Lambda function (requires [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)). - **[S3_AWSSDK](S3_AWSSDK/README.md)**: a Lambda function that uses the [AWS SDK for Swift](https://docs.aws.amazon.com/sdk-for-swift/latest/developer-guide/getting-started.html) to invoke an [Amazon S3](https://docs.aws.amazon.com/AmazonS3/latest/userguide/Welcome.html) API (requires [AWS SAM](https://aws.amazon.com/serverless/sam/)). diff --git a/Plugins/AWSLambdaPackager/Plugin.swift b/Plugins/AWSLambdaPackager/Plugin.swift index 3be4b1ba..a8693945 100644 --- a/Plugins/AWSLambdaPackager/Plugin.swift +++ b/Plugins/AWSLambdaPackager/Plugin.swift @@ -74,7 +74,7 @@ struct AWSLambdaPackager: CommandPlugin { "\(archives.count > 0 ? archives.count.description : "no") archive\(archives.count != 1 ? "s" : "") created" ) for (product, archivePath) in archives { - print(" * \(product.name) at \(archivePath)") + print(" * \(product.name) at \(archivePath.path())") } } diff --git a/Plugins/AWSLambdaPackager/PluginUtils.swift b/Plugins/AWSLambdaPackager/PluginUtils.swift index 52d1b2be..f4e8cb02 100644 --- a/Plugins/AWSLambdaPackager/PluginUtils.swift +++ b/Plugins/AWSLambdaPackager/PluginUtils.swift @@ -28,6 +28,9 @@ struct Utils { ) throws -> String { if logLevel >= .debug { print("\(executable.path()) \(arguments.joined(separator: " "))") + if let customWorkingDirectory { + print("Working directory: \(customWorkingDirectory.path())") + } } let fd = dup(1) @@ -85,8 +88,8 @@ struct Utils { process.standardError = pipe process.executableURL = executable process.arguments = arguments - if let workingDirectory = customWorkingDirectory { - process.currentDirectoryURL = URL(fileURLWithPath: workingDirectory.path()) + if let customWorkingDirectory { + process.currentDirectoryURL = URL(fileURLWithPath: customWorkingDirectory.path()) } process.terminationHandler = { _ in outputQueue.async { diff --git a/Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift b/Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift index dc0165d4..9eed4389 100644 --- a/Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift +++ b/Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift @@ -12,6 +12,8 @@ // //===----------------------------------------------------------------------===// +// commented out as long as we have a fix for Swift 6 language mode CI + #if DEBUG import Dispatch import Logging @@ -20,7 +22,7 @@ import NIOCore import NIOHTTP1 import NIOPosix -// This functionality is designed for local testing hence beind a #if DEBUG flag. +// This functionality is designed for local testing hence being a #if DEBUG flag. // For example: // // try Lambda.withLocalServer { @@ -32,16 +34,18 @@ extension Lambda { /// Execute code in the context of a mock Lambda server. /// /// - parameters: - /// - invocationEndpoint: The endpoint to post events to. + /// - invocationEndpoint: The endpoint to post events to. /// - body: Code to run within the context of the mock server. Typically this would be a Lambda.run function call. /// /// - note: This API is designed strictly for local testing and is behind a DEBUG flag - static func withLocalServer(invocationEndpoint: String? = nil, _ body: @escaping () -> Value) throws -> Value - { + static func withLocalServer( + invocationEndpoint: String? = nil, + _ body: @escaping () async throws -> Value + ) async throws -> Value { let server = LocalLambda.Server(invocationEndpoint: invocationEndpoint) - try server.start().wait() + try await server.start().get() defer { try! server.stop() } - return body() + return try await body() } } @@ -61,7 +65,7 @@ private enum LocalLambda { self.logger = logger self.group = MultiThreadedEventLoopGroup(numberOfThreads: 1) self.host = "127.0.0.1" - self.port = 0 + self.port = 7000 self.invocationEndpoint = invocationEndpoint ?? "/invoke" } @@ -185,7 +189,7 @@ private enum LocalLambda { } Self.invocationState = .waitingForInvocation(promise) case .some(let invocation): - // if there is a task pending, we can immediatly respond with it. + // if there is a task pending, we can immediately respond with it. Self.invocationState = .waitingForLambdaResponse(invocation) self.writeResponse(context: context, response: invocation.makeResponse()) } diff --git a/Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift b/Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift index 726c39a4..86274836 100644 --- a/Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift +++ b/Sources/AWSLambdaRuntimeCore/LambdaRuntime.swift @@ -43,14 +43,6 @@ public final class LambdaRuntime: @unchecked Sendable where Handler: St } public func run() async throws { - guard let runtimeEndpoint = Lambda.env("AWS_LAMBDA_RUNTIME_API") else { - throw LambdaRuntimeError(code: .missingLambdaRuntimeAPIEnvironmentVariable) - } - - let ipAndPort = runtimeEndpoint.split(separator: ":", maxSplits: 1) - let ip = String(ipAndPort[0]) - guard let port = Int(ipAndPort[1]) else { throw LambdaRuntimeError(code: .invalidPort) } - let handler = self.handlerMutex.withLockedValue { handler in let result = handler handler = nil @@ -61,16 +53,45 @@ public final class LambdaRuntime: @unchecked Sendable where Handler: St throw LambdaRuntimeError(code: .runtimeCanOnlyBeStartedOnce) } - try await LambdaRuntimeClient.withRuntimeClient( - configuration: .init(ip: ip, port: port), - eventLoop: self.eventLoop, - logger: self.logger - ) { runtimeClient in - try await Lambda.runLoop( - runtimeClient: runtimeClient, - handler: handler, + // are we running inside an AWS Lambda runtime environment ? + // AWS_LAMBDA_RUNTIME_API is set when running on Lambda + // https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html + if let runtimeEndpoint = Lambda.env("AWS_LAMBDA_RUNTIME_API") { + + let ipAndPort = runtimeEndpoint.split(separator: ":", maxSplits: 1) + let ip = String(ipAndPort[0]) + guard let port = Int(ipAndPort[1]) else { throw LambdaRuntimeError(code: .invalidPort) } + + try await LambdaRuntimeClient.withRuntimeClient( + configuration: .init(ip: ip, port: port), + eventLoop: self.eventLoop, logger: self.logger - ) + ) { runtimeClient in + try await Lambda.runLoop( + runtimeClient: runtimeClient, + handler: handler, + logger: self.logger + ) + } + + } else { + + // we're not running on Lambda, let's start a local server for testing + try await Lambda.withLocalServer(invocationEndpoint: Lambda.env("LOCAL_LAMBDA_SERVER_INVOCATION_ENDPOINT")) + { + + try await LambdaRuntimeClient.withRuntimeClient( + configuration: .init(ip: "127.0.0.1", port: 7000), + eventLoop: self.eventLoop, + logger: self.logger + ) { runtimeClient in + try await Lambda.runLoop( + runtimeClient: runtimeClient, + handler: handler, + logger: self.logger + ) + } + } } } } diff --git a/readme.md b/readme.md index 24bd135c..973e6b8e 100644 --- a/readme.md +++ b/readme.md @@ -4,6 +4,18 @@ > [!WARNING] > The Swift AWS Runtime v2 is work in progress. We will add more documentation and code examples over time. +## The Swift AWS Lambda Runtime + +Many modern systems have client components like iOS, macOS or watchOS applications as well as server components that those clients interact with. Serverless functions are often the easiest and most efficient way for client application developers to extend their applications into the cloud. + +Serverless functions are increasingly becoming a popular choice for running event-driven or otherwise ad-hoc compute tasks in the cloud. They power mission critical microservices and data intensive workloads. In many cases, serverless functions allow developers to more easily scale and control compute costs given their on-demand nature. + +When using serverless functions, attention must be given to resource utilization as it directly impacts the costs of the system. This is where Swift shines! With its low memory footprint, deterministic performance, and quick start time, Swift is a fantastic match for the serverless functions architecture. + +Combine this with Swift's developer friendliness, expressiveness, and emphasis on safety, and we have a solution that is great for developers at all skill levels, scalable, and cost effective. + +Swift AWS Lambda Runtime was designed to make building Lambda functions in Swift simple and safe. The library is an implementation of the [AWS Lambda Runtime API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html) and uses an embedded asynchronous HTTP Client based on [SwiftNIO](http://github.com/apple/swift-nio) that is fine-tuned for performance in the AWS Runtime context. The library provides a multi-tier API that allows building a range of Lambda functions: From quick and simple closures to complex, performance-sensitive event handlers. + ## Pre-requisites - Ensure you have the Swift 6.x toolchain installed. You can [install Swift toolchains](https://www.swift.org/install/macos/) from Swift.org @@ -16,7 +28,11 @@ - Some examples are using [AWS SAM](https://aws.amazon.com/serverless/sam/). Install the [SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html) before deploying these examples. -## TL;DR +## Getting started + +To get started, read [the Swift AWS Lambda runtime v1 tutorial](https://swiftpackageindex.com/swift-server/swift-aws-lambda-runtime/1.0.0-alpha.3/tutorials/table-of-content). It provides developers with detailed step-by-step instructions to develop, build, and deploy a Lambda function. + +Or, if you're impatient to start with runtime v2, try these six steps: 1. Create a new Swift executable project @@ -128,53 +144,42 @@ This should print "dlroW olleH" ``` -## Tutorial - -[The Swift AWS Lambda Runtime docc tutorial](https://swiftpackageindex.com/swift-server/swift-aws-lambda-runtime/1.0.0-alpha.3/tutorials/table-of-content) provides developers with detailed step-by-step instructions to develop, build, and deploy a Lambda function. +## Developing your Swift Lambda functions -## Swift AWS Lambda Runtime - -Many modern systems have client components like iOS, macOS or watchOS applications as well as server components that those clients interact with. Serverless functions are often the easiest and most efficient way for client application developers to extend their applications into the cloud. - -Serverless functions are increasingly becoming a popular choice for running event-driven or otherwise ad-hoc compute tasks in the cloud. They power mission critical microservices and data intensive workloads. In many cases, serverless functions allow developers to more easily scale and control compute costs given their on-demand nature. - -When using serverless functions, attention must be given to resource utilization as it directly impacts the costs of the system. This is where Swift shines! With its low memory footprint, deterministic performance, and quick start time, Swift is a fantastic match for the serverless functions architecture. - -Combine this with Swift's developer friendliness, expressiveness, and emphasis on safety, and we have a solution that is great for developers at all skill levels, scalable, and cost effective. - -Swift AWS Lambda Runtime was designed to make building Lambda functions in Swift simple and safe. The library is an implementation of the [AWS Lambda Runtime API](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html) and uses an embedded asynchronous HTTP Client based on [SwiftNIO](http://github.com/apple/swift-nio) that is fine-tuned for performance in the AWS Runtime context. The library provides a multi-tier API that allows building a range of Lambda functions: From quick and simple closures to complex, performance-sensitive event handlers. - -## Design Principles - -The [design document](Sources/AWSLambdaRuntimeCore/Documentation.docc/Proposals/0001-v2-api.md) details the v2 API proposal for the swift-aws-lambda-runtime library, which aims to enhance the developer experience for building serverless functions in Swift. - -The proposal has been reviewed and [incorporated feedback from the community](https://forums.swift.org/t/aws-lambda-v2-api-proposal/73819). The full v2 API design document is available [in this repository](Sources/AWSLambdaRuntimeCore/Documentation.docc/Proposals/0001-v2-api.md). - -### Key Design Principles - -The v2 API prioritizes the following principles: - -- Readability and Maintainability: Extensive use of `async`/`await` improves code clarity and simplifies maintenance. - -- Developer Control: Developers own the `main()` function and have the flexibility to inject dependencies into the `LambdaRuntime`. This allows you to manage service lifecycles efficiently using [Swift Service Lifecycle](https://github.com/swift-server/swift-service-lifecycle) for structured concurrency. +### Receive and respond with JSON objects -- Simplified Codable Support: The `LambdaCodableAdapter` struct eliminates the need for verbose boilerplate code when encoding and decoding events and responses. +Typically, your Lambda functions will receive an input parameter expressed as JSON and will respond with another JSON. The Swift AWS Lambda runtime automatically takes care of encoding and decoding JSON objects when your Lambda function handler accepts `Decodable` and returns `Encodable` conforming `struct`. -### New Capabilities +Here is an example of a minimal function that accepts a JSON object as input and responds with another JSON object. -The v2 API introduces two new features: +```swift +import AWSLambdaRuntime -[Response Streaming](https://aws.amazon.com/blogs/compute/introducing-aws-lambda-response-streaming/]): This functionality is ideal for handling large responses that need to be sent incrementally.   +// the data structure to represent the input parameter +struct HelloRequest: Decodable { + let name: String + let age: Int +} -[Background Work](https://aws.amazon.com/blogs/compute/running-code-after-returning-a-response-from-an-aws-lambda-function/): Schedule tasks to run after returning a response to the AWS Lambda control plane. +// the data structure to represent the output response +struct HelloResponse: Encodable { + let greetings: String +} -These new capabilities provide greater flexibility and control when building serverless functions in Swift with the swift-aws-lambda-runtime library. +// the Lambda runtime +let runtime = LambdaRuntime { + (event: HelloRequest, context: LambdaContext) in -## AWSLambdaRuntime API + HelloResponse( + greetings: "Hello \(event.name). You look \(event.age > 30 ? "younger" : "older") than your age." + ) +} -### Receive and respond with JSON objects +// start the loop +try await runtime.run() +``` -tbd + link to docc +You can learn how to deploy and invoke this function in [the Hello JSON example README file](Examples/HelloJSON/README.md). ### Lambda Streaming Response @@ -211,7 +216,30 @@ let runtime = LambdaRuntime.init(handler: SendNumbersWithPause()) try await runtime.run() ``` -You can learn how to deploy and invoke this function in [the example README file](Examples/Streaming/README.md). +You can learn how to deploy and invoke this function in [the streaming example README file](Examples/Streaming/README.md). + +### Integration with AWS Services + + Most Lambda functions are triggered by events originating in other AWS services such as `Amazon SNS`, `Amazon SQS` or `AWS APIGateway`. + + The [Swift AWS Lambda Events](http://github.com/swift-server/swift-aws-lambda-events) package includes an `AWSLambdaEvents` module that provides implementations for most common AWS event types further simplifying writing Lambda functions. + + Here is an example Lambda function invoked when the AWS APIGateway receives an HTTP request. + + ```swift +import AWSLambdaEvents +import AWSLambdaRuntime + +let runtime = LambdaRuntime { + (event: APIGatewayV2Request, context: LambdaContext) -> APIGatewayV2Response in + + APIGatewayV2Response(statusCode: .ok, body: "Hello World!") +} + +try await runtime.run() +``` + + You can learn how to deploy and invoke this function in [the API Gateway example README file](Examples/APIGateway/README.md). ### Integration with Swift Service LifeCycle @@ -264,4 +292,79 @@ let runtime = LambdaRuntime.init(handler: adapter) try await runtime.run() ``` -You can learn how to deploy and invoke this function in [the example README file](Examples/BackgroundTasks/README.md). \ No newline at end of file +You can learn how to deploy and invoke this function in [the background tasks example README file](Examples/BackgroundTasks/README.md). + +## Testing Locally + +Before deploying your code to AWS Lambda, you can test it locally by running the executable target on your local machine. It will look like this on CLI: + +```sh +swift run +``` + +When not running inside a Lambda execution environment, it starts a local HTTP server listening on port 7000. You can invoke your local Lambda function by sending an HTTP POST request to `http://127.0.0.1:7000/invoke`. + +The request must include the JSON payload expected as an `event` by your function. You can create a text file with the JSON payload documented by AWS or captured from a trace. In this example, we used [the APIGatewayv2 JSON payload from the documentation](https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html#apigateway-example-event), saved as `events/create-session.json` text file. + +Then we use curl to invoke the local endpoint with the test JSON payload. + +```sh +curl -v --header "Content-Type:\ application/json" --data @events/create-session.json http://127.0.0.1:7000/invoke +* Trying 127.0.0.1:7000... +* Connected to 127.0.0.1 (127.0.0.1) port 7000 +> POST /invoke HTTP/1.1 +> Host: 127.0.0.1:7000 +> User-Agent: curl/8.4.0 +> Accept: */* +> Content-Type:\ application/json +> Content-Length: 1160 +> +< HTTP/1.1 200 OK +< content-length: 247 +< +* Connection #0 to host 127.0.0.1 left intact +{"statusCode":200,"isBase64Encoded":false,"body":"...","headers":{"Access-Control-Allow-Origin":"*","Content-Type":"application\/json; charset=utf-8","Access-Control-Allow-Headers":"*"}} +``` +### Modifying the local endpoint + +By default, when using the local Lambda server, it listens on the `/invoke` endpoint. + +Some testing tools, such as the [AWS Lambda runtime interface emulator](https://docs.aws.amazon.com/lambda/latest/dg/images-test.html), require a different endpoint. In that case, you can use the `LOCAL_LAMBDA_SERVER_INVOCATION_ENDPOINT` environment variable to force the runtime to listen on a different endpoint. + +Example: + +```sh +LOCAL_LAMBDA_SERVER_INVOCATION_ENDPOINT=/2015-03-31/functions/function/invocations swift run +``` + +## Deploying your Swift Lambda functions + + +TODO + + +## Swift AWS Lambda Runtime - Design Principles + +The [design document](Sources/AWSLambdaRuntimeCore/Documentation.docc/Proposals/0001-v2-api.md) details the v2 API proposal for the swift-aws-lambda-runtime library, which aims to enhance the developer experience for building serverless functions in Swift. + +The proposal has been reviewed and [incorporated feedback from the community](https://forums.swift.org/t/aws-lambda-v2-api-proposal/73819). The full v2 API design document is available [in this repository](Sources/AWSLambdaRuntimeCore/Documentation.docc/Proposals/0001-v2-api.md). + +### Key Design Principles + +The v2 API prioritizes the following principles: + +- Readability and Maintainability: Extensive use of `async`/`await` improves code clarity and simplifies maintenance. + +- Developer Control: Developers own the `main()` function and have the flexibility to inject dependencies into the `LambdaRuntime`. This allows you to manage service lifecycles efficiently using [Swift Service Lifecycle](https://github.com/swift-server/swift-service-lifecycle) for structured concurrency. + +- Simplified Codable Support: The `LambdaCodableAdapter` struct eliminates the need for verbose boilerplate code when encoding and decoding events and responses. + +### New Capabilities + +The v2 API introduces two new features: + +[Response Streaming](https://aws.amazon.com/blogs/compute/introducing-aws-lambda-response-streaming/]): This functionality is ideal for handling large responses that need to be sent incrementally.   + +[Background Work](https://aws.amazon.com/blogs/compute/running-code-after-returning-a-response-from-an-aws-lambda-function/): Schedule tasks to run after returning a response to the AWS Lambda control plane. + +These new capabilities provide greater flexibility and control when building serverless functions in Swift with the swift-aws-lambda-runtime library. \ No newline at end of file