Skip to content

Commit eb9512d

Browse files
authored
[doc] update quicksetup guide for v2 (#446)
Update the quick-setup.md docc file to reflect the Runtime v2 API
1 parent 64b1d31 commit eb9512d

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

Sources/AWSLambdaRuntimeCore/Documentation.docc/quick-setup.md

+34-32
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ import PackageDescription
3131
let package = Package(
3232
name: "YourProjetName",
3333
platforms: [
34-
.macOS(.v12),
34+
.macOS(.v15),
3535
],
3636
products: [
37-
.executable(name: "YourFunctionName", targets: ["YourFunctionName"]),
37+
.executable(name: "MyFirstLambdaFunction", targets: ["MyFirstLambdaFunction"]),
3838
],
3939
dependencies: [
40-
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0-alpha"),
40+
.package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "main"),
4141
],
4242
targets: [
4343
.executableTarget(
44-
name: "YourFunctionName",
44+
name: "MyFirstLambdaFunction",
4545
dependencies: [
4646
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
4747
],
@@ -55,48 +55,50 @@ let package = Package(
5555

5656
> Be sure to rename the `main.swift` file to something else.
5757
58-
Extends the `SimpleLambdaHandler` protocol and implement `handle(_:context)`.
58+
Create an instance of `LambdaRuntime` and pass as a closure a function with this signature: `(_ : Event, context: LambdaContext) async throws -> Output` (as defined in the `LambdaHandler` protocol). `Event` must be `Decodable`.
5959

60+
If your Lambda function is invoked by another AWS service, use the `AWSLambdaEvent` library at [https://github.com/swift-server/swift-aws-lambda-events](https://github.com/swift-server/swift-aws-lambda-events) to represent the input event.
6061

61-
If your Lambda function is invoked by another AWS service, use the `AWSLambdaEvent` library at [https://github.com/swift-server/swift-aws-lambda-events](https://github.com/swift-server/swift-aws-lambda-events)
62+
Finally, call `runtime.run()` to start the event loop.
6263

6364
```swift
64-
import AWSLambdaRuntime
65-
66-
struct Input: Codable {
67-
let number: Double
65+
// the data structure to represent the input parameter
66+
struct HelloRequest: Decodable {
67+
let name: String
68+
let age: Int
6869
}
6970

70-
struct Number: Codable {
71-
let result: Double
71+
// the data structure to represent the output response
72+
struct HelloResponse: Encodable {
73+
let greetings: String
7274
}
7375

74-
@main
75-
struct SquareNumberHandler: SimpleLambdaHandler {
76-
typealias Event = Input
77-
typealias Output = Number
78-
79-
func handle(_ event: Input, context: LambdaContext) async throws -> Number {
80-
Number(result: event.number * event.number)
81-
}
76+
// the Lambda runtime
77+
let runtime = LambdaRuntime {
78+
(event: HelloRequest, context: LambdaContext) in
79+
80+
HelloResponse(
81+
greetings: "Hello \(event.name). You look \(event.age > 30 ? "younger" : "older") than your age."
82+
)
8283
}
84+
85+
// start the loop
86+
try await runtime.run()
8387
```
8488

8589
4. Test your code locally
8690

8791
```sh
88-
export LOCAL_LAMBDA_SERVER_ENABLED=true
89-
90-
swift run
92+
swift run # this starts a local server on port 7000
9193

9294
# Switch to another Terminal tab
9395

9496
curl --header "Content-Type: application/json" \
9597
--request POST \
96-
--data '{"number": 3}' \
98+
--data '{"name": "Seb", "age": 50}' \
9799
http://localhost:7000/invoke
98100

99-
{"result":9}
101+
{"greetings":"Hello Seb. You look younger than your age."}
100102
```
101103

102104
5. Build and package your code for AWS Lambda
@@ -109,24 +111,24 @@ AWS Lambda runtime runs on Amazon Linux. You must compile your code for Amazon L
109111
swift package --allow-network-connections docker archive
110112

111113
-------------------------------------------------------------------------
112-
building "squarenumberlambda" in docker
114+
building "MyFirstLambdaFunction" in docker
113115
-------------------------------------------------------------------------
114116
updating "swift:amazonlinux2" docker image
115117
amazonlinux2: Pulling from library/swift
116118
Digest: sha256:5b0cbe56e35210fa90365ba3a4db9cd2b284a5b74d959fc1ee56a13e9c35b378
117119
Status: Image is up to date for swift:amazonlinux2
118120
docker.io/library/swift:amazonlinux2
119-
building "SquareNumberLambda"
121+
building "MyFirstLambdaFunction"
120122
Building for production...
121123
...
122124
-------------------------------------------------------------------------
123-
archiving "SquareNumberLambda"
125+
archiving "MyFirstLambdaFunction"
124126
-------------------------------------------------------------------------
125127
1 archive created
126-
* SquareNumberLambda at /Users/YourUserName/SquareNumberLambda/.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/SquareNumberLambda/SquareNumberLambda.zip
128+
* MyFirstLambdaFunction at /Users/YourUserName/MyFirstLambdaFunction/.build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyFirstLambdaFunction/MyFirstLambdaFunction.zip
127129

128130

129-
cp .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/SquareNumberLambda/SquareNumberLambda.zip ~/Desktop
131+
cp .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/MyFirstLambdaFunction/MyFirstLambdaFunction.zip ~/Desktop
130132
```
131133

132134
6. Deploy on AWS Lambda
@@ -139,9 +141,9 @@ cp .build/plugins/AWSLambdaPackager/outputs/AWSLambdaPackager/SquareNumberLambda
139141
- Select **Provide your own bootstrap on Amazon Linux 2** as **Runtime**
140142
- Select an **Architecture** that matches the one of the machine where you build the code. Select **x86_64** when you build on Intel-based Macs or **arm64** for Apple Silicon-based Macs.
141143
- Upload the ZIP create during step 5
142-
- Select the **Test** tab, enter a test event such as `{"number":3}` and select **Test**
144+
- Select the **Test** tab, enter a test event such as `{"name": "Seb", "age": 50}` and select **Test**
143145

144-
If the test succeeds, you will see the result: '{"result":9}'
146+
If the test succeeds, you will see the result: `{"greetings":"Hello Seb. You look younger than your age."}`.
145147

146148

147149
Congratulations 🎉! You just wrote, test, build, and deployed a Lambda function written in Swift.

0 commit comments

Comments
 (0)