Skip to content

Commit d3c72e9

Browse files
authored
Add a SAM template for deploying the lambdas to AWS. (#2)
motivation: demonstrate how to deploy with SAM changes: * Add a SAM template for deploying the lambdas to AWS. * Updated README with instructions on how to do this.
1 parent 40d0599 commit d3c72e9

12 files changed

+232
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ Package.resolved
99
.podspecs
1010
function.zip
1111
.swiftpm
12+
samconfig.toml

APIGateway-template.yml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
AWSTemplateFormatVersion : '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: A sample SAM template for deploying Lambda functions.
4+
5+
Resources:
6+
# APIGateway Function
7+
apiGatewayFunction:
8+
Type: AWS::Serverless::Function
9+
Properties:
10+
Handler: Provided
11+
Runtime: provided
12+
CodeUri: .build/lambda/APIGateway/lambda.zip
13+
# Add an API Gateway event source for the Lambda
14+
Events:
15+
HttpGet:
16+
Type: Api
17+
Properties:
18+
RestApiId: !Ref lambdaApiGateway
19+
Path: '/samples/apig'
20+
Method: get
21+
# Instructs new versions to be published to an alias named "live".
22+
AutoPublishAlias: live
23+
24+
lambdaApiGateway:
25+
Type: AWS::Serverless::Api
26+
Properties:
27+
Name: Lambda API Gateway
28+
StageName: Beta
29+
30+
Outputs:
31+
LambdaApiGatewayEndpoint:
32+
Description: 'API Gateway endpoint URL.'
33+
Value: !Sub 'https://${lambdaApiGateway}.execute-api.${AWS::Region}.amazonaws.com/Beta/samples/apig'

Benchmark-template.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
AWSTemplateFormatVersion : '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: A sample SAM template for deploying Lambda functions.
4+
5+
Resources:
6+
# Benchmark Function
7+
benchmarkFunction:
8+
Type: AWS::Serverless::Function
9+
Properties:
10+
Handler: Provided
11+
Runtime: provided
12+
CodeUri: .build/lambda/Benchmark/lambda.zip
13+
# Instructs new versions to be published to an alias named "live".
14+
AutoPublishAlias: live

CurrencyExchange-template.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
AWSTemplateFormatVersion : '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: A sample SAM template for deploying Lambda functions.
4+
5+
Resources:
6+
# CurrencyExchange Function
7+
currencyExchangeFunction:
8+
Type: AWS::Serverless::Function
9+
Properties:
10+
Handler: Provided
11+
Runtime: provided
12+
CodeUri: .build/lambda/CurrencyExchange/lambda.zip
13+
Timeout: 300
14+
# Instructs new versions to be published to an alias named "live".
15+
AutoPublishAlias: live

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM fabianfett/amazonlinux-swift:5.2-branch-amazonlinux2
1+
FROM fabianfett/amazonlinux-swift:5.2-amazonlinux2
22

33
RUN yum -y install \
44
git \

ErrorHandling-template.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
AWSTemplateFormatVersion : '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: A sample SAM template for deploying Lambda functions.
4+
5+
Resources:
6+
# ErrorHandling Function
7+
errorHandlingFunction:
8+
Type: AWS::Serverless::Function
9+
Properties:
10+
Handler: Provided
11+
Runtime: provided
12+
CodeUri: .build/lambda/ErrorHandling/lambda.zip
13+
# Instructs new versions to be published to an alias named "live".
14+
AutoPublishAlias: live

HelloWorld-template.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
AWSTemplateFormatVersion : '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Description: A sample SAM template for deploying Lambda functions.
4+
5+
Resources:
6+
# HelloWorld Function
7+
helloWorldFunction:
8+
Type: AWS::Serverless::Function
9+
Properties:
10+
Handler: Provided
11+
Runtime: provided
12+
CodeUri: .build/lambda/HelloWorld/lambda.zip
13+
# Instructs new versions to be published to an alias named "live".
14+
AutoPublishAlias: live

README.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This repository is a deployable example demonstrating how to package and deploy
44
a Swift based Lambda to AWS.
55

6-
Steps to deploy this sample to ASW:
6+
Steps to deploy this sample to AWS:
77

88
* Login to AWS Console and create an AWS Lambda with the following settings:
99
* Runtime: Custom runtime
@@ -53,3 +53,63 @@ Steps to deploy this sample to ASW:
5353
"error": "fatal"
5454
}
5555
``
56+
57+
### Deploy to AWS using AWS Serverless Application Model
58+
59+
AWS [Serverless Application Model](https://aws.amazon.com/serverless/sam/) (SAM) is an 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.
60+
61+
***Note:*** Deploying using SAM will automatically create resources within your AWS account. Charges may apply for these resources.
62+
63+
To use SAM to deploy this sample to AWS-
64+
65+
1. Install the AWS CLI by following the instructions at https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html
66+
67+
2. Install SAM CLI by following the instructions at https://aws.amazon.com/serverless/sam/
68+
69+
3. Build, package and deploy the Lambda
70+
71+
```
72+
./scripts/sam-deploy.sh --guided
73+
```
74+
75+
The script will ask you which sample lambda you wish to deploy. It will then guide you through the SAM setup process.
76+
77+
```
78+
Setting default arguments for 'sam deploy'
79+
=========================================
80+
Stack Name [sam-app]: swift-aws-lambda-runtime-sample
81+
AWS Region [us-east-1]: <your-favourite-region>
82+
#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
83+
Confirm changes before deploy [y/N]: Y
84+
#SAM needs permission to be able to create roles to connect to the resources in your template
85+
Allow SAM CLI IAM role creation [Y/n]: Y
86+
Save arguments to samconfig.toml [Y/n]: Y
87+
```
88+
89+
If you said yes to confirm changes, SAM will ask you to accept changes to the infratructure you are setting up. For more on this, see [Cloud Formation](https://aws.amazon.com/cloudformation/).
90+
91+
The sam-deploy script passes through any parameters to the SAM deploy command.
92+
93+
4. Subsequent deploys can just use the command; minus the guided parameter-
94+
95+
```
96+
./scripts/sam-deploy.sh
97+
```
98+
99+
The script will ask you which sample lambda you wish to deploy. You are deploy a different sample lambda, the deploy process will pull down the previous lambda.
100+
101+
SAM will still ask you to confirm changes if you said yes to that initially.
102+
103+
5. Testing
104+
105+
For the API Gateway sample:
106+
107+
The SAM template will provide an output labelled `LambdaApiGatewayEndpoint` which you can use to test the Lambda. For example-
108+
109+
```
110+
curl <<LambdaApiGatewayEndpoint>>
111+
```
112+
113+
***Warning:*** This SAM template is only intended as a sample and creates a publically accessible http endpoint.
114+
115+
For all other samples use the AWS Lambda console.

scripts/build-and-package.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
##===----------------------------------------------------------------------===##
3+
##
4+
## This source file is part of the SwiftAWSLambdaRuntime open source project
5+
##
6+
## Copyright (c) 2020 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+
set -eu
17+
18+
executable=$1
19+
20+
echo "-------------------------------------------------------------------------"
21+
echo "building \"$executable\" lambda"
22+
echo "-------------------------------------------------------------------------"
23+
docker run --rm -v `pwd`:/workspace -w /workspace builder bash -cl "swift build --product $executable -c release -Xswiftc -g"
24+
echo "done"
25+
26+
echo "-------------------------------------------------------------------------"
27+
echo "packaging \"$executable\" lambda"
28+
echo "-------------------------------------------------------------------------"
29+
docker run --rm -v `pwd`:/workspace -w /workspace builder bash -cl "./scripts/package.sh $executable"

scripts/deploy.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ echo "-------------------------------------------------------------------------"
5757
echo "uploading \"$executable\" lambda to s3"
5858
echo "-------------------------------------------------------------------------"
5959

60-
aws s3 cp .build/lambda/lambda.zip s3://$s3_bucket/
60+
aws s3 cp .build/lambda/$executable/lambda.zip s3://$s3_bucket/
6161

6262
echo "-------------------------------------------------------------------------"
63-
echo "updating \"$lambda_name\" to laetst \"$executable\""
63+
echo "updating \"$lambda_name\" to latest \"$executable\""
6464
echo "-------------------------------------------------------------------------"
6565
aws lambda update-function-code --function $lambda_name --s3-bucket $s3_bucket --s3-key lambda.zip

scripts/package.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ set -eu
1818

1919
executable=$1
2020

21-
target=.build/lambda
21+
target=.build/lambda/$executable
2222
rm -rf "$target"
2323
mkdir -p "$target"
2424
cp ".build/release/$executable" "$target/"

scripts/sam-deploy.sh

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 SAM"
45+
echo "-------------------------------------------------------------------------"
46+
47+
sam deploy --template "${executable}-template.yml" $@

0 commit comments

Comments
 (0)