From 2aab0f4544f5a7e6d95909bb1b18233858dc0b94 Mon Sep 17 00:00:00 2001 From: Andrea Scuderi Date: Fri, 5 Jun 2020 22:25:24 +0100 Subject: [PATCH 1/7] Add example scripts and docs for Serverless Framework --- Examples/LambdaFunctions/.gitignore | 1 + Examples/LambdaFunctions/README.md | 78 +++++++++++++++++++ .../scripts/serverless-deploy.sh | 47 +++++++++++ .../scripts/serverless-remove.sh | 40 ++++++++++ .../serverless/APIGateway-template.yml | 27 +++++++ .../serverless/Benchmark-template.yml | 19 +++++ .../serverless/CurrencyExchange-template.yml | 19 +++++ .../serverless/ErrorHandling-template.yml | 20 +++++ .../serverless/HelloWorld-template.yml | 25 ++++++ 9 files changed, 276 insertions(+) create mode 100644 Examples/LambdaFunctions/.gitignore create mode 100755 Examples/LambdaFunctions/scripts/serverless-deploy.sh create mode 100755 Examples/LambdaFunctions/scripts/serverless-remove.sh create mode 100644 Examples/LambdaFunctions/serverless/APIGateway-template.yml create mode 100644 Examples/LambdaFunctions/serverless/Benchmark-template.yml create mode 100644 Examples/LambdaFunctions/serverless/CurrencyExchange-template.yml create mode 100644 Examples/LambdaFunctions/serverless/ErrorHandling-template.yml create mode 100644 Examples/LambdaFunctions/serverless/HelloWorld-template.yml diff --git a/Examples/LambdaFunctions/.gitignore b/Examples/LambdaFunctions/.gitignore new file mode 100644 index 00000000..9461c893 --- /dev/null +++ b/Examples/LambdaFunctions/.gitignore @@ -0,0 +1 @@ +.serverless \ No newline at end of file diff --git a/Examples/LambdaFunctions/README.md b/Examples/LambdaFunctions/README.md index 9d42790f..90590727 100644 --- a/Examples/LambdaFunctions/README.md +++ b/Examples/LambdaFunctions/README.md @@ -85,3 +85,81 @@ 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 AWS Serverless Framework + +[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 HelloWorld and 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: helloworld-swift-aws +stage: dev +region: us-east-1 +stack: helloworld-swift-aws-dev +resources: 11 +api keys: + None +endpoints: + GET - https://jm3b9p4bu2.execute-api.us-east-1.amazonaws.com/dev/hello +functions: + hello: helloworld-swift-aws-dev-hello +layers: + None +``` + +For example: + + ``` + curl https://jm3b9p4bu2.execute-api.us-east-1.amazonaws.com/dev/hello + ``` + +***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. \ No newline at end of file diff --git a/Examples/LambdaFunctions/scripts/serverless-deploy.sh b/Examples/LambdaFunctions/scripts/serverless-deploy.sh new file mode 100755 index 00000000..1f9c388c --- /dev/null +++ b/Examples/LambdaFunctions/scripts/serverless-deploy.sh @@ -0,0 +1,47 @@ +#!/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)" + +executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') ) + +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 + +echo -e "\ndeploying $executable" + +echo "-------------------------------------------------------------------------" +echo "preparing docker build image" +echo "-------------------------------------------------------------------------" +docker build . -q -t builder + +$DIR/build-and-package.sh ${executable} + +echo "-------------------------------------------------------------------------" +echo "deploying using Serverless" +echo "-------------------------------------------------------------------------" + +serverless deploy --config "serverless/${executable}-template.yml" --stage dev -v \ No newline at end of file diff --git a/Examples/LambdaFunctions/scripts/serverless-remove.sh b/Examples/LambdaFunctions/scripts/serverless-remove.sh new file mode 100755 index 00000000..78f686f2 --- /dev/null +++ b/Examples/LambdaFunctions/scripts/serverless-remove.sh @@ -0,0 +1,40 @@ +#!/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)" + +executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') ) + +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 + +echo -e "\nremoving $executable" + +echo "-------------------------------------------------------------------------" +echo "removing using Serverless" +echo "-------------------------------------------------------------------------" + +serverless remove --config "serverless/${executable}-template.yml" --stage dev -v \ No newline at end of file diff --git a/Examples/LambdaFunctions/serverless/APIGateway-template.yml b/Examples/LambdaFunctions/serverless/APIGateway-template.yml new file mode 100644 index 00000000..2e358fce --- /dev/null +++ b/Examples/LambdaFunctions/serverless/APIGateway-template.yml @@ -0,0 +1,27 @@ +service: apigateway-swift-aws + +package: + artifact: .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 + events: + - httpApi: + method: GET + path: /api \ No newline at end of file diff --git a/Examples/LambdaFunctions/serverless/Benchmark-template.yml b/Examples/LambdaFunctions/serverless/Benchmark-template.yml new file mode 100644 index 00000000..e83f27b0 --- /dev/null +++ b/Examples/LambdaFunctions/serverless/Benchmark-template.yml @@ -0,0 +1,19 @@ +service: benchmark-swift-aws + +package: + artifact: .build/lambda/Benchmark/lambda.zip + +provider: + name: aws + runtime: provided + iamRoleStatements: + - Effect: Allow + Action: + - logs:CreateLogGroup + - logs:CreateLogStream + - logs:PutLogEvents + Resource: "*" + +functions: + benchmarkFunction: + handler: Benchmark \ No newline at end of file diff --git a/Examples/LambdaFunctions/serverless/CurrencyExchange-template.yml b/Examples/LambdaFunctions/serverless/CurrencyExchange-template.yml new file mode 100644 index 00000000..2f853514 --- /dev/null +++ b/Examples/LambdaFunctions/serverless/CurrencyExchange-template.yml @@ -0,0 +1,19 @@ +service: currency-swift-aws + +package: + artifact: .build/lambda/CurrencyExchange/lambda.zip + +provider: + name: aws + runtime: provided + iamRoleStatements: + - Effect: Allow + Action: + - logs:CreateLogGroup + - logs:CreateLogStream + - logs:PutLogEvents + Resource: "*" + +functions: + currencyExchangeFunction: + handler: CurrencyExchange \ No newline at end of file diff --git a/Examples/LambdaFunctions/serverless/ErrorHandling-template.yml b/Examples/LambdaFunctions/serverless/ErrorHandling-template.yml new file mode 100644 index 00000000..367be490 --- /dev/null +++ b/Examples/LambdaFunctions/serverless/ErrorHandling-template.yml @@ -0,0 +1,20 @@ +service: errorhandling-swift-aws + +package: + artifact: .build/lambda/ErrorHandling/lambda.zip + +provider: + name: aws + runtime: provided + iamRoleStatements: + - Effect: Allow + Action: + - logs:CreateLogGroup + - logs:CreateLogStream + - logs:PutLogEvents + Resource: "*" + +functions: + errorHandlingFunction: + handler: ErrorHandling + memorySize: 128 \ No newline at end of file diff --git a/Examples/LambdaFunctions/serverless/HelloWorld-template.yml b/Examples/LambdaFunctions/serverless/HelloWorld-template.yml new file mode 100644 index 00000000..00c97a41 --- /dev/null +++ b/Examples/LambdaFunctions/serverless/HelloWorld-template.yml @@ -0,0 +1,25 @@ +service: helloworld-swift-aws + +package: + artifact: .build/lambda/HelloWorld/lambda.zip + +provider: + name: aws + runtime: provided + iamRoleStatements: + - Effect: Allow + Action: + - logs:CreateLogGroup + - logs:CreateLogStream + - logs:PutLogEvents + Resource: "*" + +functions: + hello: + handler: HelloWorld + memorySize: 128 + events: + - http: + method: GET + path: /hello + integration: lambda \ No newline at end of file From 724bd2cab8ff7c5b7b65eec9202085dceb1a062b Mon Sep 17 00:00:00 2001 From: Andrea Scuderi Date: Sat, 6 Jun 2020 11:24:40 +0100 Subject: [PATCH 2/7] Add memorySize: 128 to all Serverless Examples --- Examples/LambdaFunctions/serverless/APIGateway-template.yml | 1 + Examples/LambdaFunctions/serverless/Benchmark-template.yml | 3 ++- .../LambdaFunctions/serverless/CurrencyExchange-template.yml | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Examples/LambdaFunctions/serverless/APIGateway-template.yml b/Examples/LambdaFunctions/serverless/APIGateway-template.yml index 2e358fce..6787ad0f 100644 --- a/Examples/LambdaFunctions/serverless/APIGateway-template.yml +++ b/Examples/LambdaFunctions/serverless/APIGateway-template.yml @@ -21,6 +21,7 @@ provider: functions: httpGet: handler: APIGateway + memorySize: 128 events: - httpApi: method: GET diff --git a/Examples/LambdaFunctions/serverless/Benchmark-template.yml b/Examples/LambdaFunctions/serverless/Benchmark-template.yml index e83f27b0..74099441 100644 --- a/Examples/LambdaFunctions/serverless/Benchmark-template.yml +++ b/Examples/LambdaFunctions/serverless/Benchmark-template.yml @@ -16,4 +16,5 @@ provider: functions: benchmarkFunction: - handler: Benchmark \ No newline at end of file + handler: Benchmark + memorySize: 128 \ No newline at end of file diff --git a/Examples/LambdaFunctions/serverless/CurrencyExchange-template.yml b/Examples/LambdaFunctions/serverless/CurrencyExchange-template.yml index 2f853514..7e5c6b09 100644 --- a/Examples/LambdaFunctions/serverless/CurrencyExchange-template.yml +++ b/Examples/LambdaFunctions/serverless/CurrencyExchange-template.yml @@ -16,4 +16,5 @@ provider: functions: currencyExchangeFunction: - handler: CurrencyExchange \ No newline at end of file + handler: CurrencyExchange + memorySize: 128 \ No newline at end of file From 6345332557f3f5e018e593c98a41cb67fe28bb6e Mon Sep 17 00:00:00 2001 From: Andrea Scuderi Date: Sat, 6 Jun 2020 11:26:00 +0100 Subject: [PATCH 3/7] Fix HelloWorld Serverless template by removing the APIGateway event --- Examples/LambdaFunctions/README.md | 19 ++++++++++++------- .../serverless/HelloWorld-template.yml | 7 +------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Examples/LambdaFunctions/README.md b/Examples/LambdaFunctions/README.md index 90590727..e1e6aed0 100644 --- a/Examples/LambdaFunctions/README.md +++ b/Examples/LambdaFunctions/README.md @@ -120,7 +120,7 @@ The `serverless-deploy.sh` script passes through any parameters to the Serverles 4. Testing -For the HelloWorld and APIGateway sample: +For the APIGateway sample: The Serverless template will provide an endpoint which you can use to test the Lambda. @@ -131,25 +131,30 @@ Outuput example: ... Serverless: Stack update finished... Service Information -service: helloworld-swift-aws +service: apigateway-swift-aws stage: dev region: us-east-1 -stack: helloworld-swift-aws-dev -resources: 11 +stack: apigateway-swift-aws-dev +resources: 12 api keys: None endpoints: - GET - https://jm3b9p4bu2.execute-api.us-east-1.amazonaws.com/dev/hello + GET - https://r39lvhfng3.execute-api.us-east-1.amazonaws.com/api functions: - hello: helloworld-swift-aws-dev-hello + 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://jm3b9p4bu2.execute-api.us-east-1.amazonaws.com/dev/hello + 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. diff --git a/Examples/LambdaFunctions/serverless/HelloWorld-template.yml b/Examples/LambdaFunctions/serverless/HelloWorld-template.yml index 00c97a41..276f9909 100644 --- a/Examples/LambdaFunctions/serverless/HelloWorld-template.yml +++ b/Examples/LambdaFunctions/serverless/HelloWorld-template.yml @@ -17,9 +17,4 @@ provider: functions: hello: handler: HelloWorld - memorySize: 128 - events: - - http: - method: GET - path: /hello - integration: lambda \ No newline at end of file + memorySize: 128 \ No newline at end of file From 5f1fb8a3e99a513339a1be7f124173de01de2a89 Mon Sep 17 00:00:00 2001 From: Andrea Scuderi Date: Fri, 12 Jun 2020 08:52:22 +0200 Subject: [PATCH 4/7] 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 --- .gitignore | 1 + Examples/{LambdaFunctions => }/Dockerfile | 0 Examples/LambdaFunctions/.gitignore | 1 - Examples/LambdaFunctions/README.md | 7 ++++++ .../SAM}/APIGateway-template.yml | 2 +- .../SAM}/Benchmark-template.yml | 2 +- .../SAM}/CurrencyExchange-template.yml | 2 +- .../SAM}/ErrorHandling-template.yml | 2 +- .../SAM}/HelloWorld-template.yml | 2 +- .../scripts/build-and-package.sh | 6 +++-- .../config.sh} | 14 +++++++---- .../{LambdaFunctions => }/scripts/deploy.sh | 25 ++++++------------- .../{LambdaFunctions => }/scripts/package.sh | 5 ++-- .../scripts/sam-deploy.sh | 21 +++------------- .../scripts/serverless-deploy.sh | 21 +++------------- Examples/scripts/serverless-remove.sh | 25 +++++++++++++++++++ .../serverless/APIGateway-template.yml | 2 +- .../serverless/Benchmark-template.yml | 2 +- .../serverless/CurrencyExchange-template.yml | 2 +- .../serverless/ErrorHandling-template.yml | 2 +- .../serverless/HelloWorld-template.yml | 2 +- 21 files changed, 73 insertions(+), 73 deletions(-) rename Examples/{LambdaFunctions => }/Dockerfile (100%) delete mode 100644 Examples/LambdaFunctions/.gitignore rename Examples/{LambdaFunctions => scripts/SAM}/APIGateway-template.yml (92%) rename Examples/{LambdaFunctions => scripts/SAM}/Benchmark-template.yml (85%) rename Examples/{LambdaFunctions => scripts/SAM}/CurrencyExchange-template.yml (85%) rename Examples/{LambdaFunctions => scripts/SAM}/ErrorHandling-template.yml (85%) rename Examples/{LambdaFunctions => scripts/SAM}/HelloWorld-template.yml (85%) rename Examples/{LambdaFunctions => }/scripts/build-and-package.sh (75%) rename Examples/{LambdaFunctions/scripts/serverless-remove.sh => scripts/config.sh} (85%) rename Examples/{LambdaFunctions => }/scripts/deploy.sh (72%) rename Examples/{LambdaFunctions => }/scripts/package.sh (87%) rename Examples/{LambdaFunctions => }/scripts/sam-deploy.sh (62%) rename Examples/{LambdaFunctions => }/scripts/serverless-deploy.sh (61%) create mode 100755 Examples/scripts/serverless-remove.sh rename Examples/{LambdaFunctions => scripts}/serverless/APIGateway-template.yml (88%) rename Examples/{LambdaFunctions => scripts}/serverless/Benchmark-template.yml (85%) rename Examples/{LambdaFunctions => scripts}/serverless/CurrencyExchange-template.yml (84%) rename Examples/{LambdaFunctions => scripts}/serverless/ErrorHandling-template.yml (84%) rename Examples/{LambdaFunctions => scripts}/serverless/HelloWorld-template.yml (84%) diff --git a/.gitignore b/.gitignore index dc546ee3..04708b0f 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ .swift-version xcuserdata Package.resolved +.serverless diff --git a/Examples/LambdaFunctions/Dockerfile b/Examples/Dockerfile similarity index 100% rename from Examples/LambdaFunctions/Dockerfile rename to Examples/Dockerfile diff --git a/Examples/LambdaFunctions/.gitignore b/Examples/LambdaFunctions/.gitignore deleted file mode 100644 index 9461c893..00000000 --- a/Examples/LambdaFunctions/.gitignore +++ /dev/null @@ -1 +0,0 @@ -.serverless \ No newline at end of file diff --git a/Examples/LambdaFunctions/README.md b/Examples/LambdaFunctions/README.md index e1e6aed0..4a68c38e 100644 --- a/Examples/LambdaFunctions/README.md +++ b/Examples/LambdaFunctions/README.md @@ -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 diff --git a/Examples/LambdaFunctions/APIGateway-template.yml b/Examples/scripts/SAM/APIGateway-template.yml similarity index 92% rename from Examples/LambdaFunctions/APIGateway-template.yml rename to Examples/scripts/SAM/APIGateway-template.yml index 38a92035..0d62d47e 100644 --- a/Examples/LambdaFunctions/APIGateway-template.yml +++ b/Examples/scripts/SAM/APIGateway-template.yml @@ -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: diff --git a/Examples/LambdaFunctions/Benchmark-template.yml b/Examples/scripts/SAM/Benchmark-template.yml similarity index 85% rename from Examples/LambdaFunctions/Benchmark-template.yml rename to Examples/scripts/SAM/Benchmark-template.yml index 38941a53..08db0e91 100644 --- a/Examples/LambdaFunctions/Benchmark-template.yml +++ b/Examples/scripts/SAM/Benchmark-template.yml @@ -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 diff --git a/Examples/LambdaFunctions/CurrencyExchange-template.yml b/Examples/scripts/SAM/CurrencyExchange-template.yml similarity index 85% rename from Examples/LambdaFunctions/CurrencyExchange-template.yml rename to Examples/scripts/SAM/CurrencyExchange-template.yml index de09cd4e..9ebee24f 100644 --- a/Examples/LambdaFunctions/CurrencyExchange-template.yml +++ b/Examples/scripts/SAM/CurrencyExchange-template.yml @@ -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 diff --git a/Examples/LambdaFunctions/ErrorHandling-template.yml b/Examples/scripts/SAM/ErrorHandling-template.yml similarity index 85% rename from Examples/LambdaFunctions/ErrorHandling-template.yml rename to Examples/scripts/SAM/ErrorHandling-template.yml index dba76b8e..05464743 100644 --- a/Examples/LambdaFunctions/ErrorHandling-template.yml +++ b/Examples/scripts/SAM/ErrorHandling-template.yml @@ -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 diff --git a/Examples/LambdaFunctions/HelloWorld-template.yml b/Examples/scripts/SAM/HelloWorld-template.yml similarity index 85% rename from Examples/LambdaFunctions/HelloWorld-template.yml rename to Examples/scripts/SAM/HelloWorld-template.yml index 7eddfbe7..b20f0a24 100644 --- a/Examples/LambdaFunctions/HelloWorld-template.yml +++ b/Examples/scripts/SAM/HelloWorld-template.yml @@ -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 diff --git a/Examples/LambdaFunctions/scripts/build-and-package.sh b/Examples/scripts/build-and-package.sh similarity index 75% rename from Examples/LambdaFunctions/scripts/build-and-package.sh rename to Examples/scripts/build-and-package.sh index 45bbea39..079579d2 100755 --- a/Examples/LambdaFunctions/scripts/build-and-package.sh +++ b/Examples/scripts/build-and-package.sh @@ -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" diff --git a/Examples/LambdaFunctions/scripts/serverless-remove.sh b/Examples/scripts/config.sh similarity index 85% rename from Examples/LambdaFunctions/scripts/serverless-remove.sh rename to Examples/scripts/config.sh index 78f686f2..4311cfc0 100755 --- a/Examples/LambdaFunctions/scripts/serverless-remove.sh +++ b/Examples/scripts/config.sh @@ -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" @@ -31,10 +35,10 @@ elif [[ ${#executables[@]} > 1 ]]; then read -p "select which executables to deploy: " executable fi -echo -e "\nremoving $executable" - echo "-------------------------------------------------------------------------" -echo "removing using Serverless" +echo "CONFIG" echo "-------------------------------------------------------------------------" - -serverless remove --config "serverless/${executable}-template.yml" --stage dev -v \ No newline at end of file +echo "DIR: $DIR" +echo "sources: $sources" +echo "executable: $executable" +echo "-------------------------------------------------------------------------" \ No newline at end of file diff --git a/Examples/LambdaFunctions/scripts/deploy.sh b/Examples/scripts/deploy.sh similarity index 72% rename from Examples/LambdaFunctions/scripts/deploy.sh rename to Examples/scripts/deploy.sh index 19db4a25..79d723a7 100755 --- a/Examples/LambdaFunctions/scripts/deploy.sh +++ b/Examples/scripts/deploy.sh @@ -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" @@ -47,7 +38,7 @@ 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" @@ -55,13 +46,13 @@ 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\"" diff --git a/Examples/LambdaFunctions/scripts/package.sh b/Examples/scripts/package.sh similarity index 87% rename from Examples/LambdaFunctions/scripts/package.sh rename to Examples/scripts/package.sh index 190be3f8..349360aa 100755 --- a/Examples/LambdaFunctions/scripts/package.sh +++ b/Examples/scripts/package.sh @@ -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" diff --git a/Examples/LambdaFunctions/scripts/sam-deploy.sh b/Examples/scripts/sam-deploy.sh similarity index 62% rename from Examples/LambdaFunctions/scripts/sam-deploy.sh rename to Examples/scripts/sam-deploy.sh index 3a937828..b0c52530 100755 --- a/Examples/LambdaFunctions/scripts/sam-deploy.sh +++ b/Examples/scripts/sam-deploy.sh @@ -14,22 +14,7 @@ ##===----------------------------------------------------------------------===## DIR="$(cd "$(dirname "$0")" && pwd)" - -executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') ) - -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 +source $DIR/config.sh echo -e "\ndeploying $executable" @@ -38,10 +23,10 @@ echo "preparing docker build image" echo "-------------------------------------------------------------------------" docker build . -q -t builder -$DIR/build-and-package.sh ${executable} +$DIR/build-and-package.sh ${executable} ${sources} echo "-------------------------------------------------------------------------" echo "deploying using SAM" echo "-------------------------------------------------------------------------" -sam deploy --template "${executable}-template.yml" $@ +sam deploy --template "./scripts/SAM/${executable}-template.yml" $@ \ No newline at end of file diff --git a/Examples/LambdaFunctions/scripts/serverless-deploy.sh b/Examples/scripts/serverless-deploy.sh similarity index 61% rename from Examples/LambdaFunctions/scripts/serverless-deploy.sh rename to Examples/scripts/serverless-deploy.sh index 1f9c388c..d038c156 100755 --- a/Examples/LambdaFunctions/scripts/serverless-deploy.sh +++ b/Examples/scripts/serverless-deploy.sh @@ -14,22 +14,7 @@ ##===----------------------------------------------------------------------===## DIR="$(cd "$(dirname "$0")" && pwd)" - -executables=( $(swift package dump-package | sed -e 's|: null|: ""|g' | jq '.products[] | (select(.type.executable)) | .name' | sed -e 's|"||g') ) - -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 +source $DIR/config.sh echo -e "\ndeploying $executable" @@ -38,10 +23,10 @@ echo "preparing docker build image" echo "-------------------------------------------------------------------------" docker build . -q -t builder -$DIR/build-and-package.sh ${executable} +$DIR/build-and-package.sh ${executable} ${sources} echo "-------------------------------------------------------------------------" echo "deploying using Serverless" echo "-------------------------------------------------------------------------" -serverless deploy --config "serverless/${executable}-template.yml" --stage dev -v \ No newline at end of file +serverless deploy --config "./scripts/serverless/${executable}-template.yml" --stage dev -v \ No newline at end of file diff --git a/Examples/scripts/serverless-remove.sh b/Examples/scripts/serverless-remove.sh new file mode 100755 index 00000000..f6b228c5 --- /dev/null +++ b/Examples/scripts/serverless-remove.sh @@ -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 \ No newline at end of file diff --git a/Examples/LambdaFunctions/serverless/APIGateway-template.yml b/Examples/scripts/serverless/APIGateway-template.yml similarity index 88% rename from Examples/LambdaFunctions/serverless/APIGateway-template.yml rename to Examples/scripts/serverless/APIGateway-template.yml index 6787ad0f..c584da29 100644 --- a/Examples/LambdaFunctions/serverless/APIGateway-template.yml +++ b/Examples/scripts/serverless/APIGateway-template.yml @@ -1,7 +1,7 @@ service: apigateway-swift-aws package: - artifact: .build/lambda/APIGateway/lambda.zip + artifact: LambdaFunctions/.build/lambda/APIGateway/lambda.zip provider: name: aws diff --git a/Examples/LambdaFunctions/serverless/Benchmark-template.yml b/Examples/scripts/serverless/Benchmark-template.yml similarity index 85% rename from Examples/LambdaFunctions/serverless/Benchmark-template.yml rename to Examples/scripts/serverless/Benchmark-template.yml index 74099441..385ba8e4 100644 --- a/Examples/LambdaFunctions/serverless/Benchmark-template.yml +++ b/Examples/scripts/serverless/Benchmark-template.yml @@ -1,7 +1,7 @@ service: benchmark-swift-aws package: - artifact: .build/lambda/Benchmark/lambda.zip + artifact: LambdaFunctions/.build/lambda/Benchmark/lambda.zip provider: name: aws diff --git a/Examples/LambdaFunctions/serverless/CurrencyExchange-template.yml b/Examples/scripts/serverless/CurrencyExchange-template.yml similarity index 84% rename from Examples/LambdaFunctions/serverless/CurrencyExchange-template.yml rename to Examples/scripts/serverless/CurrencyExchange-template.yml index 7e5c6b09..9c3354e3 100644 --- a/Examples/LambdaFunctions/serverless/CurrencyExchange-template.yml +++ b/Examples/scripts/serverless/CurrencyExchange-template.yml @@ -1,7 +1,7 @@ service: currency-swift-aws package: - artifact: .build/lambda/CurrencyExchange/lambda.zip + artifact: LambdaFunctions/.build/lambda/CurrencyExchange/lambda.zip provider: name: aws diff --git a/Examples/LambdaFunctions/serverless/ErrorHandling-template.yml b/Examples/scripts/serverless/ErrorHandling-template.yml similarity index 84% rename from Examples/LambdaFunctions/serverless/ErrorHandling-template.yml rename to Examples/scripts/serverless/ErrorHandling-template.yml index 367be490..6b1ef0ba 100644 --- a/Examples/LambdaFunctions/serverless/ErrorHandling-template.yml +++ b/Examples/scripts/serverless/ErrorHandling-template.yml @@ -1,7 +1,7 @@ service: errorhandling-swift-aws package: - artifact: .build/lambda/ErrorHandling/lambda.zip + artifact: LambdaFunctions/.build/lambda/ErrorHandling/lambda.zip provider: name: aws diff --git a/Examples/LambdaFunctions/serverless/HelloWorld-template.yml b/Examples/scripts/serverless/HelloWorld-template.yml similarity index 84% rename from Examples/LambdaFunctions/serverless/HelloWorld-template.yml rename to Examples/scripts/serverless/HelloWorld-template.yml index 276f9909..8d974e64 100644 --- a/Examples/LambdaFunctions/serverless/HelloWorld-template.yml +++ b/Examples/scripts/serverless/HelloWorld-template.yml @@ -1,7 +1,7 @@ service: helloworld-swift-aws package: - artifact: .build/lambda/HelloWorld/lambda.zip + artifact: LambdaFunctions/.build/lambda/HelloWorld/lambda.zip provider: name: aws From ee36b6de705ebe5533790083ca2e55d3502bbcae Mon Sep 17 00:00:00 2001 From: Andrea Scuderi Date: Fri, 12 Jun 2020 09:07:26 +0200 Subject: [PATCH 5/7] Amend Serverless Framework title in readme.md --- Examples/LambdaFunctions/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Examples/LambdaFunctions/README.md b/Examples/LambdaFunctions/README.md index 4a68c38e..52ecf0e3 100644 --- a/Examples/LambdaFunctions/README.md +++ b/Examples/LambdaFunctions/README.md @@ -93,7 +93,7 @@ The SAM template will provide an output labelled `LambdaApiGatewayEndpoint` whic For all other samples use the AWS Lambda console. -### Deployment instructions using AWS Serverless Framework +### 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. From 14ef7fe61877317ce59b6b8ce428798961fce880 Mon Sep 17 00:00:00 2001 From: Andrea Scuderi Date: Fri, 12 Jun 2020 20:59:33 +0200 Subject: [PATCH 6/7] Restore scripts under Example/LambdaFunctions --- Examples/{ => LambdaFunctions}/Dockerfile | 0 .../scripts/SAM/APIGateway-template.yml | 2 +- .../scripts/SAM/Benchmark-template.yml | 2 +- .../scripts/SAM/CurrencyExchange-template.yml | 2 +- .../scripts/SAM/ErrorHandling-template.yml | 2 +- .../scripts/SAM/HelloWorld-template.yml | 2 +- .../{ => LambdaFunctions}/scripts/build-and-package.sh | 7 +++---- Examples/{ => LambdaFunctions}/scripts/config.sh | 5 ----- Examples/{ => LambdaFunctions}/scripts/deploy.sh | 8 +++----- Examples/{ => LambdaFunctions}/scripts/package.sh | 5 ++--- Examples/{ => LambdaFunctions}/scripts/sam-deploy.sh | 2 +- .../{ => LambdaFunctions}/scripts/serverless-deploy.sh | 2 +- .../{ => LambdaFunctions}/scripts/serverless-remove.sh | 0 .../scripts/serverless/APIGateway-template.yml | 2 +- .../scripts/serverless/Benchmark-template.yml | 2 +- .../scripts/serverless/CurrencyExchange-template.yml | 2 +- .../scripts/serverless/ErrorHandling-template.yml | 2 +- .../scripts/serverless/HelloWorld-template.yml | 2 +- 18 files changed, 20 insertions(+), 29 deletions(-) rename Examples/{ => LambdaFunctions}/Dockerfile (100%) rename Examples/{ => LambdaFunctions}/scripts/SAM/APIGateway-template.yml (92%) rename Examples/{ => LambdaFunctions}/scripts/SAM/Benchmark-template.yml (85%) rename Examples/{ => LambdaFunctions}/scripts/SAM/CurrencyExchange-template.yml (85%) rename Examples/{ => LambdaFunctions}/scripts/SAM/ErrorHandling-template.yml (85%) rename Examples/{ => LambdaFunctions}/scripts/SAM/HelloWorld-template.yml (85%) rename Examples/{ => LambdaFunctions}/scripts/build-and-package.sh (85%) rename Examples/{ => LambdaFunctions}/scripts/config.sh (95%) rename Examples/{ => LambdaFunctions}/scripts/deploy.sh (89%) rename Examples/{ => LambdaFunctions}/scripts/package.sh (87%) rename Examples/{ => LambdaFunctions}/scripts/sam-deploy.sh (95%) rename Examples/{ => LambdaFunctions}/scripts/serverless-deploy.sh (95%) rename Examples/{ => LambdaFunctions}/scripts/serverless-remove.sh (100%) rename Examples/{ => LambdaFunctions}/scripts/serverless/APIGateway-template.yml (88%) rename Examples/{ => LambdaFunctions}/scripts/serverless/Benchmark-template.yml (85%) rename Examples/{ => LambdaFunctions}/scripts/serverless/CurrencyExchange-template.yml (84%) rename Examples/{ => LambdaFunctions}/scripts/serverless/ErrorHandling-template.yml (84%) rename Examples/{ => LambdaFunctions}/scripts/serverless/HelloWorld-template.yml (84%) diff --git a/Examples/Dockerfile b/Examples/LambdaFunctions/Dockerfile similarity index 100% rename from Examples/Dockerfile rename to Examples/LambdaFunctions/Dockerfile diff --git a/Examples/scripts/SAM/APIGateway-template.yml b/Examples/LambdaFunctions/scripts/SAM/APIGateway-template.yml similarity index 92% rename from Examples/scripts/SAM/APIGateway-template.yml rename to Examples/LambdaFunctions/scripts/SAM/APIGateway-template.yml index 0d62d47e..38a92035 100644 --- a/Examples/scripts/SAM/APIGateway-template.yml +++ b/Examples/LambdaFunctions/scripts/SAM/APIGateway-template.yml @@ -9,7 +9,7 @@ Resources: Properties: Handler: Provided Runtime: provided - CodeUri: LambdaFunctions/.build/lambda/APIGateway/lambda.zip + CodeUri: .build/lambda/APIGateway/lambda.zip # Add an API Gateway event source for the Lambda Events: HttpGet: diff --git a/Examples/scripts/SAM/Benchmark-template.yml b/Examples/LambdaFunctions/scripts/SAM/Benchmark-template.yml similarity index 85% rename from Examples/scripts/SAM/Benchmark-template.yml rename to Examples/LambdaFunctions/scripts/SAM/Benchmark-template.yml index 08db0e91..38941a53 100644 --- a/Examples/scripts/SAM/Benchmark-template.yml +++ b/Examples/LambdaFunctions/scripts/SAM/Benchmark-template.yml @@ -9,6 +9,6 @@ Resources: Properties: Handler: Provided Runtime: provided - CodeUri: LambdaFunctions/.build/lambda/Benchmark/lambda.zip + CodeUri: .build/lambda/Benchmark/lambda.zip # Instructs new versions to be published to an alias named "live". AutoPublishAlias: live diff --git a/Examples/scripts/SAM/CurrencyExchange-template.yml b/Examples/LambdaFunctions/scripts/SAM/CurrencyExchange-template.yml similarity index 85% rename from Examples/scripts/SAM/CurrencyExchange-template.yml rename to Examples/LambdaFunctions/scripts/SAM/CurrencyExchange-template.yml index 9ebee24f..de09cd4e 100644 --- a/Examples/scripts/SAM/CurrencyExchange-template.yml +++ b/Examples/LambdaFunctions/scripts/SAM/CurrencyExchange-template.yml @@ -9,7 +9,7 @@ Resources: Properties: Handler: Provided Runtime: provided - CodeUri: LambdaFunctions/.build/lambda/CurrencyExchange/lambda.zip + CodeUri: .build/lambda/CurrencyExchange/lambda.zip Timeout: 300 # Instructs new versions to be published to an alias named "live". AutoPublishAlias: live diff --git a/Examples/scripts/SAM/ErrorHandling-template.yml b/Examples/LambdaFunctions/scripts/SAM/ErrorHandling-template.yml similarity index 85% rename from Examples/scripts/SAM/ErrorHandling-template.yml rename to Examples/LambdaFunctions/scripts/SAM/ErrorHandling-template.yml index 05464743..dba76b8e 100644 --- a/Examples/scripts/SAM/ErrorHandling-template.yml +++ b/Examples/LambdaFunctions/scripts/SAM/ErrorHandling-template.yml @@ -9,6 +9,6 @@ Resources: Properties: Handler: Provided Runtime: provided - CodeUri: LambdaFunctions/.build/lambda/ErrorHandling/lambda.zip + CodeUri: .build/lambda/ErrorHandling/lambda.zip # Instructs new versions to be published to an alias named "live". AutoPublishAlias: live diff --git a/Examples/scripts/SAM/HelloWorld-template.yml b/Examples/LambdaFunctions/scripts/SAM/HelloWorld-template.yml similarity index 85% rename from Examples/scripts/SAM/HelloWorld-template.yml rename to Examples/LambdaFunctions/scripts/SAM/HelloWorld-template.yml index b20f0a24..7eddfbe7 100644 --- a/Examples/scripts/SAM/HelloWorld-template.yml +++ b/Examples/LambdaFunctions/scripts/SAM/HelloWorld-template.yml @@ -9,6 +9,6 @@ Resources: Properties: Handler: Provided Runtime: provided - CodeUri: LambdaFunctions/.build/lambda/HelloWorld/lambda.zip + CodeUri: .build/lambda/HelloWorld/lambda.zip # Instructs new versions to be published to an alias named "live". AutoPublishAlias: live diff --git a/Examples/scripts/build-and-package.sh b/Examples/LambdaFunctions/scripts/build-and-package.sh similarity index 85% rename from Examples/scripts/build-and-package.sh rename to Examples/LambdaFunctions/scripts/build-and-package.sh index 079579d2..b59f2314 100755 --- a/Examples/scripts/build-and-package.sh +++ b/Examples/LambdaFunctions/scripts/build-and-package.sh @@ -16,16 +16,15 @@ set -eu executable=$1 -workspace="$(pwd)/.." -sources=$2 +workspace="$(pwd)/../.." echo "-------------------------------------------------------------------------" echo "building \"$executable\" lambda" echo "-------------------------------------------------------------------------" -docker run --rm -v "$workspace":/workspace -w /workspace/Examples/$sources builder bash -cl "swift build --product $executable -c release -Xswiftc -g" +docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "swift build --product $executable -c release -Xswiftc -g" echo "done" echo "-------------------------------------------------------------------------" echo "packaging \"$executable\" lambda" echo "-------------------------------------------------------------------------" -docker run --rm -v "$workspace":/workspace -w /workspace/Examples builder bash -cl "./scripts/package.sh $executable $sources" +docker run --rm -v "$workspace":/workspace -w /workspace/Examples/LambdaFunctions builder bash -cl "./scripts/package.sh $executable" diff --git a/Examples/scripts/config.sh b/Examples/LambdaFunctions/scripts/config.sh similarity index 95% rename from Examples/scripts/config.sh rename to Examples/LambdaFunctions/scripts/config.sh index 4311cfc0..d6ec4b7f 100755 --- a/Examples/scripts/config.sh +++ b/Examples/LambdaFunctions/scripts/config.sh @@ -15,11 +15,7 @@ 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" @@ -39,6 +35,5 @@ echo "-------------------------------------------------------------------------" echo "CONFIG" echo "-------------------------------------------------------------------------" echo "DIR: $DIR" -echo "sources: $sources" echo "executable: $executable" echo "-------------------------------------------------------------------------" \ No newline at end of file diff --git a/Examples/scripts/deploy.sh b/Examples/LambdaFunctions/scripts/deploy.sh similarity index 89% rename from Examples/scripts/deploy.sh rename to Examples/LambdaFunctions/scripts/deploy.sh index 79d723a7..619826eb 100755 --- a/Examples/scripts/deploy.sh +++ b/Examples/LambdaFunctions/scripts/deploy.sh @@ -21,9 +21,7 @@ lambda_name=SwiftSample # S3 bucket name to upload zip file (must exist in AWS S3) s3_bucket=swift-lambda-test -workspace="$(pwd)/.." - -sources="LambdaFunctions" +workspace="$(pwd)/../.." DIR="$(cd "$(dirname "$0")" && pwd)" source $DIR/config.sh @@ -46,13 +44,13 @@ echo "done" echo "-------------------------------------------------------------------------" echo "packaging \"$executable\" lambda" echo "-------------------------------------------------------------------------" -docker run --rm -v "$workspace/Examples":/workspace -w /workspace builder bash -cl "./scripts/package.sh $executable $sources" +docker run --rm -v `pwd`:/workspace -w /workspace builder bash -cl "./scripts/package.sh $executable" echo "-------------------------------------------------------------------------" echo "uploading \"$executable\" lambda to s3" echo "-------------------------------------------------------------------------" -aws s3 cp $sources/.build/lambda/$executable/lambda.zip s3://$s3_bucket/ +aws s3 cp .build/lambda/$executable/lambda.zip s3://$s3_bucket/ echo "-------------------------------------------------------------------------" echo "updating \"$lambda_name\" to latest \"$executable\"" diff --git a/Examples/scripts/package.sh b/Examples/LambdaFunctions/scripts/package.sh similarity index 87% rename from Examples/scripts/package.sh rename to Examples/LambdaFunctions/scripts/package.sh index 349360aa..190be3f8 100755 --- a/Examples/scripts/package.sh +++ b/Examples/LambdaFunctions/scripts/package.sh @@ -16,12 +16,11 @@ set -eu executable=$1 -sources=$2 -target=$sources/.build/lambda/$executable +target=.build/lambda/$executable rm -rf "$target" mkdir -p "$target" -cp "$sources/.build/release/$executable" "$target/" +cp ".build/release/$executable" "$target/" cp -Pv /usr/lib/swift/linux/lib*so* "$target" cd "$target" ln -s "$executable" "bootstrap" diff --git a/Examples/scripts/sam-deploy.sh b/Examples/LambdaFunctions/scripts/sam-deploy.sh similarity index 95% rename from Examples/scripts/sam-deploy.sh rename to Examples/LambdaFunctions/scripts/sam-deploy.sh index b0c52530..779ed0e9 100755 --- a/Examples/scripts/sam-deploy.sh +++ b/Examples/LambdaFunctions/scripts/sam-deploy.sh @@ -23,7 +23,7 @@ echo "preparing docker build image" echo "-------------------------------------------------------------------------" docker build . -q -t builder -$DIR/build-and-package.sh ${executable} ${sources} +$DIR/build-and-package.sh ${executable} echo "-------------------------------------------------------------------------" echo "deploying using SAM" diff --git a/Examples/scripts/serverless-deploy.sh b/Examples/LambdaFunctions/scripts/serverless-deploy.sh similarity index 95% rename from Examples/scripts/serverless-deploy.sh rename to Examples/LambdaFunctions/scripts/serverless-deploy.sh index d038c156..7e2d7cb0 100755 --- a/Examples/scripts/serverless-deploy.sh +++ b/Examples/LambdaFunctions/scripts/serverless-deploy.sh @@ -23,7 +23,7 @@ echo "preparing docker build image" echo "-------------------------------------------------------------------------" docker build . -q -t builder -$DIR/build-and-package.sh ${executable} ${sources} +$DIR/build-and-package.sh ${executable} echo "-------------------------------------------------------------------------" echo "deploying using Serverless" diff --git a/Examples/scripts/serverless-remove.sh b/Examples/LambdaFunctions/scripts/serverless-remove.sh similarity index 100% rename from Examples/scripts/serverless-remove.sh rename to Examples/LambdaFunctions/scripts/serverless-remove.sh diff --git a/Examples/scripts/serverless/APIGateway-template.yml b/Examples/LambdaFunctions/scripts/serverless/APIGateway-template.yml similarity index 88% rename from Examples/scripts/serverless/APIGateway-template.yml rename to Examples/LambdaFunctions/scripts/serverless/APIGateway-template.yml index c584da29..6787ad0f 100644 --- a/Examples/scripts/serverless/APIGateway-template.yml +++ b/Examples/LambdaFunctions/scripts/serverless/APIGateway-template.yml @@ -1,7 +1,7 @@ service: apigateway-swift-aws package: - artifact: LambdaFunctions/.build/lambda/APIGateway/lambda.zip + artifact: .build/lambda/APIGateway/lambda.zip provider: name: aws diff --git a/Examples/scripts/serverless/Benchmark-template.yml b/Examples/LambdaFunctions/scripts/serverless/Benchmark-template.yml similarity index 85% rename from Examples/scripts/serverless/Benchmark-template.yml rename to Examples/LambdaFunctions/scripts/serverless/Benchmark-template.yml index 385ba8e4..74099441 100644 --- a/Examples/scripts/serverless/Benchmark-template.yml +++ b/Examples/LambdaFunctions/scripts/serverless/Benchmark-template.yml @@ -1,7 +1,7 @@ service: benchmark-swift-aws package: - artifact: LambdaFunctions/.build/lambda/Benchmark/lambda.zip + artifact: .build/lambda/Benchmark/lambda.zip provider: name: aws diff --git a/Examples/scripts/serverless/CurrencyExchange-template.yml b/Examples/LambdaFunctions/scripts/serverless/CurrencyExchange-template.yml similarity index 84% rename from Examples/scripts/serverless/CurrencyExchange-template.yml rename to Examples/LambdaFunctions/scripts/serverless/CurrencyExchange-template.yml index 9c3354e3..7e5c6b09 100644 --- a/Examples/scripts/serverless/CurrencyExchange-template.yml +++ b/Examples/LambdaFunctions/scripts/serverless/CurrencyExchange-template.yml @@ -1,7 +1,7 @@ service: currency-swift-aws package: - artifact: LambdaFunctions/.build/lambda/CurrencyExchange/lambda.zip + artifact: .build/lambda/CurrencyExchange/lambda.zip provider: name: aws diff --git a/Examples/scripts/serverless/ErrorHandling-template.yml b/Examples/LambdaFunctions/scripts/serverless/ErrorHandling-template.yml similarity index 84% rename from Examples/scripts/serverless/ErrorHandling-template.yml rename to Examples/LambdaFunctions/scripts/serverless/ErrorHandling-template.yml index 6b1ef0ba..367be490 100644 --- a/Examples/scripts/serverless/ErrorHandling-template.yml +++ b/Examples/LambdaFunctions/scripts/serverless/ErrorHandling-template.yml @@ -1,7 +1,7 @@ service: errorhandling-swift-aws package: - artifact: LambdaFunctions/.build/lambda/ErrorHandling/lambda.zip + artifact: .build/lambda/ErrorHandling/lambda.zip provider: name: aws diff --git a/Examples/scripts/serverless/HelloWorld-template.yml b/Examples/LambdaFunctions/scripts/serverless/HelloWorld-template.yml similarity index 84% rename from Examples/scripts/serverless/HelloWorld-template.yml rename to Examples/LambdaFunctions/scripts/serverless/HelloWorld-template.yml index 8d974e64..276f9909 100644 --- a/Examples/scripts/serverless/HelloWorld-template.yml +++ b/Examples/LambdaFunctions/scripts/serverless/HelloWorld-template.yml @@ -1,7 +1,7 @@ service: helloworld-swift-aws package: - artifact: LambdaFunctions/.build/lambda/HelloWorld/lambda.zip + artifact: .build/lambda/HelloWorld/lambda.zip provider: name: aws From 8cba0c25d363c2dd103f85bdf693669b4c7c4c07 Mon Sep 17 00:00:00 2001 From: Andrea Scuderi Date: Fri, 12 Jun 2020 21:03:49 +0200 Subject: [PATCH 7/7] Fix README.md --- Examples/LambdaFunctions/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Examples/LambdaFunctions/README.md b/Examples/LambdaFunctions/README.md index 52ecf0e3..3c46a092 100644 --- a/Examples/LambdaFunctions/README.md +++ b/Examples/LambdaFunctions/README.md @@ -4,11 +4,11 @@ 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. +The scripts are prepared to work from the `LambdaFunctions` folder. ``` git clone https://github.com/swift-server/swift-aws-lambda-runtime.git -cd swift-aws-lambda-runtime/Examples +cd swift-aws-lambda-runtime/Examples/LambdaFunctions ``` Note: The example scripts assume you have [jq](https://stedolan.github.io/jq/download/) command line tool installed.