Skip to content

Add example scripts and docs for Serverless Framework #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
.swift-version
xcuserdata
Package.resolved
.serverless
File renamed without changes.
90 changes: 90 additions & 0 deletions Examples/LambdaFunctions/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ This sample project is a collection of Lambda functions that demonstrates
how to write a simple Lambda function in Swift, and how to package and deploy it
to the AWS Lambda platform.

The scripts are prepared to work from the `Examples` folder.

```
git clone https://github.com/swift-server/swift-aws-lambda-runtime.git
cd swift-aws-lambda-runtime/Examples
```

Note: The example scripts assume you have [jq](https://stedolan.github.io/jq/download/) command line tool installed.

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

For all other samples use the AWS Lambda console.

### Deployment instructions using Serverless Framework (serverless.com)

[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.

***Note:*** Deploying using Serverless will automatically create resources within your AWS account. Charges may apply for these resources.

To use Serverless to deploy this sample to AWS:

1. Install the AWS CLI by following the [instructions](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html).

2. Install Serverless by following the [instructions](https://www.serverless.com/framework/docs/getting-started/).
If you already have installed be sure you have the latest version.
The examples have been tested with the version 1.72.0.

```
Serverless --version
Framework Core: 1.72.0 (standalone)
Plugin: 3.6.13
SDK: 2.3.1
Components: 2.30.12
```

3. Build, package and deploy the Lambda

```
./scripts/serverless-deploy.sh
```

The script will ask you which sample Lambda you wish to deploy.

The `serverless-deploy.sh` script passes through any parameters to the Serverless deploy command.

4. Testing

For the APIGateway sample:

The Serverless template will provide an endpoint which you can use to test the Lambda.

Outuput example:

```
...
...
Serverless: Stack update finished...
Service Information
service: apigateway-swift-aws
stage: dev
region: us-east-1
stack: apigateway-swift-aws-dev
resources: 12
api keys:
None
endpoints:
GET - https://r39lvhfng3.execute-api.us-east-1.amazonaws.com/api
functions:
httpGet: apigateway-swift-aws-dev-httpGet
layers:
None

Stack Outputs
HttpGetLambdaFunctionQualifiedArn: arn:aws:lambda:us-east-1:XXXXXXXXX:function:apigateway-swift-aws-dev-httpGet:1
ServerlessDeploymentBucketName: apigateway-swift-aws-dev-serverlessdeploymentbuck-ud51msgcrj1e
HttpApiUrl: https://r39lvhfng3.execute-api.us-east-1.amazonaws.com
```

For example:

```
curl https://r39lvhfng3.execute-api.us-east-1.amazonaws.com/api
```

***Warning:*** This Serverless template is only intended as a sample and creates a publicly accessible HTTP endpoint.

For all other samples use the AWS Lambda console.

4. Remove

```
./scripts/serverless-remove.sh
```

The script will ask you which sample Lambda you wish to remove from the previous depolyment.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Resources:
Properties:
Handler: Provided
Runtime: provided
CodeUri: .build/lambda/APIGateway/lambda.zip
CodeUri: LambdaFunctions/.build/lambda/APIGateway/lambda.zip
# Add an API Gateway event source for the Lambda
Events:
HttpGet:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ Resources:
Properties:
Handler: Provided
Runtime: provided
CodeUri: .build/lambda/Benchmark/lambda.zip
CodeUri: LambdaFunctions/.build/lambda/Benchmark/lambda.zip
# Instructs new versions to be published to an alias named "live".
AutoPublishAlias: live
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Resources:
Properties:
Handler: Provided
Runtime: provided
CodeUri: .build/lambda/CurrencyExchange/lambda.zip
CodeUri: LambdaFunctions/.build/lambda/CurrencyExchange/lambda.zip
Timeout: 300
# Instructs new versions to be published to an alias named "live".
AutoPublishAlias: live
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ Resources:
Properties:
Handler: Provided
Runtime: provided
CodeUri: .build/lambda/ErrorHandling/lambda.zip
CodeUri: LambdaFunctions/.build/lambda/ErrorHandling/lambda.zip
# Instructs new versions to be published to an alias named "live".
AutoPublishAlias: live
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ Resources:
Properties:
Handler: Provided
Runtime: provided
CodeUri: .build/lambda/HelloWorld/lambda.zip
CodeUri: LambdaFunctions/.build/lambda/HelloWorld/lambda.zip
# Instructs new versions to be published to an alias named "live".
AutoPublishAlias: live
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
set -eu

executable=$1
workspace="$(pwd)/.."
sources=$2

echo "-------------------------------------------------------------------------"
echo "building \"$executable\" lambda"
echo "-------------------------------------------------------------------------"
docker run --rm -v "$(pwd)/../..":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "swift build --product $executable -c release -Xswiftc -g"
docker run --rm -v "$workspace":/workspace -w /workspace/Examples/$sources builder bash -cl "swift build --product $executable -c release -Xswiftc -g"
echo "done"

echo "-------------------------------------------------------------------------"
echo "packaging \"$executable\" lambda"
echo "-------------------------------------------------------------------------"
docker run --rm -v "$(pwd)/../..":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "./scripts/package.sh $executable"
docker run --rm -v "$workspace":/workspace -w /workspace/Examples builder bash -cl "./scripts/package.sh $executable $sources"
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

DIR="$(cd "$(dirname "$0")" && pwd)"

sources="LambdaFunctions"

cd $sources
executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') )
cd ..

if [[ ${#executables[@]} = 0 ]]; then
echo "no executables found"
Expand All @@ -31,17 +35,10 @@ elif [[ ${#executables[@]} > 1 ]]; then
read -p "select which executables to deploy: " executable
fi

echo -e "\ndeploying $executable"

echo "-------------------------------------------------------------------------"
echo "preparing docker build image"
echo "CONFIG"
echo "-------------------------------------------------------------------------"
docker build . -q -t builder

$DIR/build-and-package.sh ${executable}

echo "-------------------------------------------------------------------------"
echo "deploying using SAM"
echo "-------------------------------------------------------------------------"

sam deploy --template "${executable}-template.yml" $@
echo "DIR: $DIR"
echo "sources: $sources"
echo "executable: $executable"
echo "-------------------------------------------------------------------------"
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,12 @@ lambda_name=SwiftSample
# S3 bucket name to upload zip file (must exist in AWS S3)
s3_bucket=swift-lambda-test

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

if [[ ${#executables[@]} = 0 ]]; then
echo "no executables found"
exit 1
elif [[ ${#executables[@]} = 1 ]]; then
executable=${executables[0]}
elif [[ ${#executables[@]} > 1 ]]; then
echo "multiple executables found:"
for executable in ${executables[@]}; do
echo " * $executable"
done
echo ""
read -p "select which executables to deploy: " executable
fi
sources="LambdaFunctions"

DIR="$(cd "$(dirname "$0")" && pwd)"
source $DIR/config.sh

echo -e "\ndeploying $executable"

Expand All @@ -47,21 +38,21 @@ docker build . -t builder
echo "-------------------------------------------------------------------------"
echo "building \"$executable\" lambda"
echo "-------------------------------------------------------------------------"
docker run --rm -v `pwd`/../..:/workspace -w /workspace builder \
docker run --rm -v $workspace:/workspace -w /workspace builder \
bash -cl "cd Examples/LambdaFunctions && \
swift build --product $executable -c release -Xswiftc -g"
echo "done"

echo "-------------------------------------------------------------------------"
echo "packaging \"$executable\" lambda"
echo "-------------------------------------------------------------------------"
docker run --rm -v `pwd`:/workspace -w /workspace builder bash -cl "./scripts/package.sh $executable"
docker run --rm -v "$workspace/Examples":/workspace -w /workspace builder bash -cl "./scripts/package.sh $executable $sources"

echo "-------------------------------------------------------------------------"
echo "uploading \"$executable\" lambda to s3"
echo "-------------------------------------------------------------------------"

aws s3 cp .build/lambda/$executable/lambda.zip s3://$s3_bucket/
aws s3 cp $sources/.build/lambda/$executable/lambda.zip s3://$s3_bucket/

echo "-------------------------------------------------------------------------"
echo "updating \"$lambda_name\" to latest \"$executable\""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
set -eu

executable=$1
sources=$2

target=.build/lambda/$executable
target=$sources/.build/lambda/$executable
rm -rf "$target"
mkdir -p "$target"
cp ".build/release/$executable" "$target/"
cp "$sources/.build/release/$executable" "$target/"
cp -Pv /usr/lib/swift/linux/lib*so* "$target"
cd "$target"
ln -s "$executable" "bootstrap"
Expand Down
32 changes: 32 additions & 0 deletions Examples/scripts/sam-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftAWSLambdaRuntime open source project
##
## Copyright (c) 2017-2018 Apple Inc. and the SwiftAWSLambdaRuntime project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

DIR="$(cd "$(dirname "$0")" && pwd)"
source $DIR/config.sh

echo -e "\ndeploying $executable"

echo "-------------------------------------------------------------------------"
echo "preparing docker build image"
echo "-------------------------------------------------------------------------"
docker build . -q -t builder

$DIR/build-and-package.sh ${executable} ${sources}

echo "-------------------------------------------------------------------------"
echo "deploying using SAM"
echo "-------------------------------------------------------------------------"

sam deploy --template "./scripts/SAM/${executable}-template.yml" $@
32 changes: 32 additions & 0 deletions Examples/scripts/serverless-deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftAWSLambdaRuntime open source project
##
## Copyright (c) 2017-2018 Apple Inc. and the SwiftAWSLambdaRuntime project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

DIR="$(cd "$(dirname "$0")" && pwd)"
source $DIR/config.sh

echo -e "\ndeploying $executable"

echo "-------------------------------------------------------------------------"
echo "preparing docker build image"
echo "-------------------------------------------------------------------------"
docker build . -q -t builder

$DIR/build-and-package.sh ${executable} ${sources}

echo "-------------------------------------------------------------------------"
echo "deploying using Serverless"
echo "-------------------------------------------------------------------------"

serverless deploy --config "./scripts/serverless/${executable}-template.yml" --stage dev -v
25 changes: 25 additions & 0 deletions Examples/scripts/serverless-remove.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash
##===----------------------------------------------------------------------===##
##
## This source file is part of the SwiftAWSLambdaRuntime open source project
##
## Copyright (c) 2017-2018 Apple Inc. and the SwiftAWSLambdaRuntime project authors
## Licensed under Apache License v2.0
##
## See LICENSE.txt for license information
## See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
##
## SPDX-License-Identifier: Apache-2.0
##
##===----------------------------------------------------------------------===##

DIR="$(cd "$(dirname "$0")" && pwd)"
source $DIR/config.sh

echo -e "\nremoving $executable"

echo "-------------------------------------------------------------------------"
echo "removing using Serverless"
echo "-------------------------------------------------------------------------"

serverless remove --config "./scripts/serverless/${executable}-template.yml" --stage dev -v
28 changes: 28 additions & 0 deletions Examples/scripts/serverless/APIGateway-template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
service: apigateway-swift-aws

package:
artifact: LambdaFunctions/.build/lambda/APIGateway/lambda.zip

provider:
name: aws
httpApi:
payload: '2.0'
runtime: provided
logs:
httpApi: true
iamRoleStatements:
- Effect: Allow
Action:
- logs:CreateLogGroup
- logs:CreateLogStream
- logs:PutLogEvents
Resource: "*"

functions:
httpGet:
handler: APIGateway
memorySize: 128
events:
- httpApi:
method: GET
path: /api
Loading