From 45b075abfdb34a944091de6bc0181d90adaea979 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 22 Apr 2022 14:09:58 +0200 Subject: [PATCH 1/9] Added script for locally building/release Lambda layer --- .gitignore | 1 + scripts/release-local-lambda-integration.sh | 49 +++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100755 scripts/release-local-lambda-integration.sh diff --git a/.gitignore b/.gitignore index e23931921e..bd5df5dddd 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ pip-log.txt /build /dist /dist-serverless +sentry-python-serverless*.zip .cache .idea .eggs diff --git a/scripts/release-local-lambda-integration.sh b/scripts/release-local-lambda-integration.sh new file mode 100755 index 0000000000..560481fd3a --- /dev/null +++ b/scripts/release-local-lambda-integration.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash + +set -euo pipefail + + +# Creating Lambda layer +echo "Creating Lambda layer in ./dist-serverless ..." +make aws-lambda-layer +echo "Done creating Lambda layer in ./dist-serverless." + + +# Adding Sentry Lambda extension to Lambda layer +echo "Adding Sentry Lambda extension to Lambda layer in ./dist-serverless ..." +mkdir -p dist-serverless/extensions +curl -0 --silent --output dist-serverless/extensions/sentry-lambda-extension `curl -s https://release-registry.services.sentry.io/apps/sentry-lambda-extension/latest | jq -r .files.\"sentry-lambda-extension\".url` +chmod +x dist-serverless/extensions/sentry-lambda-extension +echo "Done adding Sentry Lambda extension to Lambda layer in ./dist-serverless." + +echo "Setting configuration for extension in in ./dist-serverless/extensions ..." +mkdir -p dist-serverless/extensions/.relay/ +cat << EOT >> dist-serverless/extensions/.relay/config.yml +--- +relay: + mode: proxy + upstream: "https://sentry.io" + host: 127.0.0.1 + port: 3000 +EOT +echo "Done setting configuration for extension in in ./dist-serverless/extensions ..." + + +# Zip Lambda layer and included Lambda extension +echo "Zipping Lambda layer and included Lambda extension..." +zip -r sentry-python-serverless-x.x.x-dev.zip dist-serverless/ -x \*__pycache__\* -x \*.yml +echo "Done Zipping Lambda layer and included Lambda extension to ./sentry-python-serverless-x.x.x-dev.zip." + + +# Deploying zipped Lambda layer to AWS +echo "Deploying zipped Lambda layer to AWS..." + +aws lambda publish-layer-version \ + --layer-name "SentryPythonServerlessSDKLocalDev" \ + --region "eu-central-1" \ + --zip-file "fileb://sentry-python-serverless-x.x.x-dev.zip" \ + --description "Local test build of SentryPythonServerlessSDK (can be deleted)" + +echo "Done deploying zipped Lambda layer to AWS as SentryPythonServerlessSDKLocalDev." + +echo "All done. Have a nice day!" From 4afe6d86cc4bb20033dc773bbd5a5f91d3d7eead Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 22 Apr 2022 14:09:58 +0200 Subject: [PATCH 2/9] Added script for locally building/release Lambda layer --- .gitignore | 1 + scripts/release-local-lambda-integration.sh | 49 +++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100755 scripts/release-local-lambda-integration.sh diff --git a/.gitignore b/.gitignore index e23931921e..bd5df5dddd 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ pip-log.txt /build /dist /dist-serverless +sentry-python-serverless*.zip .cache .idea .eggs diff --git a/scripts/release-local-lambda-integration.sh b/scripts/release-local-lambda-integration.sh new file mode 100755 index 0000000000..560481fd3a --- /dev/null +++ b/scripts/release-local-lambda-integration.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash + +set -euo pipefail + + +# Creating Lambda layer +echo "Creating Lambda layer in ./dist-serverless ..." +make aws-lambda-layer +echo "Done creating Lambda layer in ./dist-serverless." + + +# Adding Sentry Lambda extension to Lambda layer +echo "Adding Sentry Lambda extension to Lambda layer in ./dist-serverless ..." +mkdir -p dist-serverless/extensions +curl -0 --silent --output dist-serverless/extensions/sentry-lambda-extension `curl -s https://release-registry.services.sentry.io/apps/sentry-lambda-extension/latest | jq -r .files.\"sentry-lambda-extension\".url` +chmod +x dist-serverless/extensions/sentry-lambda-extension +echo "Done adding Sentry Lambda extension to Lambda layer in ./dist-serverless." + +echo "Setting configuration for extension in in ./dist-serverless/extensions ..." +mkdir -p dist-serverless/extensions/.relay/ +cat << EOT >> dist-serverless/extensions/.relay/config.yml +--- +relay: + mode: proxy + upstream: "https://sentry.io" + host: 127.0.0.1 + port: 3000 +EOT +echo "Done setting configuration for extension in in ./dist-serverless/extensions ..." + + +# Zip Lambda layer and included Lambda extension +echo "Zipping Lambda layer and included Lambda extension..." +zip -r sentry-python-serverless-x.x.x-dev.zip dist-serverless/ -x \*__pycache__\* -x \*.yml +echo "Done Zipping Lambda layer and included Lambda extension to ./sentry-python-serverless-x.x.x-dev.zip." + + +# Deploying zipped Lambda layer to AWS +echo "Deploying zipped Lambda layer to AWS..." + +aws lambda publish-layer-version \ + --layer-name "SentryPythonServerlessSDKLocalDev" \ + --region "eu-central-1" \ + --zip-file "fileb://sentry-python-serverless-x.x.x-dev.zip" \ + --description "Local test build of SentryPythonServerlessSDK (can be deleted)" + +echo "Done deploying zipped Lambda layer to AWS as SentryPythonServerlessSDKLocalDev." + +echo "All done. Have a nice day!" From 8a8c1e3fd7077b4cf84fab4403efaaaefcc5a87f Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 22 Apr 2022 14:46:33 +0200 Subject: [PATCH 3/9] Added script to attach layer to function --- scripts/attach-layer-to-lambda-function.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100755 scripts/attach-layer-to-lambda-function.sh diff --git a/scripts/attach-layer-to-lambda-function.sh b/scripts/attach-layer-to-lambda-function.sh new file mode 100755 index 0000000000..f929014c72 --- /dev/null +++ b/scripts/attach-layer-to-lambda-function.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash + +set -euo pipefail + +FUNCTION_NAME=$1 + +echo "Getting ARN of newest Sentry lambda layer..." +LAYER_ARN=$(aws lambda list-layer-versions --layer-name SentryPythonServerlessSDKLocalDev --query "LayerVersions[0].LayerVersionArn" | tr -d '"') +echo "Done getting ARN of newest Sentry lambda layer $LAYER_ARN." + +echo "Attaching Lamba layer to function $FUNCTION_NAME..." +aws lambda update-function-configuration --function-name "$FUNCTION_NAME" --layers "$LAYER_ARN" --no-cli-pager +echo "Done attaching Lamba layer to function $FUNCTION_NAME." + +echo "All done. Have a nice day!" From d2a920c78e1cc646af29b015f97e98e103ce11e5 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 22 Apr 2022 14:50:17 +0200 Subject: [PATCH 4/9] Cleanup --- scripts/attach-layer-to-lambda-function.sh | 15 ------- scripts/release-local-lambda-integration.sh | 49 --------------------- 2 files changed, 64 deletions(-) delete mode 100755 scripts/attach-layer-to-lambda-function.sh delete mode 100755 scripts/release-local-lambda-integration.sh diff --git a/scripts/attach-layer-to-lambda-function.sh b/scripts/attach-layer-to-lambda-function.sh deleted file mode 100755 index f929014c72..0000000000 --- a/scripts/attach-layer-to-lambda-function.sh +++ /dev/null @@ -1,15 +0,0 @@ -#!/usr/bin/env bash - -set -euo pipefail - -FUNCTION_NAME=$1 - -echo "Getting ARN of newest Sentry lambda layer..." -LAYER_ARN=$(aws lambda list-layer-versions --layer-name SentryPythonServerlessSDKLocalDev --query "LayerVersions[0].LayerVersionArn" | tr -d '"') -echo "Done getting ARN of newest Sentry lambda layer $LAYER_ARN." - -echo "Attaching Lamba layer to function $FUNCTION_NAME..." -aws lambda update-function-configuration --function-name "$FUNCTION_NAME" --layers "$LAYER_ARN" --no-cli-pager -echo "Done attaching Lamba layer to function $FUNCTION_NAME." - -echo "All done. Have a nice day!" diff --git a/scripts/release-local-lambda-integration.sh b/scripts/release-local-lambda-integration.sh deleted file mode 100755 index 560481fd3a..0000000000 --- a/scripts/release-local-lambda-integration.sh +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/env bash - -set -euo pipefail - - -# Creating Lambda layer -echo "Creating Lambda layer in ./dist-serverless ..." -make aws-lambda-layer -echo "Done creating Lambda layer in ./dist-serverless." - - -# Adding Sentry Lambda extension to Lambda layer -echo "Adding Sentry Lambda extension to Lambda layer in ./dist-serverless ..." -mkdir -p dist-serverless/extensions -curl -0 --silent --output dist-serverless/extensions/sentry-lambda-extension `curl -s https://release-registry.services.sentry.io/apps/sentry-lambda-extension/latest | jq -r .files.\"sentry-lambda-extension\".url` -chmod +x dist-serverless/extensions/sentry-lambda-extension -echo "Done adding Sentry Lambda extension to Lambda layer in ./dist-serverless." - -echo "Setting configuration for extension in in ./dist-serverless/extensions ..." -mkdir -p dist-serverless/extensions/.relay/ -cat << EOT >> dist-serverless/extensions/.relay/config.yml ---- -relay: - mode: proxy - upstream: "https://sentry.io" - host: 127.0.0.1 - port: 3000 -EOT -echo "Done setting configuration for extension in in ./dist-serverless/extensions ..." - - -# Zip Lambda layer and included Lambda extension -echo "Zipping Lambda layer and included Lambda extension..." -zip -r sentry-python-serverless-x.x.x-dev.zip dist-serverless/ -x \*__pycache__\* -x \*.yml -echo "Done Zipping Lambda layer and included Lambda extension to ./sentry-python-serverless-x.x.x-dev.zip." - - -# Deploying zipped Lambda layer to AWS -echo "Deploying zipped Lambda layer to AWS..." - -aws lambda publish-layer-version \ - --layer-name "SentryPythonServerlessSDKLocalDev" \ - --region "eu-central-1" \ - --zip-file "fileb://sentry-python-serverless-x.x.x-dev.zip" \ - --description "Local test build of SentryPythonServerlessSDK (can be deleted)" - -echo "Done deploying zipped Lambda layer to AWS as SentryPythonServerlessSDKLocalDev." - -echo "All done. Have a nice day!" From a2d4ce452a9f79d3be61a8f1ac29f8246a30e2a5 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 22 Apr 2022 14:54:18 +0200 Subject: [PATCH 5/9] Added script to build and attach lambda layer --- .../aws-attach-layer-to-lambda-function.sh | 19 +++++++ scripts/aws-deploy-local-layer.sh | 52 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100755 scripts/aws-attach-layer-to-lambda-function.sh create mode 100755 scripts/aws-deploy-local-layer.sh diff --git a/scripts/aws-attach-layer-to-lambda-function.sh b/scripts/aws-attach-layer-to-lambda-function.sh new file mode 100755 index 0000000000..be702ae4e5 --- /dev/null +++ b/scripts/aws-attach-layer-to-lambda-function.sh @@ -0,0 +1,19 @@ +#!/usr/bin/env bash + +set -euo pipefail + +FUNCTION_NAME=$1 + +echo "Getting ARN of newest Sentry lambda layer..." +LAYER_ARN=$(aws lambda list-layer-versions --layer-name SentryPythonServerlessSDKLocalDev --query "LayerVersions[0].LayerVersionArn" | tr -d '"') +echo "Done getting ARN of newest Sentry lambda layer $LAYER_ARN." + +echo "Attaching Lamba layer to function $FUNCTION_NAME..." +echo "Warning: This remove all other layers!" +aws lambda update-function-configuration \ + --function-name "$FUNCTION_NAME" \ + --layers "$LAYER_ARN" \ + --no-cli-pager +echo "Done attaching Lamba layer to function '$FUNCTION_NAME'." + +echo "All done. Have a nice day!" diff --git a/scripts/aws-deploy-local-layer.sh b/scripts/aws-deploy-local-layer.sh new file mode 100755 index 0000000000..d74eec1977 --- /dev/null +++ b/scripts/aws-deploy-local-layer.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash + +set -euo pipefail + + +# Creating Lambda layer +echo "Creating Lambda layer in ./dist-serverless ..." +make aws-lambda-layer +echo "Done creating Lambda layer in ./dist-serverless." + + +# Adding Sentry Lambda extension to Lambda layer +echo "Adding Sentry Lambda extension to Lambda layer in ./dist-serverless ..." +mkdir -p dist-serverless/extensions +curl -0 --silent --output dist-serverless/extensions/sentry-lambda-extension `curl -s https://release-registry.services.sentry.io/apps/sentry-lambda-extension/latest | jq -r .files.\"sentry-lambda-extension\".url` +chmod +x dist-serverless/extensions/sentry-lambda-extension +echo "Done adding Sentry Lambda extension to Lambda layer in ./dist-serverless." + +echo "Setting configuration for extension in in ./dist-serverless/extensions ..." +mkdir -p dist-serverless/extensions/.relay/ +cat << EOT >> dist-serverless/extensions/.relay/config.yml +--- +relay: + mode: proxy + upstream: "https://sentry.io" + host: 127.0.0.1 + port: 3000 +EOT +echo "Done setting configuration for extension in in ./dist-serverless/extensions ..." + + +# Zip Lambda layer and included Lambda extension +echo "Zipping Lambda layer and included Lambda extension..." +zip -r sentry-python-serverless-x.x.x-dev.zip \ + dist-serverless/ \ + -x \*__pycache__\* -x \*.yml +echo "Done Zipping Lambda layer and included Lambda extension to ./sentry-python-serverless-x.x.x-dev.zip." + + +# Deploying zipped Lambda layer to AWS +echo "Deploying zipped Lambda layer to AWS..." + +aws lambda publish-layer-version \ + --layer-name "SentryPythonServerlessSDKLocalDev" \ + --region "eu-central-1" \ + --zip-file "fileb://sentry-python-serverless-x.x.x-dev.zip" \ + --description "Local test build of SentryPythonServerlessSDK (can be deleted)" \ + --no-cli-pager + +echo "Done deploying zipped Lambda layer to AWS as 'SentryPythonServerlessSDKLocalDev'." + +echo "All done. Have a nice day!" From dd1efe214e85d919740216fd57e4a5f98f46deb7 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 22 Apr 2022 16:09:19 +0200 Subject: [PATCH 6/9] Added check for argument --- scripts/aws-attach-layer-to-lambda-function.sh | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/scripts/aws-attach-layer-to-lambda-function.sh b/scripts/aws-attach-layer-to-lambda-function.sh index be702ae4e5..e5c8c05e91 100755 --- a/scripts/aws-attach-layer-to-lambda-function.sh +++ b/scripts/aws-attach-layer-to-lambda-function.sh @@ -2,6 +2,17 @@ set -euo pipefail +# Check for argument +if [ $# -eq 0 ] + then + SCRIPT_NAME=$(basename "$0") + echo "ERROR: No argument supplied. Please give the name of a Lambda function!" + echo "" + echo "Usage: $SCRIPT_NAME " + echo "" + exit 1 +fi + FUNCTION_NAME=$1 echo "Getting ARN of newest Sentry lambda layer..." From 7c4dca1f1be3ef6af3912e12ab5353d52399e976 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 22 Apr 2022 16:12:22 +0200 Subject: [PATCH 7/9] Added link to action to make sure it does the same --- scripts/aws-deploy-local-layer.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/scripts/aws-deploy-local-layer.sh b/scripts/aws-deploy-local-layer.sh index d74eec1977..7fc5890a9a 100755 --- a/scripts/aws-deploy-local-layer.sh +++ b/scripts/aws-deploy-local-layer.sh @@ -9,6 +9,11 @@ make aws-lambda-layer echo "Done creating Lambda layer in ./dist-serverless." +# IMPORTANT: +# Please make sure that this does the same as the GitHub action that +# is building the Lambda layer in production! +# see: https://github.com/getsentry/action-build-aws-lambda-extension/blob/main/action.yml#L23-L40 + # Adding Sentry Lambda extension to Lambda layer echo "Adding Sentry Lambda extension to Lambda layer in ./dist-serverless ..." mkdir -p dist-serverless/extensions @@ -25,6 +30,8 @@ relay: upstream: "https://sentry.io" host: 127.0.0.1 port: 3000 +limits: + shutdown_timeout: 2 EOT echo "Done setting configuration for extension in in ./dist-serverless/extensions ..." From 475d328e2fd8c72d4766f4ad6f6150dd15d2827a Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Fri, 22 Apr 2022 16:28:50 +0200 Subject: [PATCH 8/9] Added contributing guide for AWS lambda --- CONTRIBUTING-aws-lambda.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 CONTRIBUTING-aws-lambda.md diff --git a/CONTRIBUTING-aws-lambda.md b/CONTRIBUTING-aws-lambda.md new file mode 100644 index 0000000000..3b679373d1 --- /dev/null +++ b/CONTRIBUTING-aws-lambda.md @@ -0,0 +1,21 @@ +# Contributing to Sentry AWS Lambda Layer + +All the general terms of the [CONTRIBUTING.md](CONTRIBUTING.md) apply. + +## Development environment + +You need to have a AWS account and AWS CLI installed and setup. + +We put together two helper functions that can help you with development: + +- `./scripts/aws-deploy-local-layer.sh` + + This script [scripts/aws-deploy-local-layer.sh](scripts/aws-deploy-local-layer.sh) will take the code you have checked out locally, create a Lambda layer out of it and deploy it to the `eu-central-1` region of your configured AWS account using `aws` CLI. + + The Lambda layer will have the name `SentryPythonServerlessSDKLocalDev` + +- `./scripts/aws-attach-layer-to-lambda-function.sh` + + You can use this script [scripts/aws-attach-layer-to-lambda-function.sh](scripts/aws-attach-layer-to-lambda-function.sh) to attach the Lambda layer you just deployed (using the first script) onto one of your existing Lambda functions. You will have to give the name of the Lambda function to attach onto as an argument. (See the script for details.) + +With this two helper scripts it should be easy to rapidly iterate your development on the Lambda layer. From 67d4a24f04a5f567988348aef72cf8cbbabf5d98 Mon Sep 17 00:00:00 2001 From: Anton Pirker Date: Thu, 5 May 2022 16:40:57 +0200 Subject: [PATCH 9/9] Whitespace --- scripts/aws-deploy-local-layer.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/aws-deploy-local-layer.sh b/scripts/aws-deploy-local-layer.sh index 7fc5890a9a..e492b8d184 100755 --- a/scripts/aws-deploy-local-layer.sh +++ b/scripts/aws-deploy-local-layer.sh @@ -2,7 +2,6 @@ set -euo pipefail - # Creating Lambda layer echo "Creating Lambda layer in ./dist-serverless ..." make aws-lambda-layer