You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Be sure to rename the `main.swift` file to something else.
57
57
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`.
59
59
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.
60
61
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.
62
63
63
64
```swift
64
-
importAWSLambdaRuntime
65
-
66
-
structInput: Codable {
67
-
letnumber: Double
65
+
// the data structure to represent the input parameter
66
+
structHelloRequest: Decodable {
67
+
let name: String
68
+
letage: Int
68
69
}
69
70
70
-
structNumber: Codable {
71
-
let result: Double
71
+
// the data structure to represent the output response
72
+
structHelloResponse: Encodable {
73
+
let greetings: String
72
74
}
73
75
74
-
@main
75
-
structSquareNumberHandler: SimpleLambdaHandler {
76
-
typealiasEvent= Input
77
-
typealiasOutput= Number
78
-
79
-
funchandle(_event: Input, context: LambdaContext) asyncthrows-> 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
+
)
82
83
}
84
+
85
+
// start the loop
86
+
tryawait runtime.run()
83
87
```
84
88
85
89
4. Test your code locally
86
90
87
91
```sh
88
-
export LOCAL_LAMBDA_SERVER_ENABLED=true
89
-
90
-
swift run
92
+
swift run # this starts a local server on port 7000
91
93
92
94
# Switch to another Terminal tab
93
95
94
96
curl --header "Content-Type: application/json" \
95
97
--request POST \
96
-
--data '{"number": 3}' \
98
+
--data '{"name": "Seb", "age": 50}' \
97
99
http://localhost:7000/invoke
98
100
99
-
{"result":9}
101
+
{"greetings":"Hello Seb. You look younger than your age."}
100
102
```
101
103
102
104
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
109
111
swift package --allow-network-connections docker archive
*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
- Select **Provide your own bootstrap on Amazon Linux 2** as **Runtime**
140
142
- 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.
141
143
- 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**
143
145
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."}`.
145
147
146
148
147
149
Congratulations 🎉! You just wrote, test, build, and deployed a Lambda function written in Swift.
0 commit comments