Skip to content
This repository was archived by the owner on Jul 14, 2020. It is now read-only.

Commit 3f640cd

Browse files
authored
Run on amzn linux (#8)
This PR changes everything. Sorry. But growing up always comes with a pain. - New package name: `swift-lambda-runtime`. The library is now called `LambdaRuntime` (#11) - The Runtime now only has one handler function. This is more in line with the [go sdk](https://github.com/aws/aws-lambda-go). The end user still has the option to specify different handler methods by using the env variable `_HANDLER` - The runtime is now intended to be used with the [amazonlinux-swift](https://fabianfett.de/amazonlinux-swift) project. That is why the makefile to extract Swift libraries from the Ubuntu Swift Dockerimage has been removed. - `swift-base64-kit` has been added as a dependency to support future Event types. - It is possible now to not return a result from the function (#10) - The synchronous interface has been removed. - The documentation explains how to create a development image (#9) - The documentation has been changed to reflect the changes.
1 parent 2c54662 commit 3f640cd

Some content is hidden

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

46 files changed

+465
-442
lines changed

.github/workflows/ci.yaml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ on:
1010
jobs:
1111
"Integration-Tests":
1212
runs-on: ubuntu-18.04
13+
env:
14+
SWIFT_VERSION: 5.1.3
1315
steps:
1416
- name: Checkout
1517
uses: actions/checkout@v1
@@ -20,9 +22,12 @@ jobs:
2022
- name: Install aws-sam-cli
2123
run: sudo pip install aws-sam-cli
2224
- name: Build Docker Swift Dev Image
23-
run: docker build -t swift-dev:5.1.2 .
24-
- name: Build local layer
25-
run: cd Layer && make create_layer
25+
run: docker build --build-arg SWIFT_VERSION=${SWIFT_VERSION} -t fabianfett/amazonlinux-swift:${SWIFT_VERSION}-amazonlinux2-dev ./docker
26+
- name: Download local layer
27+
run: |
28+
mkdir -p Layer
29+
curl -o Layer/swift-${SWIFT_VERSION}-RELEASE.zip https://amazonlinux-swift.s3.eu-central-1.amazonaws.com/layers/swift-${SWIFT_VERSION}-RELEASE.zip
30+
unzip Layer/swift-${SWIFT_VERSION}-RELEASE.zip -d Layer/swift-lambda-layer
2631
- name: test local lambda
2732
run: make test_lambda
2833
env:
@@ -48,7 +53,7 @@ jobs:
4853
- name: Test
4954
run: swift test --enable-code-coverage --enable-test-discovery
5055
- name: Convert coverage files
51-
run: llvm-cov export -format="lcov" .build/debug/swift-aws-lambdaPackageTests.xctest -instr-profile .build/debug/codecov/default.profdata > info.lcov
56+
run: llvm-cov export -format="lcov" .build/debug/swift-lambda-runtimePackageTests.xctest -instr-profile .build/debug/codecov/default.profdata > info.lcov
5257
- name: Upload to codecov.io
5358
uses: codecov/[email protected]
5459
with:
@@ -72,6 +77,6 @@ jobs:
7277
- name: Xcode Tests
7378
run: |
7479
swift package generate-xcodeproj
75-
xcodebuild -quiet -parallel-testing-enabled YES -scheme swift-aws-lambda-Package -enableCodeCoverage YES build test
80+
xcodebuild -quiet -parallel-testing-enabled YES -scheme swift-lambda-runtime-Package -enableCodeCoverage YES build test
7681
- name: Codecov
77-
run: bash <(curl -s https://codecov.io/bash) -J 'AWSLambda' -t ${{secrets.CODECOV_TOKEN}}
82+
run: bash <(curl -s https://codecov.io/bash) -J 'LambdaRuntime' -t ${{secrets.CODECOV_TOKEN}}

.gitignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
.build
33
/*.xcodeproj
44
xcuserdata
5-
Layer/swift-lambda-runtime
6-
Layer/swift-lambda-runtime.zip
7-
Examples/**/lambda.zip
5+
Layer
6+
Examples/**/lambda.zip
7+
packaged.yaml

Dockerfile

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

Docs/Add-Layer-to-Function.png

-146 KB
Binary file not shown.

Examples/SquareNumber/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let package = Package(
1111
targets: [
1212
.target(
1313
name: "SquareNumber",
14-
dependencies: ["AWSLambda"]
14+
dependencies: ["LambdaRuntime"]
1515
),
1616
]
1717
)

Examples/SquareNumber/Sources/SquareNumber/main.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import AWSLambda
1+
import Foundation
2+
import LambdaRuntime
23
import NIO
34

45
struct Input: Codable {
@@ -9,9 +10,9 @@ struct Output: Codable {
910
let result: Double
1011
}
1112

12-
func squareNumber(input: Input, context: Context) -> Output {
13+
func squareNumber(input: Input, context: Context) -> EventLoopFuture<Output> {
1314
let squaredNumber = input.number * input.number
14-
return Output(result: squaredNumber)
15+
return context.eventLoop.makeSucceededFuture(Output(result: squaredNumber))
1516
}
1617

1718
func printNumber(input: Input, context: Context) -> EventLoopFuture<Void> {
@@ -25,11 +26,18 @@ defer {
2526
}
2627

2728
do {
28-
let runtime = try Runtime.createRuntime(eventLoopGroup: group)
29+
30+
let handler: LambdaRuntime.Handler
31+
switch ProcessInfo.processInfo.environment["_HANDLER"] {
32+
case "printNumber":
33+
handler = LambdaRuntime.codable(printNumber)
34+
default:
35+
handler = LambdaRuntime.codable(squareNumber)
36+
}
37+
38+
let runtime = try LambdaRuntime.createRuntime(eventLoopGroup: group, handler: handler)
2939
defer { try! runtime.syncShutdown() }
3040

31-
runtime.register(for: "squareNumber", handler: Runtime.codable(squareNumber))
32-
runtime.register(for: "printNumber", handler: Runtime.codable(printNumber))
3341
try runtime.start().wait()
3442
}
3543
catch {

Examples/SquareNumber/template.yaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,13 @@ Resources:
1515
SwiftLayer:
1616
Type: AWS::Serverless::LayerVersion
1717
Properties:
18-
ContentUri: Layer/swift-lambda-runtime/
19-
# ContentUri:
20-
# Bucket: de.fabianfett.denkan.app.sam
21-
# Key: swift-lambda-runtime.zip
18+
ContentUri: Layer/swift-lambda-layer/
2219

2320
SquareNumberFunction:
2421
Type: AWS::Serverless::Function
2522
Properties:
2623
CodeUri: Examples/SquareNumber/lambda.zip
27-
Handler: "SquareNumber.squareNumber"
24+
Handler: "squareNumber"
2825
Runtime: provided
2926
Layers:
3027
- !Ref SwiftLayer
@@ -33,7 +30,7 @@ Resources:
3330
Type: AWS::Serverless::Function
3431
Properties:
3532
CodeUri: Examples/SquareNumber/lambda.zip
36-
Handler: "SquareNumber.printNumber"
33+
Handler: "printNumber"
3734
Runtime: provided
3835
Layers:
3936
- !Ref SwiftLayer

Examples/TodoAPIGateway/Package.resolved

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
"repositoryURL": "https://github.com/swift-server/async-http-client.git",
77
"state": {
88
"branch": null,
9-
"revision": "51dc885a30ca704b02fa803099b0a9b5b38067b6",
10-
"version": "1.0.0"
9+
"revision": "48e284d1ea6d0e8baac1af1c4ad8bd298670caf6",
10+
"version": "1.0.1"
1111
}
1212
},
1313
{
1414
"package": "AWSSDKSwift",
1515
"repositoryURL": "https://github.com/swift-aws/aws-sdk-swift.git",
1616
"state": {
17-
"branch": "master",
18-
"revision": "00f9cca4c05bcc65572f78a66437fd764a3384e8",
19-
"version": null
17+
"branch": null,
18+
"revision": "22c57f8ec403a28d89dccf5450b928998f82fdc6",
19+
"version": "4.0.0"
2020
}
2121
},
2222
{
@@ -60,8 +60,8 @@
6060
"repositoryURL": "https://github.com/apple/swift-nio.git",
6161
"state": {
6262
"branch": null,
63-
"revision": "ff01888051cd7efceb1bf8319c1dd3986c4bf6fc",
64-
"version": "2.10.1"
63+
"revision": "f6487a11d80bfb9a0a0a752b7442847c7e3a8253",
64+
"version": "2.12.0"
6565
}
6666
},
6767
{
@@ -78,17 +78,17 @@
7878
"repositoryURL": "https://github.com/apple/swift-nio-ssl.git",
7979
"state": {
8080
"branch": null,
81-
"revision": "ccf96bbe65ecc7c1558ab0dba7ffabdea5c1d31f",
82-
"version": "2.4.4"
81+
"revision": "b75ffaba05b2cffdb1420d558f1a90b4e6c46dcc",
82+
"version": "2.5.0"
8383
}
8484
},
8585
{
8686
"package": "swift-nio-transport-services",
8787
"repositoryURL": "https://github.com/apple/swift-nio-transport-services.git",
8888
"state": {
8989
"branch": null,
90-
"revision": "80b11dc13261e0e52f75ea3a0b2e04f24e925019",
91-
"version": "1.2.1"
90+
"revision": "c7f06384dc5ce7e8506de5ed9b59e35b4d88c64b",
91+
"version": "1.3.0"
9292
}
9393
}
9494
]

Examples/TodoAPIGateway/Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ let package = Package(
1212
dependencies: [
1313
.package(url: "https://github.com/apple/swift-nio.git", .upToNextMajor(from: "2.9.0")),
1414
.package(url: "https://github.com/apple/swift-log.git", .upToNextMajor(from: "1.1.1")),
15-
.package(url: "https://github.com/swift-aws/aws-sdk-swift.git", .branch("master")),
15+
.package(url: "https://github.com/swift-aws/aws-sdk-swift.git", .upToNextMajor(from: "4.0.0")),
1616
.package(path: "../../"),
1717
],
1818
targets: [
1919
.target(
2020
name: "TodoAPIGateway",
21-
dependencies: ["AWSLambda", "Logging", "TodoService", "NIO", "NIOHTTP1", "DynamoDB"]),
21+
dependencies: ["LambdaRuntime", "Logging", "TodoService", "NIO", "NIOHTTP1", "DynamoDB"]),
2222
.testTarget(
2323
name: "TodoAPIGatewayTests",
2424
dependencies: ["TodoAPIGateway"]),

Examples/TodoAPIGateway/Sources/TodoAPIGateway/TodoController.swift

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
//
2-
// File.swift
3-
//
4-
//
5-
// Created by Fabian Fett on 19.11.19.
6-
//
7-
81
import Foundation
92
import NIO
103
import NIOHTTP1
114
import TodoService
12-
import AWSLambda
5+
import LambdaRuntime
136

147
class TodoController {
158

@@ -150,7 +143,7 @@ class TodoController {
150143
encoder.userInfo[.baseUrl] = URL(string: "\(proto)://\(host)/\(request.requestContext.stage)")!
151144
}
152145
else { //local
153-
encoder.userInfo[.baseUrl] = URL(string: "\(proto)://\(host)/")!
146+
encoder.userInfo[.baseUrl] = URL(string: "\(proto)://\(host)")!
154147
}
155148

156149
return encoder

Examples/TodoAPIGateway/Sources/TodoAPIGateway/main.swift

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import AWSLambda
1+
import LambdaRuntime
22
import NIO
33
import Logging
44
import Foundation
@@ -10,12 +10,12 @@ LoggingSystem.bootstrap(StreamLogHandler.standardError)
1010

1111
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
1212
defer { try! group.syncShutdownGracefully() }
13-
let logger = Logger(label: "AWSLambda.TodoAPIGateway")
13+
let logger = Logger(label: "Lambda.TodoAPIGateway")
1414

1515
do {
1616
logger.info("start runtime")
17-
let runtime = try Runtime.createRuntime(eventLoopGroup: group)
18-
let env = runtime.environment
17+
18+
let env = try Environment()
1919
let store = DynamoTodoStore(
2020
eventLoopGroup: group,
2121
tableName: "SwiftLambdaTodos",
@@ -25,19 +25,30 @@ do {
2525
region: Region(rawValue: env.region)!)
2626
let controller = TodoController(store: store)
2727

28-
defer { try! runtime.syncShutdown() }
29-
30-
logger.info("register functions")
31-
32-
runtime.register(for: "list", handler: APIGateway.handler(controller.listTodos))
33-
runtime.register(for: "create", handler: APIGateway.handler(controller.createTodo))
34-
runtime.register(for: "deleteAll", handler: APIGateway.handler(controller.deleteAll))
35-
runtime.register(for: "getTodo", handler: APIGateway.handler(controller.getTodo))
36-
runtime.register(for: "deleteTodo", handler: APIGateway.handler(controller.deleteTodo))
37-
runtime.register(for: "patchTodo", handler: APIGateway.handler(controller.patchTodo))
28+
logger.info("register function")
29+
30+
let handler: LambdaRuntime.Handler
31+
switch env.handlerName {
32+
case "list":
33+
handler = APIGateway.handler(controller.listTodos)
34+
case "create":
35+
handler = APIGateway.handler(controller.createTodo)
36+
case "deleteAll":
37+
handler = APIGateway.handler(controller.deleteAll)
38+
case "getTodo":
39+
handler = APIGateway.handler(controller.getTodo)
40+
case "deleteTodo":
41+
handler = APIGateway.handler(controller.deleteTodo)
42+
case "patchTodo":
43+
handler = APIGateway.handler(controller.patchTodo)
44+
default:
45+
fatalError("Unexpected handler")
46+
}
3847

3948
logger.info("starting runloop")
4049

50+
let runtime = try LambdaRuntime.createRuntime(eventLoopGroup: group, environment: env, handler: handler)
51+
defer { try! runtime.syncShutdown() }
4152
try runtime.start().wait()
4253
}
4354
catch {

Examples/TodoAPIGateway/Sources/TodoService/DynamoTodoStore.swift

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,16 @@ public class DynamoTodoStore {
1313
tableName: String,
1414
accessKeyId: String,
1515
secretAccessKey: String,
16-
sessionToken: String,
16+
sessionToken: String?,
1717
region: Region)
1818
{
19-
2019
self.dynamo = DynamoDB(
2120
accessKeyId: accessKeyId,
2221
secretAccessKey: secretAccessKey,
2322
sessionToken: sessionToken,
2423
region: region,
2524
eventLoopGroupProvider: .shared(eventLoopGroup))
2625
self.tableName = tableName
27-
2826
}
2927

3028
}
@@ -130,17 +128,11 @@ extension DynamoTodoStore: TodoStore {
130128
.map { _ in }
131129
}
132130

133-
// func updateTodo() -> EventLoopFuture<TodoItem> {
134-
//
135-
// }
136-
137131
public func deleteAllTodos() -> EventLoopFuture<Void> {
138-
139132
return self.getTodos()
140133
.flatMap { (todos) -> EventLoopFuture<Void> in
141134
let ids = todos.map() { $0.id }
142135
return self.deleteTodos(ids: ids)
143136
}
144-
145137
}
146138
}

Examples/TodoAPIGateway/Tests/TodoServiceTests/TodoServiceTests.swift renamed to Examples/TodoAPIGateway/Tests/TodoServiceTests/DynamoTodoStoreTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import NIO
33
import XCTest
44
@testable import TodoService
55

6-
final class TodoAPIGatewayTests: XCTestCase {
7-
6+
final class DynamoTodoStoreTests: XCTestCase {
7+
88
}

0 commit comments

Comments
 (0)