Skip to content

Commit b757de0

Browse files
authored
Update and add examples to new APIs (#228)
* take advantage of @main where possible * move the top level Sample code to the examples subdirectory * extract a few examples form the "LambdaFunctions" directory (which is really a deployment demo) and move them to the top level Examples directory * rename "LambdaFunctions" examples as "Deployments" to make their intent clearer * add a sample that demonstrates how to test a lambda now that SwiftPM can test executables directly * update the test-sample docker setup to build & test th new samples * fix a few typos and In/Out typealias left overs * remove LinuxMain since its no longer required
1 parent 7c1dea0 commit b757de0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+310
-188
lines changed

Examples/Benchmark/Package.swift

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+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "swift-aws-lambda-runtime-example",
7+
platforms: [
8+
.macOS(.v12),
9+
],
10+
products: [
11+
.executable(name: "MyLambda", targets: ["MyLambda"]),
12+
],
13+
dependencies: [
14+
// this is the dependency on the swift-aws-lambda-runtime library
15+
// in real-world projects this would say
16+
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
17+
.package(name: "swift-aws-lambda-runtime", path: "../.."),
18+
],
19+
targets: [
20+
.executableTarget(
21+
name: "MyLambda",
22+
dependencies: [
23+
.product(name: "AWSLambdaRuntimeCore", package: "swift-aws-lambda-runtime"),
24+
],
25+
path: "."
26+
),
27+
]
28+
)

Examples/Benchmark/main.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import AWSLambdaRuntimeCore
16+
import NIOCore
17+
18+
// If you would like to benchmark Swift's Lambda Runtime,
19+
// use this example which is more performant.
20+
// `EventLoopLambdaHandler` does not offload the Lambda processing to a separate thread
21+
// while the closure-based handlers do.
22+
23+
struct MyLambda: EventLoopLambdaHandler {
24+
typealias Event = String
25+
typealias Output = String
26+
27+
func handle(_ event: String, context: Lambda.Context) -> EventLoopFuture<String> {
28+
context.eventLoop.makeSucceededFuture("hello, world!")
29+
}
30+
}
31+
32+
Lambda.run { $0.eventLoop.makeSucceededFuture(MyLambda()) }

Examples/Deployment/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM swift:5.5-amazonlinux2
2+
3+
RUN yum -y install zip

Examples/LambdaFunctions/Package.swift renamed to Examples/Deployment/Package.swift

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ let package = Package(
1313
// good for benchmarking
1414
.executable(name: "Benchmark", targets: ["Benchmark"]),
1515
// demonstrate different types of error handling
16-
.executable(name: "ErrorHandling", targets: ["ErrorHandling"]),
17-
// fully featured example with domain specific business logic
18-
.executable(name: "CurrencyExchange", targets: ["CurrencyExchange"]),
1916
],
2017
dependencies: [
2118
// this is the dependency on the swift-aws-lambda-runtime library
@@ -30,11 +27,5 @@ let package = Package(
3027
.executableTarget(name: "HelloWorld", dependencies: [
3128
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
3229
]),
33-
.executableTarget(name: "ErrorHandling", dependencies: [
34-
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
35-
]),
36-
.executableTarget(name: "CurrencyExchange", dependencies: [
37-
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
38-
]),
3930
]
4031
)

Examples/LambdaFunctions/README.md renamed to Examples/Deployment/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
# Lambda Functions Examples
1+
# Deployment Examples
22

33
This sample project is a collection of Lambda functions that demonstrates
44
how to write a simple Lambda function in Swift, and how to package and deploy it
55
to the AWS Lambda platform.
66

7-
The scripts are prepared to work from the `LambdaFunctions` folder.
7+
The scripts are prepared to work from the `Deployment` folder.
88

99
```
1010
git clone https://github.com/swift-server/swift-aws-lambda-runtime.git
11-
cd swift-aws-lambda-runtime/Examples/LambdaFunctions
11+
cd swift-aws-lambda-runtime/Examples/Deployment
1212
```
1313

