Skip to content

Commit cf98776

Browse files
Use spm archive and remove lambda layer
1 parent f0b3786 commit cf98776

30 files changed

+50
-1945
lines changed

Dockerfile

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
FROM swift:5.7.3-amazonlinux2 as builder
2+
RUN yum -y update && \
3+
yum -y install git zip

Makefile

+10-106
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2019 (c) Andrea Scuderi - https://github.com/swift-sprinter
1+
# Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-sprinter
22

33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -12,69 +12,29 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# Use this tag to build a customized local image
15+
SWIFT_VERSION=5.7.3
1616

17-
SWIFT_VERSION?=5.7.2-amazonlinux2
18-
LAYER_VERSION?=5.7.2-amazonlinux2
19-
DOCKER_OS?=amazonlinux2
20-
21-
# SWIFT_VERSION?=nightly-master-amazonlinux2
22-
# LAYER_VERSION?=nightly-master-amazonlinux2
23-
# DOCKER_OS?=amazonlinux2
24-
25-
# SWIFT_VERSION?=5.2.4-bionic
26-
# LAYER_VERSION?=5-2-4-bionic
27-
# DOCKER_OS=bionic
28-
29-
DOCKER_TAG=nio-swift:$(SWIFT_VERSION)
30-
SWIFT_DOCKER_IMAGE=$(DOCKER_TAG)
31-
SWIFT_LAMBDA_LIBRARY=nio-swift-lambda-runtime-$(LAYER_VERSION)
17+
SWIFT_DOCKER_IMAGE=swift-amazonlinux2-builder:$(SWIFT_VERSION)
3218
SWIFT_CONFIGURATION=release
3319

34-
BUILD_PATH=.build
35-
SERVERLESS_BUILD=build
36-
SERVERLESS_LAYER=swift-lambda-runtime
37-
38-
# Configuration
39-
40-
# HelloWorld Example Configuration
41-
SWIFT_EXECUTABLE?=Products
42-
SWIFT_PROJECT_PATH?=Products
43-
LAMBDA_FUNCTION_NAME?=Products
44-
LAMBDA_HANDLER?=$(SWIFT_EXECUTABLE).handler
45-
4620
# Internals
47-
LAMBDA_ZIP=lambda.zip
48-
SHARED_LIBS_FOLDER=swift-shared-libs
49-
LAYER_ZIP=swift-lambda-runtime-$(LAYER_VERSION).zip
50-
ROOT_BUILD_PATH=./build
51-
LAYER_BUILD_PATH=$(ROOT_BUILD_PATH)
52-
LAMBDA_BUILD_PATH=$(ROOT_BUILD_PATH)
21+
LAMBDA_ARCHIVE_PATH=./$(DOCKER_BUILD_PATH)
5322

54-
# use this for local development
5523
MOUNT_ROOT=$(shell pwd)
56-
DOCKER_PROJECT_PATH=$(SWIFT_PROJECT_PATH)
57-
ROOT_BUILD_PATH=./.build
58-
LAYER_BUILD_PATH=$(ROOT_BUILD_PATH)/layer
59-
LAMBDA_BUILD_PATH=$(ROOT_BUILD_PATH)/lambda
60-
LOCAL_LAMBDA_PATH=$(ROOT_BUILD_PATH)/local
61-
LOCALSTACK_TMP=$(ROOT_BUILD_PATH)/.tmp
62-
TMP_BUILD_PATH=$(ROOT_BUILD_PATH)/tmp
63-
DATETIME=$(shell date +'%y%m%d-%H%M%S')
64-
DOCKER_FOLDER=docker
65-
BOOTSTRAP=$(DOCKER_FOLDER)/$(SWIFT_VERSION)/bootstrap
24+
DOCKER_BUILD_PATH=build
25+
DOCKER_PROJECT_PATH=Products
6626

6727
docker_build:
68-
docker build --tag $(DOCKER_TAG) docker/$(SWIFT_VERSION)/.
28+
docker build --tag $(SWIFT_DOCKER_IMAGE) .
6929

70-
build_lambda:
30+
archive_lambda: create_build_directory
7131
docker run \
7232
--rm \
7333
--volume "$(MOUNT_ROOT)/:/src" \
7434
--volume "$(MOUNT_ROOT)/../Breeze:/Breeze" \
7535
--workdir "/src/$(DOCKER_PROJECT_PATH)" \
7636
$(SWIFT_DOCKER_IMAGE) \
77-
/bin/bash -c "swift build -c $(SWIFT_CONFIGURATION) -Xswiftc -g"
37+
/bin/bash -c "swift package archive -c $(SWIFT_CONFIGURATION) --verbose --output-path /src/$(DOCKER_BUILD_PATH)"
7838

7939
docker_bash:
8040
docker run \
@@ -86,61 +46,5 @@ docker_bash:
8646
$(SWIFT_DOCKER_IMAGE) \
8747
/bin/bash
8848

