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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 90 additions & 0 deletions
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

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

Lines changed: 3 additions & 2 deletions
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"
Lines changed: 39 additions & 0 deletions
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

Lines changed: 4 additions & 15 deletions
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"

0 commit comments

Comments
 (0)