|
4 | 4 | > [!WARNING]
|
5 | 5 | > The Swift AWS Runtime v2 is work in progress. We will add more documentation and code examples over time.
|
6 | 6 |
|
| 7 | +## The Swift AWS Lambda Runtime |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +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. |
| 14 | + |
| 15 | +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. |
| 16 | + |
| 17 | +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. |
| 18 | + |
7 | 19 | ## Pre-requisites
|
8 | 20 |
|
9 | 21 | - Ensure you have the Swift 6.x toolchain installed. You can [install Swift toolchains](https://www.swift.org/install/macos/) from Swift.org
|
|
16 | 28 |
|
17 | 29 | - 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.
|
18 | 30 |
|
19 |
| -## TL;DR |
| 31 | +## Getting started |
| 32 | + |
| 33 | +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. |
| 34 | + |
| 35 | +Or, if you're impatient to start with runtime v2, try these six steps: |
20 | 36 |
|
21 | 37 | 1. Create a new Swift executable project
|
22 | 38 |
|
@@ -128,53 +144,42 @@ This should print
|
128 | 144 | "dlroW olleH"
|
129 | 145 | ```
|
130 | 146 |
|
131 |
| -## Tutorial |
132 |
| -
|
133 |
| -[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. |
134 |
| -
|
135 |
| -## Swift AWS Lambda Runtime |
136 |
| -
|
137 |
| -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. |
138 |
| -
|
139 |
| -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. |
140 |
| -
|
141 |
| -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. |
| 147 | +## Developing your Swift Lambda functions |
142 | 148 |
|
143 |
| -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. |
144 |
| -
|
145 |
| -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. |
146 |
| -
|
147 |
| -## Design Principles |
148 |
| -
|
149 |
| -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. |
150 |
| -
|
151 |
| -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). |
152 |
| -
|
153 |
| -### Key Design Principles |
154 |
| -
|
155 |
| -The v2 API prioritizes the following principles: |
156 |
| -
|
157 |
| -- Readability and Maintainability: Extensive use of `async`/`await` improves code clarity and simplifies maintenance. |
158 |
| -
|
159 |
| -- 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. |
| 149 | +### Receive and respond with JSON objects |
160 | 150 |
|
161 |
| -- Simplified Codable Support: The `LambdaCodableAdapter` struct eliminates the need for verbose boilerplate code when encoding and decoding events and responses. |
| 151 | +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`. |
162 | 152 |
|
163 |
| -### New Capabilities |
| 153 | +Here is an example of a minimal function that accepts a JSON object as input and responds with another JSON object. |
164 | 154 |
|
165 |
| -The v2 API introduces two new features: |
| 155 | +```swift |
| 156 | +import AWSLambdaRuntime |
166 | 157 |
|
167 |
| -[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. |
| 158 | +// the data structure to represent the input parameter |
| 159 | +struct HelloRequest: Decodable { |
| 160 | + let name: String |
| 161 | + let age: Int |
| 162 | +} |
168 | 163 |
|
169 |
| -[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. |
| 164 | +// the data structure to represent the output response |
| 165 | +struct HelloResponse: Encodable { |
| 166 | + let greetings: String |
| 167 | +} |
170 | 168 |
|
171 |
| -These new capabilities provide greater flexibility and control when building serverless functions in Swift with the swift-aws-lambda-runtime library. |
| 169 | +// the Lambda runtime |
| 170 | +let runtime = LambdaRuntime { |
| 171 | + (event: HelloRequest, context: LambdaContext) in |
172 | 172 |
|
173 |
| -## AWSLambdaRuntime API |
| 173 | + HelloResponse( |
| 174 | + greetings: "Hello \(event.name). You look \(event.age > 30 ? "younger" : "older") than your age." |
| 175 | + ) |
| 176 | +} |
174 | 177 |
|
175 |
| -### Receive and respond with JSON objects |
| 178 | +// start the loop |
| 179 | +try await runtime.run() |
| 180 | +``` |
176 | 181 |
|
177 |
| -tbd + link to docc |
| 182 | +You can learn how to deploy and invoke this function in [the example README file](Examples/HelloJSON/README.md). |
178 | 183 |
|
179 | 184 | ### Lambda Streaming Response
|
180 | 185 |
|
@@ -264,4 +269,36 @@ let runtime = LambdaRuntime.init(handler: adapter)
|
264 | 269 | try await runtime.run()
|
265 | 270 | ```
|
266 | 271 |
|
267 |
| -You can learn how to deploy and invoke this function in [the example README file](Examples/BackgroundTasks/README.md). |
| 272 | +You can learn how to deploy and invoke this function in [the example README file](Examples/BackgroundTasks/README.md). |
| 273 | + |
| 274 | +## Deploying your Swift Lambda functions |
| 275 | + |
| 276 | + |
| 277 | +TODO |
| 278 | + |
| 279 | + |
| 280 | +## Swift AWS Lambda Runtime - Design Principles |
| 281 | + |
| 282 | +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. |
| 283 | + |
| 284 | +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). |
| 285 | + |
| 286 | +### Key Design Principles |
| 287 | + |
| 288 | +The v2 API prioritizes the following principles: |
| 289 | + |
| 290 | +- Readability and Maintainability: Extensive use of `async`/`await` improves code clarity and simplifies maintenance. |
| 291 | + |
| 292 | +- 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. |
| 293 | + |
| 294 | +- Simplified Codable Support: The `LambdaCodableAdapter` struct eliminates the need for verbose boilerplate code when encoding and decoding events and responses. |
| 295 | + |
| 296 | +### New Capabilities |
| 297 | + |
| 298 | +The v2 API introduces two new features: |
| 299 | + |
| 300 | +[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. |
| 301 | + |
| 302 | +[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. |
| 303 | + |
| 304 | +These new capabilities provide greater flexibility and control when building serverless functions in Swift with the swift-aws-lambda-runtime library. |
0 commit comments