89-
cp_lambda_to_sls_build_local: create_build_directory
90-
docker run \
91-
--rm \
92-
--volume "$(MOUNT_ROOT)/:/src" \
93-
--workdir "/src/$(DOCKER_PROJECT_PATH)" \
94-
$(SWIFT_DOCKER_IMAGE) \
95-
/bin/bash -c "swift build -c $(SWIFT_CONFIGURATION) --show-bin-path | tr '\n' '/' > $(BUILD_PATH)/path.txt; echo '$(SWIFT_EXECUTABLE)' >> $(BUILD_PATH)/path.txt | cat $(BUILD_PATH)/path.txt | xargs cp -t ../$(SERVERLESS_BUILD); rm $(BUILD_PATH)/path.txt"
96-
9749
create_build_directory:
98-
if [ ! -d "$(LAMBDA_BUILD_PATH)" ]; then mkdir -p $(LAMBDA_BUILD_PATH); fi
99-
if [ ! -d "$(LAYER_BUILD_PATH)" ]; then mkdir -p $(LAYER_BUILD_PATH); fi
100-
if [ ! -d "$(SERVERLESS_BUILD)" ]; then mkdir -p $(SERVERLESS_BUILD); fi
101-
102-
extract_libraries:
103-
docker run \
104-
-it \
105-
--rm \
106-
--volume "$(MOUNT_ROOT)/:/src" \
107-
--workdir "/src" \
108-
$(SWIFT_DOCKER_IMAGE) \
109-
/bin/bash -c "ldd $(SWIFT_PROJECT_PATH)/.build/$(SWIFT_CONFIGURATION)/$(SWIFT_EXECUTABLE) | grep swift | ./extract.sh > docker/$(SWIFT_VERSION)/swift-shared-libraries.txt"
110-
111-
package_lambda: create_build_directory build_lambda
112-
zip -r -j $(LAMBDA_BUILD_PATH)/$(LAMBDA_ZIP) $(SWIFT_PROJECT_PATH)/$(BUILD_PATH)/$(SWIFT_CONFIGURATION)/$(SWIFT_EXECUTABLE)
113-
114-
package_layer: create_build_directory
115-
$(eval SHARED_LIBRARIES := $(shell cat docker/$(SWIFT_VERSION)/swift-shared-libraries.txt | tr '\n' ' '))
116-
mkdir -p $(SHARED_LIBS_FOLDER)/lib
117-
ifeq '$(DOCKER_OS)' 'bionic'
118-
docker run \
119-
--rm \
120-
--volume "$(shell pwd)/:/src" \
121-
--workdir "/src" \
122-
$(SWIFT_DOCKER_IMAGE) \
123-
cp /lib64/ld-linux-x86-64.so.2 $(SHARED_LIBS_FOLDER)
124-
endif
125-
docker run \
126-
--rm \
127-
--volume "$(shell pwd)/:/src" \
128-
--workdir "/src" \
129-
$(SWIFT_DOCKER_IMAGE) \
130-
cp -t $(SHARED_LIBS_FOLDER)/lib $(SHARED_LIBRARIES)
131-
cp $(BOOTSTRAP) $(SHARED_LIBS_FOLDER)
132-
cd $(SHARED_LIBS_FOLDER); pwd; zip -r ../$(LAYER_BUILD_PATH)/$(LAYER_ZIP) bootstrap lib
133-
134-
cp_layer_to_sls_build_local: create_build_directory
135-
if [ ! -d "./$(SERVERLESS_BUILD)/$(SERVERLESS_LAYER)" ]; then mkdir -p ./$(SERVERLESS_BUILD)/$(SERVERLESS_LAYER); fi
136-
cp -R ./$(SHARED_LIBS_FOLDER)/. ./$(SERVERLESS_BUILD)/$(SERVERLESS_LAYER)
137-
138-
# test_api:
139-
# curl -X POST https://$(API_ID).execute-api.us-east-1.amazonaws.com/products -H 'Content-Type: application/json' -d '{"updatedAt":"2022-06-27T20:27:03.030Z","name":"Hello","description":"Hello","sku":"4","createdAt":"2022-06-27T20:27:03.030Z"}'
140-
# curl -X POST https://$(API_ID).execute-api.us-east-1.amazonaws.com/products -H 'Content-Type: application/json' -d '{"updatedAt":"2022-06-27T20:27:03.030Z","name":"Hello","description":"Hello","sku":"5","createdAt":"2022-06-27T20:27:03.030Z"}'
141-
# curl https://$(API_ID).execute-api.us-east-1.amazonaws.com/products/4
142-
# curl https://$(API_ID).execute-api.us-east-1.amazonaws.com/products
143-
# curl -X DEL https://$(API_ID).execute-api.us-east-1.amazonaws.com/products/4
144-
# curl -PUT https://$(API_ID).execute-api.us-east-1.amazonaws.com/products -H 'Content-Type: application/json' -d '{"createdAt":"2022-06-27T20:27:03.030Z","description":"Hello 3","updatedAt":"2022-06-27T20:27:03.030Z","name":"Hello","sku":"3"}'
145-
# curl -X DEL https://$(API_ID).execute-api.us-east-1.amazonaws.com/products/5
146-
# curl https://$(API_ID).execute-api.us-east-1.amazonaws.com/products
50+
if [ ! -d "$(LAMBDA_ARCHIVE_PATH)" ]; then mkdir -p $(LAMBDA_ARCHIVE_PATH); fi