1414
Note: The example scripts assume you have [jq](https://stedolan.github.io/jq/download/) command line tool installed.
@@ -27,7 +27,7 @@ Steps to deploy this sample to AWS Lambda using the AWS CLI:
2727
./scripts/deploy.sh
2828
```
2929

30-
Notes:
30+
Notes:
3131
- This script assumes you have AWS CLI installed and credentials setup in `~/.aws/credentials`.
3232
- The default lambda function name is `SwiftSample`. You can specify a different one updating `lambda_name` in `deploy.sh`
3333
- Update `s3_bucket=swift-lambda-test` in `deploy.sh` before running (AWS S3 buckets require a unique global name)
@@ -129,7 +129,7 @@ The `serverless-deploy.sh` script passes through any parameters to the Serverles
129129

130130
For the APIGateway sample:
131131

132-
The Serverless template will provide an endpoint which you can use to test the Lambda.
132+
The Serverless template will provide an endpoint which you can use to test the Lambda.
133133

134134
Outuput example:
135135

@@ -174,4 +174,4 @@ For all other samples use the AWS Lambda console.
174174
./scripts/serverless-remove.sh
175175
```
176176

177-
The script will ask you which sample Lambda you wish to remove from the previous depolyment.
177+
The script will ask you which sample Lambda you wish to remove from the previous deployment.

Examples/LambdaFunctions/Sources/Benchmark/main.swift renamed to Examples/Deployment/Sources/Benchmark/main.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import NIO
1919
// use this example which is more performant.
2020
// `EventLoopLambdaHandler` does not offload the Lambda processing to a separate thread
2121
// while the closure-based handlers do.
22-
Lambda.run { $0.eventLoop.makeSucceededFuture(BenchmarkHandler()) }
2322

2423
struct BenchmarkHandler: EventLoopLambdaHandler {
2524
typealias Event = String
@@ -29,3 +28,5 @@ struct BenchmarkHandler: EventLoopLambdaHandler {
2928
context.eventLoop.makeSucceededFuture("hello, world!")
3029
}
3130
}
31+
32+
Lambda.run { $0.eventLoop.makeSucceededFuture(BenchmarkHandler()) }

Examples/LambdaFunctions/scripts/build-and-package.sh renamed to Examples/Deployment/scripts/build-and-package.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ echo "done"
2727
echo "-------------------------------------------------------------------------"
2828
echo "building \"$executable\" lambda"
2929
echo "-------------------------------------------------------------------------"
30-
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder \
30+
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/Deployment builder \
3131
bash -cl "swift build --product $executable -c release"
3232
echo "done"
3333

3434
echo "-------------------------------------------------------------------------"
3535
echo "packaging \"$executable\" lambda"
3636
echo "-------------------------------------------------------------------------"
37-
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder \
37+
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/Deployment builder \
3838
bash -cl "./scripts/package.sh $executable"
3939
echo "done"

Examples/Echo/Lambda.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import AWSLambdaRuntime
16+
17+
// in this example we are receiving and responding with strings
18+
19+
@main
20+
struct MyLambda: LambdaHandler {
21+
typealias Event = String
22+
typealias Output = String
23+
24+
init(context: Lambda.InitializationContext) async throws {
25+
// setup your resources that you want to reuse for every invocation here.
26+
}
27+
28+
func handle(_ input: String, context: Lambda.Context) async throws -> String {
29+
// as an example, respond with the input's reversed
30+
String(input.reversed())
31+
}
32+
}

Examples/Echo/Package.swift

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+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "swift-aws-lambda-runtime-example",
7+
platforms: [
8+
.macOS(.v12),
9+
],
10+
products: [
11+
.executable(name: "MyLambda", targets: ["MyLambda"]),
12+
],
13+
dependencies: [
14+
// this is the dependency on the swift-aws-lambda-runtime library
15+
// in real-world projects this would say
16+
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
17+
.package(name: "swift-aws-lambda-runtime", path: "../.."),
18+
],
19+
targets: [
20+
.executableTarget(
21+
name: "MyLambda",
22+
dependencies: [
23+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
24+
],
25+
path: "."
26+
),
27+
]
28+
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import AWSLambdaRuntime
1717
// MARK: - Run Lambda
1818

1919
@main
20-
struct ErrorsHappenHandler: LambdaHandler {
20+
struct MyLambda: LambdaHandler {
2121
typealias Event = Request
2222
typealias Output = Response
2323

Examples/ErrorHandling/Package.swift

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+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "swift-aws-lambda-runtime-example",
7+
platforms: [
8+
.macOS(.v12),
9+
],
10+
products: [
11+
.executable(name: "MyLambda", targets: ["MyLambda"]),
12+
],
13+
dependencies: [
14+
// this is the dependency on the swift-aws-lambda-runtime library
15+
// in real-world projects this would say
16+
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
17+
.package(name: "swift-aws-lambda-runtime", path: "../.."),
18+
],
19+
targets: [
20+
.executableTarget(
21+
name: "MyLambda",
22+
dependencies: [
23+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
24+
],
25+
path: "."
26+
),
27+
]
28+
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import Logging
2424
// MARK: - Run Lambda
2525

2626
@main
27-
struct CurrencyExchangeHandler: LambdaHandler {
27+
struct MyLambda: LambdaHandler {
2828
typealias Event = Request
2929
typealias Output = [Exchange]
3030

Examples/Foundation/Package.swift

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+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "swift-aws-lambda-runtime-example",
7+
platforms: [
8+
.macOS(.v12),
9+
],
10+
products: [
11+
.executable(name: "MyLambda", targets: ["MyLambda"]),
12+
],
13+
dependencies: [
14+
// this is the dependency on the swift-aws-lambda-runtime library
15+
// in real-world projects this would say
16+
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
17+
.package(name: "swift-aws-lambda-runtime", path: "../.."),
18+
],
19+
targets: [
20+
.executableTarget(
21+
name: "MyLambda",
22+
dependencies: [
23+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
24+
],
25+
path: "."
26+
),
27+
]
28+
)

Sources/CodableSample/main.swift renamed to Examples/JSON/Lambda.swift

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the SwiftAWSLambdaRuntime open source project
44
//
5-
// Copyright (c) 2017-2020 Apple Inc. and the SwiftAWSLambdaRuntime project authors
5+
// Copyright (c) 2021 Apple Inc. and the SwiftAWSLambdaRuntime project authors
66
// Licensed under Apache License v2.0
77
//
88
// See LICENSE.txt for license information
@@ -13,7 +13,6 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import AWSLambdaRuntime
16-
import NIOCore
1716

1817
struct Request: Codable {
1918
let body: String
@@ -24,23 +23,19 @@ struct Response: Codable {
2423
}
2524

2625
// in this example we are receiving and responding with codables. Request and Response above are examples of how to use
27-
// codables to model your reqeuest and response objects
28-
struct Handler: EventLoopLambdaHandler {
26+
// codables to model your request and response objects
27+
28+
@main
29+
struct MyLambda: LambdaHandler {
2930
typealias Event = Request
3031
typealias Output = Response
3132

32-
func handle(_ event: Request, context: Lambda.Context) -> EventLoopFuture<Response> {
33+
init(context: Lambda.InitializationContext) async throws {
34+
// setup your resources that you want to reuse for every invocation here.
35+
}
36+
37+
func handle(_ event: Request, context: Lambda.Context) async throws -> Response {
3338
// as an example, respond with the input event's reversed body
34-
context.eventLoop.makeSucceededFuture(Response(body: String(event.body.reversed())))
39+
Response(body: String(event.body.reversed()))
3540
}
3641
}
37-
38-
Lambda.run { $0.eventLoop.makeSucceededFuture(Handler()) }
39-
40-
// MARK: - this can also be expressed as a closure:
41-
42-
/*
43-
Lambda.run { (_, request: Request, callback) in
44-
callback(.success(Response(body: String(request.body.reversed()))))
45-
}
46-
*/

Examples/JSON/Package.swift

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+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "swift-aws-lambda-runtime-example",
7+
platforms: [
8+
.macOS(.v12),
9+
],
10+
products: [
11+
.executable(name: "MyLambda", targets: ["MyLambda"]),
12+
],
13+
dependencies: [
14+
// this is the dependency on the swift-aws-lambda-runtime library
15+
// in real-world projects this would say
16+
// .package(url: "https://github.com/swift-server/swift-aws-lambda-runtime.git", from: "1.0.0")
17+
.package(name: "swift-aws-lambda-runtime", path: "../.."),
18+
],
19+
targets: [
20+
.executableTarget(
21+
name: "MyLambda",
22+
dependencies: [
23+
.product(name: "AWSLambdaRuntime", package: "swift-aws-lambda-runtime"),
24+
],
25+
path: "."
26+
),
27+
]
28+
)

Examples/LambdaFunctions/Dockerfile

Lines changed: 0 additions & 3 deletions
This file was deleted.

Examples/LambdaFunctions/scripts/SAM/CurrencyExchange-template.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)