Skip to content

Commit c683b41

Browse files
Add example scripts and docs for Serverless Framework (#113)
* Add example scripts and docs for Serverless Framework * Refactor Examples folder and scripts - Add a common config.sh script - Move SAM templates to scripts/SAM - Add Serverless templates to scripts/serverless - Update the scripts to work with new folder structure and config.sh
1 parent a5e2fd0 commit c683b41

18 files changed

+304
-34
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
.swift-version
88
xcuserdata
99
Package.resolved
10+
.serverless

Examples/LambdaFunctions/README.md

+90
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ 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.
8+
9+
```
10+
git clone https://github.com/swift-server/swift-aws-lambda-runtime.git
11+
cd swift-aws-lambda-runtime/Examples/LambdaFunctions
12+
```
13+
714
Note: The example scripts assume you have [jq](https://stedolan.github.io/jq/download/) command line tool installed.
815

916
## Deployment instructions using AWS CLI
@@ -85,3 +92,86 @@ The SAM template will provide an output labelled `LambdaApiGatewayEndpoint` whic
8592
***Warning:*** This SAM template is only intended as a sample and creates a publicly accessible HTTP endpoint.
8693

8794
For all other samples use the AWS Lambda console.
95+
96+
### Deployment instructions using Serverless Framework (serverless.com)
97+
98+
[Serverless framework](https://www.serverless.com/open-source/) (Serverless) is a provider agnostic, open-source framework for building serverless applications. This framework allows you to easily deploy other AWS resources and more complex deployment mechanisms such a CI pipelines. Serverless Framework offers solutions for not only deploying but also testing, monitoring, alerting, and security and is widely adopted by the industry and offers along the open-source version a paid one.
99+
100+
***Note:*** Deploying using Serverless will automatically create resources within your AWS account. Charges may apply for these resources.
101+
102+
To use Serverless to deploy this sample to AWS:
103+
104+
1. Install the AWS CLI by following the [instructions](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html).
105+
106+
2. Install Serverless by following the [instructions](https://www.serverless.com/framework/docs/getting-started/).
107+
If you already have installed be sure you have the latest version.
108+
The examples have been tested with the version 1.72.0.
109+
110+
```
111+
Serverless --version
112+
Framework Core: 1.72.0 (standalone)
113+
Plugin: 3.6.13
114+
SDK: 2.3.1
115+
Components: 2.30.12
116+
```
117+
118+
3. Build, package and deploy the Lambda
119+
120+
```
121+
./scripts/serverless-deploy.sh
122+
```
123+
124+
The script will ask you which sample Lambda you wish to deploy.
125+
126+
The `serverless-deploy.sh` script passes through any parameters to the Serverless deploy command.
127+
128+
4. Testing
129+
130+
For the APIGateway sample:
131+
132+
The Serverless template will provide an endpoint which you can use to test the Lambda.
133+
134+
Outuput example:
135+
136+
```
137+
...
138+
...
139+
Serverless: Stack update finished...
140+
Service Information
141+
service: apigateway-swift-aws
142+
stage: dev
143+
region: us-east-1
144+
stack: apigateway-swift-aws-dev
145+
resources: 12
146+
api keys:
147+
None
148+
endpoints:
149+
GET - https://r39lvhfng3.execute-api.us-east-1.amazonaws.com/api
150+
functions:
151+
httpGet: apigateway-swift-aws-dev-httpGet
152+
layers:
153+
None
154+
155+
Stack Outputs
156+
HttpGetLambdaFunctionQualifiedArn: arn:aws:lambda:us-east-1:XXXXXXXXX:function:apigateway-swift-aws-dev-httpGet:1
157+
ServerlessDeploymentBucketName: apigateway-swift-aws-dev-serverlessdeploymentbuck-ud51msgcrj1e
158+
HttpApiUrl: https://r39lvhfng3.execute-api.us-east-1.amazonaws.com
159+
```
160+
161+
For example:
162+
163+
```
164+
curl https://r39lvhfng3.execute-api.us-east-1.amazonaws.com/api
165+
```
166+
167+
***Warning:*** This Serverless template is only intended as a sample and creates a publicly accessible HTTP endpoint.
168+
169+
For all other samples use the AWS Lambda console.
170+
171+
4. Remove
172+
173+
```
174+
./scripts/serverless-remove.sh
175+
```
176+
177+
The script will ask you which sample Lambda you wish to remove from the previous depolyment.

Examples/LambdaFunctions/scripts/build-and-package.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
set -eu
1717

1818
executable=$1
19+
workspace="$(pwd)/../.."
1920

2021
echo "-------------------------------------------------------------------------"
2122
echo "building \"$executable\" lambda"
2223
echo "-------------------------------------------------------------------------"
23-
docker run --rm -v "$(pwd)/../..":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "swift build --product $executable -c release -Xswiftc -g"
24+
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "swift build --product $executable -c release -Xswiftc -g"
2425
echo "done"
2526

2627
echo "-------------------------------------------------------------------------"
2728
echo "packaging \"$executable\" lambda"
2829
echo "-------------------------------------------------------------------------"
29-
docker run --rm -v "$(pwd)/../..":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "./scripts/package.sh $executable"
30+
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "./scripts/package.sh $executable"
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash
2+
##===----------------------------------------------------------------------===##
3+
##
4+
## This source file is part of the SwiftAWSLambdaRuntime open source project
5+
##
6+
## Copyright (c) 2017-2018 Apple Inc. and the SwiftAWSLambdaRuntime project authors
7+
## Licensed under Apache License v2.0
8+
##
9+
## See LICENSE.txt for license information
10+
## See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
11+
##
12+
## SPDX-License-Identifier: Apache-2.0
13+
##
14+
##===----------------------------------------------------------------------===##
15+
16+
DIR="$(cd "$(dirname "$0")" && pwd)"
17+
18+
executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') )
19+
20+
if [[ ${#executables[@]} = 0 ]]; then
21+
echo "no executables found"
22+
exit 1
23+
elif [[ ${#executables[@]} = 1 ]]; then
24+
executable=${executables[0]}
25+
elif [[ ${#executables[@]} > 1 ]]; then
26+
echo "multiple executables found:"
27+
for executable in ${executables[@]}; do
28+
echo " * $executable"
29+
done
30+
echo ""
31+
read -p "select which executables to deploy: " executable
32+
fi
33+
34+
echo "-------------------------------------------------------------------------"
35+
echo "CONFIG"
36+
echo "-------------------------------------------------------------------------"
37+
echo "DIR: $DIR"
38+
echo "executable: $executable"
39+
echo "-------------------------------------------------------------------------"

Examples/LambdaFunctions/scripts/deploy.sh

+4-15
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,10 @@ lambda_name=SwiftSample
2121
# S3 bucket name to upload zip file (must exist in AWS S3)
2222
s3_bucket=swift-lambda-test
2323

24-
executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') )
24+
workspace="$(pwd)/../.."
2525

26-
if [[ ${#executables[@]} = 0 ]]; then
27-
echo "no executables found"
28-
exit 1
29-
elif [[ ${#executables[@]} = 1 ]]; then
30-
executable=${executables[0]}
31-
elif [[ ${#executables[@]} > 1 ]]; then
32-
echo "multiple executables found:"
33-
for executable in ${executables[@]}; do
34-
echo " * $executable"
35-
done
36-
echo ""
37-
read -p "select which executables to deploy: " executable
38-
fi
26+
DIR="$(cd "$(dirname "$0")" && pwd)"
27+
source $DIR/config.sh
3928

4029
echo -e "\ndeploying $executable"
4130

@@ -47,7 +36,7 @@ docker build . -t builder
4736
echo "-------------------------------------------------------------------------"
4837
echo "building \"$executable\" lambda"
4938
echo "-------------------------------------------------------------------------"
50-
docker run --rm -v `pwd`/../..:/workspace -w /workspace builder \
39+
docker run --rm -v $workspace:/workspace -w /workspace builder \
5140
bash -cl "cd Examples/LambdaFunctions && \
5241
swift build --product $executable -c release -Xswiftc -g"
5342
echo "done"

Examples/LambdaFunctions/scripts/sam-deploy.sh

+2-17
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,7 @@
1414
##===----------------------------------------------------------------------===##
1515

1616
DIR="$(cd "$(dirname "$0")" && pwd)"
17-
18-
executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') )
19-
20-
if [[ ${#executables[@]} = 0 ]]; then
21-
echo "no executables found"
22-
exit 1
23-
elif [[ ${#executables[@]} = 1 ]]; then
24-
executable=${executables[0]}
25-
elif [[ ${#executables[@]} > 1 ]]; then
26-
echo "multiple executables found:"
27-
for executable in ${executables[@]}; do
28-
echo " * $executable"
29-
done
30-
echo ""
31-
read -p "select which executables to deploy: " executable
32-
fi
17+
source $DIR/config.sh
3318

3419
echo -e "\ndeploying $executable"
3520

@@ -44,4 +29,4 @@ echo "-------------------------------------------------------------------------"
4429
echo "deploying using SAM"
4530
echo "-------------------------------------------------------------------------"
4631

47-
sam deploy --template "${executable}-template.yml" $@
32+
sam deploy --template "./scripts/SAM/${executable}-template.yml" $@
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/bash
2+
##===----------------------------------------------------------------------===##
3+
##
4+
## This source file is part of the SwiftAWSLambdaRuntime open source project
5+
##
6+
## Copyright (c) 2017-2018 Apple Inc. and the SwiftAWSLambdaRuntime project authors
7+
## Licensed under Apache License v2.0
8+
##
9+
## See LICENSE.txt for license information
10+
## See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
11+
##
12+
## SPDX-License-Identifier: Apache-2.0
13+
##
14+
##===----------------------------------------------------------------------===##
15+
16+
DIR="$(cd "$(dirname "$0")" && pwd)"
17+
source $DIR/config.sh
18+
19+
echo -e "\ndeploying $executable"
20+
21+
echo "-------------------------------------------------------------------------"
22+
echo "preparing docker build image"
23+
echo "-------------------------------------------------------------------------"
24+
docker build . -q -t builder
25+
26+
$DIR/build-and-package.sh ${executable}
27+
28+
echo "-------------------------------------------------------------------------"
29+
echo "deploying using Serverless"
30+
echo "-------------------------------------------------------------------------"
31+
32+
serverless deploy --config "./scripts/serverless/${executable}-template.yml" --stage dev -v
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
##===----------------------------------------------------------------------===##
3+
##
4+
## This source file is part of the SwiftAWSLambdaRuntime open source project
5+
##
6+
## Copyright (c) 2017-2018 Apple Inc. and the SwiftAWSLambdaRuntime project authors
7+
## Licensed under Apache License v2.0
8+
##
9+
## See LICENSE.txt for license information
10+
## See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
11+
##
12+
## SPDX-License-Identifier: Apache-2.0
13+
##
14+
##===----------------------------------------------------------------------===##
15+
16+
DIR="$(cd "$(dirname "$0")" && pwd)"
17+
source $DIR/config.sh
18+
19+
echo -e "\nremoving $executable"
20+
21+
echo "-------------------------------------------------------------------------"
22+
echo "removing using Serverless"
23+
echo "-------------------------------------------------------------------------"
24+
25+
serverless remove --config "./scripts/serverless/${executable}-template.yml" --stage dev -v
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
service: apigateway-swift-aws
2+
3+
package:
4+
artifact: .build/lambda/APIGateway/lambda.zip
5+
6+
provider:
7+
name: aws
8+
httpApi:
9+
payload: '2.0'
10+
runtime: provided
11+
logs:
12+
httpApi: true
13+
iamRoleStatements:
14+
- Effect: Allow
15+
Action:
16+
- logs:CreateLogGroup
17+
- logs:CreateLogStream
18+
- logs:PutLogEvents
19+
Resource: "*"
20+
21+
functions:
22+
httpGet:
23+
handler: APIGateway
24+
memorySize: 128
25+
events:
26+
- httpApi:
27+
method: GET
28+
path: /api
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
service: benchmark-swift-aws
2+
3+
package:
4+
artifact: .build/lambda/Benchmark/lambda.zip
5+
6+
provider:
7+
name: aws
8+
runtime: provided
9+
iamRoleStatements:
10+
- Effect: Allow
11+
Action:
12+
- logs:CreateLogGroup
13+
- logs:CreateLogStream
14+
- logs:PutLogEvents
15+
Resource: "*"
16+
17+
functions:
18+
benchmarkFunction:
19+
handler: Benchmark
20+
memorySize: 128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
service: currency-swift-aws
2+
3+
package:
4+
artifact: .build/lambda/CurrencyExchange/lambda.zip
5+
6+
provider:
7+
name: aws
8+
runtime: provided
9+
iamRoleStatements:
10+
- Effect: Allow
11+
Action:
12+
- logs:CreateLogGroup
13+
- logs:CreateLogStream
14+
- logs:PutLogEvents
15+
Resource: "*"
16+
17+
functions:
18+
currencyExchangeFunction:
19+
handler: CurrencyExchange
20+
memorySize: 128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
service: errorhandling-swift-aws
2+
3+
package:
4+
artifact: .build/lambda/ErrorHandling/lambda.zip
5+
6+
provider:
7+
name: aws
8+
runtime: provided
9+
iamRoleStatements:
10+
- Effect: Allow
11+
Action:
12+
- logs:CreateLogGroup
13+
- logs:CreateLogStream
14+
- logs:PutLogEvents
15+
Resource: "*"
16+
17+
functions:
18+
errorHandlingFunction:
19+
handler: ErrorHandling
20+
memorySize: 128

0 commit comments

Comments
 (0)