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()) }

0 commit comments

Comments
 (0)