Skip to content

Commit 5f1fb8a

Browse files
Refactor Examples folder and scripts
- Move scripts to Examples - Add a common config.sh script - Move SAM templates to Examples/scripts/SAM - Move Serverless templates to Examples/scripts/serverless - Update the scripts to work with new folder structure and config.sh - Move Dockerfile to Examples
1 parent 6345332 commit 5f1fb8a

21 files changed

+73
-73
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
File renamed without changes.

Examples/LambdaFunctions/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

Examples/LambdaFunctions/README.md

Lines changed: 7 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 `Examples` folder.
8+
9+
```
10+
git clone https://github.com/swift-server/swift-aws-lambda-runtime.git
11+
cd swift-aws-lambda-runtime/Examples
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

Examples/LambdaFunctions/APIGateway-template.yml renamed to Examples/scripts/SAM/APIGateway-template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Resources:
99
Properties:
1010
Handler: Provided
1111
Runtime: provided
12-
CodeUri: .build/lambda/APIGateway/lambda.zip
12+
CodeUri: LambdaFunctions/.build/lambda/APIGateway/lambda.zip
1313
# Add an API Gateway event source for the Lambda
1414
Events:
1515
HttpGet:

Examples/LambdaFunctions/Benchmark-template.yml renamed to Examples/scripts/SAM/Benchmark-template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ Resources:
99
Properties:
1010
Handler: Provided
1111
Runtime: provided
12-
CodeUri: .build/lambda/Benchmark/lambda.zip
12+
CodeUri: LambdaFunctions/.build/lambda/Benchmark/lambda.zip
1313
# Instructs new versions to be published to an alias named "live".
1414
AutoPublishAlias: live

Examples/LambdaFunctions/CurrencyExchange-template.yml renamed to Examples/scripts/SAM/CurrencyExchange-template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Resources:
99
Properties:
1010
Handler: Provided
1111
Runtime: provided
12-
CodeUri: .build/lambda/CurrencyExchange/lambda.zip
12+
CodeUri: LambdaFunctions/.build/lambda/CurrencyExchange/lambda.zip
1313
Timeout: 300
1414
# Instructs new versions to be published to an alias named "live".
1515
AutoPublishAlias: live

Examples/LambdaFunctions/ErrorHandling-template.yml renamed to Examples/scripts/SAM/ErrorHandling-template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ Resources:
99
Properties:
1010
Handler: Provided
1111
Runtime: provided
12-
CodeUri: .build/lambda/ErrorHandling/lambda.zip
12+
CodeUri: LambdaFunctions/.build/lambda/ErrorHandling/lambda.zip
1313
# Instructs new versions to be published to an alias named "live".
1414
AutoPublishAlias: live

Examples/LambdaFunctions/HelloWorld-template.yml renamed to Examples/scripts/SAM/HelloWorld-template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ Resources:
99
Properties:
1010
Handler: Provided
1111
Runtime: provided
12-
CodeUri: .build/lambda/HelloWorld/lambda.zip
12+
CodeUri: LambdaFunctions/.build/lambda/HelloWorld/lambda.zip
1313
# Instructs new versions to be published to an alias named "live".
1414
AutoPublishAlias: live

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@
1616
set -eu
1717

1818
executable=$1
19+
workspace="$(pwd)/.."
20+
sources=$2
1921

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

2628
echo "-------------------------------------------------------------------------"
2729
echo "packaging \"$executable\" lambda"
2830
echo "-------------------------------------------------------------------------"
29-
docker run --rm -v "$(pwd)/../..":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "./scripts/package.sh $executable"
31+
docker run --rm -v "$workspace":/workspace -w /workspace/Examples builder bash -cl "./scripts/package.sh $executable $sources"

Examples/LambdaFunctions/scripts/serverless-remove.sh renamed to Examples/scripts/config.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

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

18+
sources="LambdaFunctions"
19+
20+
cd $sources
1821
executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') )
22+
cd ..
1923

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

34-
echo -e "\nremoving $executable"
35-
3638
echo "-------------------------------------------------------------------------"
37-
echo "removing using Serverless"
39+
echo "CONFIG"
3840
echo "-------------------------------------------------------------------------"
39-
40-
serverless remove --config "serverless/${executable}-template.yml" --stage dev -v
41+
echo "DIR: $DIR"
42+
echo "sources: $sources"
43+
echo "executable: $executable"
44+
echo "-------------------------------------------------------------------------"

Examples/LambdaFunctions/scripts/deploy.sh renamed to Examples/scripts/deploy.sh

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,12 @@ 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+
sources="LambdaFunctions"
27+
28+
DIR="$(cd "$(dirname "$0")" && pwd)"
29+
source $DIR/config.sh
3930

4031
echo -e "\ndeploying $executable"
4132

@@ -47,21 +38,21 @@ docker build . -t builder
4738
echo "-------------------------------------------------------------------------"
4839
echo "building \"$executable\" lambda"
4940
echo "-------------------------------------------------------------------------"
50-
docker run --rm -v `pwd`/../..:/workspace -w /workspace builder \
41+
docker run --rm -v $workspace:/workspace -w /workspace builder \
5142
bash -cl "cd Examples/LambdaFunctions && \
5243
swift build --product $executable -c release -Xswiftc -g"
5344
echo "done"
5445

5546
echo "-------------------------------------------------------------------------"
5647
echo "packaging \"$executable\" lambda"
5748
echo "-------------------------------------------------------------------------"
58-
docker run --rm -v `pwd`:/workspace -w /workspace builder bash -cl "./scripts/package.sh $executable"
49+
docker run --rm -v "$workspace/Examples":/workspace -w /workspace builder bash -cl "./scripts/package.sh $executable $sources"
5950

6051
echo "-------------------------------------------------------------------------"
6152
echo "uploading \"$executable\" lambda to s3"
6253
echo "-------------------------------------------------------------------------"
6354

64-
aws s3 cp .build/lambda/$executable/lambda.zip s3://$s3_bucket/
55+
aws s3 cp $sources/.build/lambda/$executable/lambda.zip s3://$s3_bucket/
6556

6657
echo "-------------------------------------------------------------------------"
6758
echo "updating \"$lambda_name\" to latest \"$executable\""

Examples/LambdaFunctions/scripts/package.sh renamed to Examples/scripts/package.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
set -eu
1717

1818
executable=$1
19+
sources=$2
1920

20-
target=.build/lambda/$executable
21+
target=$sources/.build/lambda/$executable
2122
rm -rf "$target"
2223
mkdir -p "$target"
23-
cp ".build/release/$executable" "$target/"
24+
cp "$sources/.build/release/$executable" "$target/"
2425
cp -Pv /usr/lib/swift/linux/lib*so* "$target"
2526
cd "$target"
2627
ln -s "$executable" "bootstrap"

Examples/LambdaFunctions/scripts/sam-deploy.sh renamed to Examples/scripts/sam-deploy.sh

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,7 @@
1414
##===----------------------------------------------------------------------===##
1515

1616
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
17+
source $DIR/config.sh
3318

3419
echo -e "\ndeploying $executable"
3520

@@ -38,10 +23,10 @@ echo "preparing docker build image"
3823
echo "-------------------------------------------------------------------------"
3924
docker build . -q -t builder
4025

41-
$DIR/build-and-package.sh ${executable}
26+
$DIR/build-and-package.sh ${executable} ${sources}
4227

4328
echo "-------------------------------------------------------------------------"
4429
echo "deploying using SAM"
4530
echo "-------------------------------------------------------------------------"
4631

47-
sam deploy --template "${executable}-template.yml" $@
32+
sam deploy --template "./scripts/SAM/${executable}-template.yml" $@

Examples/LambdaFunctions/scripts/serverless-deploy.sh renamed to Examples/scripts/serverless-deploy.sh

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,7 @@
1414
##===----------------------------------------------------------------------===##
1515

1616
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
17+
source $DIR/config.sh
3318

3419
echo -e "\ndeploying $executable"
3520

@@ -38,10 +23,10 @@ echo "preparing docker build image"
3823
echo "-------------------------------------------------------------------------"
3924
docker build . -q -t builder
4025

41-
$DIR/build-and-package.sh ${executable}
26+
$DIR/build-and-package.sh ${executable} ${sources}
4227

4328
echo "-------------------------------------------------------------------------"
4429
echo "deploying using Serverless"
4530
echo "-------------------------------------------------------------------------"
4631

47-
serverless deploy --config "serverless/${executable}-template.yml" --stage dev -v
32+
serverless deploy --config "./scripts/serverless/${executable}-template.yml" --stage dev -v

Examples/scripts/serverless-remove.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
source $DIR/config.sh
18+
19+
echo -e "\nremoving $executable"
20+
21+
echo "-------------------------------------------------------------------------"
22+
echo "removing using Serverless"
23+
echo "-------------------------------------------------------------------------"
24+
25+
serverless remove --config "./scripts/serverless/${executable}-template.yml" --stage dev -v

Examples/LambdaFunctions/serverless/APIGateway-template.yml renamed to Examples/scripts/serverless/APIGateway-template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
service: apigateway-swift-aws
22

33
package:
4-
artifact: .build/lambda/APIGateway/lambda.zip
4+
artifact: LambdaFunctions/.build/lambda/APIGateway/lambda.zip
55

66
provider:
77
name: aws

Examples/LambdaFunctions/serverless/Benchmark-template.yml renamed to Examples/scripts/serverless/Benchmark-template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
service: benchmark-swift-aws
22

33
package:
4-
artifact: .build/lambda/Benchmark/lambda.zip
4+
artifact: LambdaFunctions/.build/lambda/Benchmark/lambda.zip
55

66
provider:
77
name: aws

Examples/LambdaFunctions/serverless/CurrencyExchange-template.yml renamed to Examples/scripts/serverless/CurrencyExchange-template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
service: currency-swift-aws
22

33
package:
4-
artifact: .build/lambda/CurrencyExchange/lambda.zip
4+
artifact: LambdaFunctions/.build/lambda/CurrencyExchange/lambda.zip
55

66
provider:
77
name: aws

Examples/LambdaFunctions/serverless/ErrorHandling-template.yml renamed to Examples/scripts/serverless/ErrorHandling-template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
service: errorhandling-swift-aws
22

33
package:
4-
artifact: .build/lambda/ErrorHandling/lambda.zip
4+
artifact: LambdaFunctions/.build/lambda/ErrorHandling/lambda.zip
55

66
provider:
77
name: aws

Examples/LambdaFunctions/serverless/HelloWorld-template.yml renamed to Examples/scripts/serverless/HelloWorld-template.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
service: helloworld-swift-aws
22

33
package:
4-
artifact: .build/lambda/HelloWorld/lambda.zip
4+
artifact: LambdaFunctions/.build/lambda/HelloWorld/lambda.zip
55

66
provider:
77
name: aws

0 commit comments

Comments
 (0)