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
Copy file name to clipboardexpand all lines: README.md
+30-3
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,28 @@
2
2
3
3
This is a boilerplate application that shows how to run a serverless Swift application on AWS Lambda using AWS SAM. The Lambda operates on Linux running on AWS Graviton2 CPUs with an arm64 architecture. It uses a local proxy to interact with the [Vapor](https://vapor.codes/) framework. This code is part of my article [Serverless Swift With Vapor On AWS Using AWS SAM And Lambda](https://medium.com/@jankammerath/serverless-swift-with-vapor-on-aws-using-aws-sam-and-lambda-3bd89bed5325). If you're interested in running Swift code on AWS Lambda, this may serve as boilerplate.
4
4
5
-
## A note on Vapor
5
+
## How to use it yourself
6
6
7
-
The vapor integration curently uses the `AsyncHTTPClient` and thus the local loopback on the Lambda instance. Vapor is initialized when the Lambda container first starts making the cold start take around 800-900ms on a 128 MB arm64 container running Amazon Linux 2. There is no measurable performance impact on using the loopback adapter within the VaporProxy class that then sends the HTTP request to the vapor app running on port `8585``.
7
+
You can simply work with the code in `App.swift` inside the `src` folder. The configuration is very simple. Once the Lambda starts, the `App()` function is called which you can use to setup Vapor and your routes. Whenver a request is sent to the Lambda function it'll forward it to the Vapor app using the VaporProxy class.
8
+
9
+
```swift
10
+
structHelloWorld: Content {
11
+
let message: String
12
+
}
13
+
14
+
/*
15
+
This App() function is called in the Handler when it first
16
+
initializes. Routes and any configuration should be done here.
17
+
Make sure to retain the App() function or replace it in the Handler.
18
+
*/
19
+
funcApp() {
20
+
// this is the Vapor app instance from the Vapor Proxy
21
+
let app = VaporProxy.shared.app
22
+
app.get { req in
23
+
returnHelloWorld(message: "Hello, world!")
24
+
}
25
+
}
26
+
```
8
27
9
28
## Running locally
10
29
@@ -17,7 +36,15 @@ sam local start-api --template template.yaml
17
36
18
37
Note that the performance characteristics of running this application locally using AWS SAM is entirely different from running it on AWS. SAM will use approx 30-40% more memory than the binary will consume with the actual Lambda on AWS. The invocation of SAM will also take more time than it will when actually running on Lambda with API Gateway.
19
38
20
-
## Sample logs
39
+
## Deploying to AWS
40
+
41
+
You can deploy the application to AWS using either AWS SAM or CloudFormation. With AWS SAM, you can simply use the `sam deploy --guided` command and SAM will guide you through the deployment of the app. If you want to use CloudFormation instead, you need to put the `bootstrap` binary in `bin/` into a zip file and upload it to the S3 bucket that you want to host the code on. The configuration of `AWS::Lambda::Function` is almost identical to `AWS::Serverless::Function`.
42
+
43
+
## Performance
44
+
45
+
The vapor integration curently uses the `AsyncHTTPClient` and thus the local loopback on the Lambda instance. Vapor is initialized when the Lambda container first starts making the cold start take around 800-900ms on a 128 MB arm64 container running Amazon Linux 2. There is no measurable performance impact on using the loopback adapter within the VaporProxy class that then sends the HTTP request to the vapor app running on port `8585``.
46
+
47
+
### Sample logs
21
48
22
49
The logs give an insight on the performance of the approx. 117 MB binary file within the Lambda. The Lambda cold start with the binary is a little less than 1 second. The log also shows how Vapor runs through the entire lifecycle of the Lambda and is reused for subsequent requests to the same Lambda container. The memory consumption of 39 MB on a 128 MB instance is perfectly in line with web frameworks of Vapor's scale (e.g. Go Gin). The logs do not show any reasonable performance impact of the usage of the loopback adapter in the VaporProxy class.
0 commit comments