Skip to content

Commit 4718960

Browse files
SWIFT-1536 Add AWS Lambda Example (#771)
1 parent 55a2d42 commit 4718960

File tree

6 files changed

+135
-1
lines changed

6 files changed

+135
-1
lines changed

Examples/AWSLambdaExample/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM swift:5.6-amazonlinux2
2+
RUN yum -y install cmake openssl-devel cyrus-sasl-devel
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// swift-tools-version:5.5
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "AWSLambdaExample",
8+
platforms: [.macOS(.v12)],
9+
dependencies: [
10+
.package(
11+
url: "https://github.com/swift-server/swift-aws-lambda-runtime.git",
12+
branch: "main"
13+
),
14+
.package(
15+
url: "https://github.com/mongodb/mongo-swift-driver",
16+
.upToNextMajor(from: "1.0.0")
17+
)
18+
],
19+
targets: [
20+
.executableTarget(
21+
name: "AWSLambdaExample",
22+
dependencies: [
23+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
24+
.product(name: "MongoSwift", package: "mongo-swift-driver")
25+
]
26+
),
27+
]
28+
)

Examples/AWSLambdaExample/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# AWSLambdaExample
2+
3+
This is a minimal working example of using the driver in AWS Lambda.
4+
5+
## Building the example
6+
The example code needs to be compiled for Amazon Linux 2 to be run in AWS Lambda.
7+
You can use [Docker](https://docs.docker.com/desktop/) to achieve this.
8+
9+
First, run the following command to build a Docker image using the [`Dockerfile`](Dockerfile) in
10+
this directory:
11+
```
12+
docker build -t swift-lambda .
13+
```
14+
This will install the dependencies necessary to build the driver on Linux.
15+
16+
Note that the `Dockerfile` uses Swift 5.6, the latest version at the time of writing. If you'd like
17+
to use a different version, visit [DockerHub](https://hub.docker.com/_/swift) for a full list of
18+
Swift Docker images.
19+
20+
Next, use the image you created to compile the example:
21+
```
22+
$ docker run \
23+
--rm \
24+
--volume "$(pwd)/:/src" \
25+
--workdir "/src/" \
26+
swift-lambda \
27+
swift build --product AWSLambdaExample -c release -Xswiftc -static-stdlib
28+
```
29+
30+
Finally, run the [`package.sh`](package.sh) script in this directory to create a symlink called
31+
`bootstrap` and zip the folder:
32+
```
33+
./package.sh AWSLambdaExample
34+
```
35+
Navigate to the Code section on the page for your AWS Lambda function in the AWS Console and upload
36+
the `lambda.zip` file created.
37+
38+
## Acknowledgements
39+
The instructions for using Docker to build the example were largely taken from
40+
[this blog post](https://fabianfett.dev/getting-started-with-swift-aws-lambda-runtime) by
41+
Fabian Fett. Feel free to read through the post for more detailed information on the Docker
42+
commands used.
43+
44+
The `package.sh` script is copied from
45+
[this example](https://github.com/swift-server/swift-aws-lambda-runtime/blob/main/Examples/Deployment/scripts/package.sh)
46+
in the `swift-aws-lambda-runtime` repository.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// begin lambda connection example 1
2+
import AWSLambdaRuntime
3+
import Foundation
4+
import MongoSwift
5+
import NIO
6+
7+
struct Input: Codable {
8+
let number: Double
9+
}
10+
11+
struct Response: Codable {
12+
let ok: Double
13+
}
14+
15+
@main
16+
final class MongoHandler: LambdaHandler {
17+
typealias Event = Input
18+
typealias Output = Response
19+
20+
let elg: EventLoopGroup
21+
let mongoClient: MongoClient
22+
23+
required init(context _: LambdaInitializationContext) async throws {
24+
let uri = ProcessInfo.processInfo.environment["MONGODB_URI"] ?? "mongodb://localhost:27017"
25+
self.elg = MultiThreadedEventLoopGroup(numberOfThreads: 4)
26+
self.mongoClient = try MongoClient(uri, using: self.elg)
27+
}
28+
29+
deinit {
30+
// clean up driver resources
31+
try? mongoClient.syncClose()
32+
cleanupMongoSwift()
33+
34+
// shut down EventLoopGroup
35+
try? elg.syncShutdownGracefully()
36+
}
37+
38+
func handle(_: Event, context _: LambdaContext) async throws -> Output {
39+
let db = self.mongoClient.db("db")
40+
let response = try await db.runCommand(["ping": 1])
41+
return try BSONDecoder().decode(Output.self, from: response)
42+
}
43+
}
44+
45+
// end lambda connection example 1

Examples/AWSLambdaExample/package.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
executable=$1
6+
7+
target=.build/lambda/$executable
8+
rm -rf "$target"
9+
mkdir -p "$target"
10+
cp ".build/release/$executable" "$target/"
11+
cd "$target"
12+
ln -s "$executable" "bootstrap"
13+
zip --symlinks lambda.zip *

etc/build-examples.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exit_code=0
44

5-
examples=("BugReport" "Docs" "KituraExample" "PerfectExample" "VaporExample" "Atlas")
5+
examples=("BugReport" "Docs" "KituraExample" "PerfectExample" "VaporExample" "Atlas" "AWSLambdaExample")
66

77
branch=${1}
88
# Ensure branch is non-empty

0 commit comments

Comments
 (0)