Products/Package.resolved

+17-8
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
"kind" : "remoteSourceControl",
3333
"location" : "https://github.com/soto-project/soto-core.git",
3434
"state" : {
35-
"revision" : "cf1c872126e4874144ed2f91aa2124e72388abe0",
36-
"version" : "6.4.1"
35+
"revision" : "787be995b0cff07bd28e9aea4c8a6424ead36e71",
36+
"version" : "6.4.2"
3737
}
3838
},
3939
{
@@ -50,8 +50,8 @@
5050
"kind" : "remoteSourceControl",
5151
"location" : "https://github.com/swift-server/swift-aws-lambda-events.git",
5252
"state" : {
53-
"branch" : "main",
54-
"revision" : "8e836e0893bc906fafd6b09e1c46a7efcc441fd5"
53+
"revision" : "8e836e0893bc906fafd6b09e1c46a7efcc441fd5",
54+
"version" : "0.1.0"
5555
}
5656
},
5757
{
@@ -81,6 +81,15 @@
8181
"version" : "1.0.4"
8282
}
8383
},
84+
{
85+
"identity" : "swift-crypto",
86+
"kind" : "remoteSourceControl",
87+
"location" : "https://github.com/apple/swift-crypto.git",
88+
"state" : {
89+
"revision" : "da0fe44138ab86e380f40a2acbd8a611b07d3f64",
90+
"version" : "2.4.0"
91+
}
92+
},
8493
{
8594
"identity" : "swift-log",
8695
"kind" : "remoteSourceControl",
@@ -113,17 +122,17 @@
113122
"kind" : "remoteSourceControl",
114123
"location" : "https://github.com/apple/swift-nio-extras.git",
115124
"state" : {
116-
"revision" : "98378d1fe56527761c180f70b2d66a7b2307fc39",
117-
"version" : "1.16.0"
125+
"revision" : "d75ed708d00353acf173ca23018b6bd46f949464",
126+
"version" : "1.17.0"
118127
}
119128
},
120129
{
121130
"identity" : "swift-nio-http2",
122131
"kind" : "remoteSourceControl",
123132
"location" : "https://github.com/apple/swift-nio-http2.git",
124133
"state" : {
125-
"revision" : "8dcda6828844022028025241e4e7d52cbbe250de",
126-
"version" : "1.25.0"
134+
"revision" : "38feec96bcd929028939107684073554bf01abeb",
135+
"version" : "1.25.2"
127136
}
128137
},
129138
{

README.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# AWS Serverless Swift API Template
22

3-
[![Swift 5.7.2](https://img.shields.io/badge/Swift-5.7.2-blue.svg)](https://swift.org/download/) [![docker amazonlinux2](https://img.shields.io/badge/docker-amazonlinux2-orange.svg)](https://swift.org/download/)
3+
[![Swift 5.7.3](https://img.shields.io/badge/Swift-5.7.3-blue.svg)](https://swift.org/download/) [![docker amazonlinux2](https://img.shields.io/badge/docker-amazonlinux2-orange.svg)](https://swift.org/download/)
44

5-
This package demostrates how to write a Scalable REST API with the Serverless stack by using only Swift as a development language.
5+
This package demonstrates how to write a Scalable REST API with the Serverless stack by using only Swift as a development language.
66

77
## Product API Example
88

@@ -35,18 +35,16 @@ The API implements the following schema:
3535

3636
More details of the API are described in [swagger.json](swagger.json).
3737

38-
The file can be imported in popular tool such as PostMan.
38+
The file can be imported into popular tools such as PostMan.
3939

4040
Be sure to update the `"host": "<BASE_URL>"` with the URL provided during the deployment.
4141

42-
The full `swagger-doc.html` has been generated using `pretty-swag`
43-
4442
## Serverless architecture
4543

4644
The architecture is based on the classical AWS Serverless stack: APIGateway, Lambda and DynamoDB.
47-
- `APIGateway`: acts as a `proxy` for the `Lambda` and exposes it to the internet.
48-
- `Lambda`: is the computational layer.
49-
- `DynamoDB`: is the AWS `NoSQL` database
45+
- `APIGateway` acts as a `proxy` for `Lambda` and exposes it to the internet.
46+
- `Lambda` is the computational layer.
47+
- `DynamoDB` is the AWS `NoSQL` database
5048

5149
Advantages:
5250
- Pay per use

build.sh

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
make docker_build
2-
make package_layer
3-
make build_lambda
4-
make cp_layer_to_sls_build_local
5-
make cp_lambda_to_sls_build_local
2+
make archive_lambda

docker/5.1.5-bionic/Dockerfile

-4
This file was deleted.

docker/5.1.5-bionic/bootstrap

-3
This file was deleted.

docker/5.1.5-bionic/swift-shared-libraries.txt

-63
This file was deleted.

docker/5.2.3-bionic/Dockerfile

-4
This file was deleted.

docker/5.2.3-bionic/bootstrap

-3
This file was deleted.

0 commit comments

Comments
 (0)