Skip to content

Commit 2aab0f4

Browse files
Add example scripts and docs for Serverless Framework
1 parent a5e2fd0 commit 2aab0f4

9 files changed

+276
-0
lines changed

Examples/LambdaFunctions/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.serverless

Examples/LambdaFunctions/README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,81 @@ The SAM template will provide an output labelled `LambdaApiGatewayEndpoint` whic
8585
***Warning:*** This SAM template is only intended as a sample and creates a publicly accessible HTTP endpoint.
8686

8787
For all other samples use the AWS Lambda console.
88+
89+
### Deployment instructions using AWS Serverless Framework
90+
91+
[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.
92+
93+
***Note:*** Deploying using Serverless will automatically create resources within your AWS account. Charges may apply for these resources.
94+
95+
To use Serverless to deploy this sample to AWS:
96+
97+
1. Install the AWS CLI by following the [instructions](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html).
98+
99+
2. Install Serverless by following the [instructions](https://www.serverless.com/framework/docs/getting-started/).
100+
If you already have installed be sure you have the latest version.
101+
The examples have been tested with the version 1.72.0.
102+
103+
```
104+
Serverless --version
105+
Framework Core: 1.72.0 (standalone)
106+
Plugin: 3.6.13
107+
SDK: 2.3.1
108+
Components: 2.30.12
109+
```
110+
111+
3. Build, package and deploy the Lambda
112+
113+
```
114+
./scripts/serverless-deploy.sh
115+
```
116+
117+
The script will ask you which sample Lambda you wish to deploy.
118+
119+
The `serverless-deploy.sh` script passes through any parameters to the Serverless deploy command.
120+
121+
4. Testing
122+
123+
For the HelloWorld and APIGateway sample:
124+
125+
The Serverless template will provide an endpoint which you can use to test the Lambda.
126+
127+
Outuput example:
128+
129+
```
130+
...
131+
...
132+
Serverless: Stack update finished...
133+
Service Information
134+
service: helloworld-swift-aws
135+
stage: dev
136+
region: us-east-1
137+
stack: helloworld-swift-aws-dev
138+
resources: 11
139+
api keys:
140+
None
141+
endpoints:
142+
GET - https://jm3b9p4bu2.execute-api.us-east-1.amazonaws.com/dev/hello
143+
functions:
144+
hello: helloworld-swift-aws-dev-hello
145+
layers:
146+
None
147+
```
148+
149+
For example:
150+
151+
```
152+
curl https://jm3b9p4bu2.execute-api.us-east-1.amazonaws.com/dev/hello
153+
```
154+
155+
***Warning:*** This Serverless template is only intended as a sample and creates a publicly accessible HTTP endpoint.
156+
157+
For all other samples use the AWS Lambda console.
158+
159+
4. Remove
160+
161+
```
162+
./scripts/serverless-remove.sh
163+
```
164+
165+
The script will ask you which sample Lambda you wish to remove from the previous depolyment.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 -e "\ndeploying $executable"
35+
36+
echo "-------------------------------------------------------------------------"
37+
echo "preparing docker build image"
38+
echo "-------------------------------------------------------------------------"
39+
docker build . -q -t builder
40+
41+
$DIR/build-and-package.sh ${executable}
42+
43+
echo "-------------------------------------------------------------------------"
44+
echo "deploying using Serverless"
45+
echo "-------------------------------------------------------------------------"
46+
47+
serverless deploy --config "serverless/${executable}-template.yml" --stage dev -v
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 -e "\nremoving $executable"
35+
36+
echo "-------------------------------------------------------------------------"
37+
echo "removing using Serverless"
38+
echo "-------------------------------------------------------------------------"
39+
40+
serverless remove --config "serverless/${executable}-template.yml" --stage dev -v
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
events:
25+
- httpApi:
26+
method: GET
27+
path: /api
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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
Lines changed: 20 additions & 0 deletions
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
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
service: helloworld-swift-aws
2+
3+
package:
4+
artifact: .build/lambda/HelloWorld/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+
hello:
19+
handler: HelloWorld
20+
memorySize: 128
21+
events:
22+
- http:
23+
method: GET
24+
path: /hello
25+
integration: lambda

0 commit comments

Comments
 (0)