Skip to content

Commit 7d2c832

Browse files
Add example scripts and docs for Serverless Framework
1 parent 5e546a1 commit 7d2c832

File tree

9 files changed

+276
-0
lines changed

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
@@ -81,3 +81,81 @@ The SAM template will provide an output labelled `LambdaApiGatewayEndpoint` whic
8181
***Warning:*** This SAM template is only intended as a sample and creates a publicly accessible HTTP endpoint.
8282

8383
For all other samples use the AWS Lambda console.
84+
85+
### Deployment instructions using AWS Serverless Framework
86+
87+
[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.
88+
89+
***Note:*** Deploying using Serverless will automatically create resources within your AWS account. Charges may apply for these resources.
90+
91+
To use Serverless to deploy this sample to AWS:
92+
93+
1. Install the AWS CLI by following the [instructions](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html).
94+
95+
2. Install Serverless by following the [instructions](https://www.serverless.com/framework/docs/getting-started/).
96+
If you already have installed be sure you have the latest version.
97+
The examples have been tested with the version 1.72.0.
98+
99+
```
100+
Serverless --version
101+
Framework Core: 1.72.0 (standalone)
102+
Plugin: 3.6.13
103+
SDK: 2.3.1
104+
Components: 2.30.12
105+
```
106+
107+
3. Build, package and deploy the Lambda
108+
109+
```
110+
./scripts/serverless-deploy.sh
111+
```
112+
113+
The script will ask you which sample Lambda you wish to deploy.
114+
115+
The `serverless-deploy.sh` script passes through any parameters to the Serverless deploy command.
116+
117+
4. Testing
118+
119+
For the HelloWorld and APIGateway sample:
120+
121+
The Serverless template will provide an endpoint which you can use to test the Lambda.
122+
123+
Outuput example:
124+
125+
```
126+
...
127+
...
128+
Serverless: Stack update finished...
129+
Service Information
130+
service: helloworld-swift-aws
131+
stage: dev
132+
region: us-east-1
133+
stack: helloworld-swift-aws-dev
134+
resources: 11
135+
api keys:
136+
None
137+
endpoints:
138+
GET - https://jm3b9p4bu2.execute-api.us-east-1.amazonaws.com/dev/hello
139+
functions:
140+
hello: helloworld-swift-aws-dev-hello
141+
layers:
142+
None
143+
```
144+
145+
For example:
146+
147+
```
148+
curl https://jm3b9p4bu2.execute-api.us-east-1.amazonaws.com/dev/hello
149+
```
150+
151+
***Warning:*** This Serverless template is only intended as a sample and creates a publicly accessible HTTP endpoint.
152+
153+
For all other samples use the AWS Lambda console.
154+
155+
4. Remove
156+
157+
```
158+
./scripts/serverless-remove.sh
159+
```
160+
161+
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)