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
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

0 commit comments

Comments
 (0)