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/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. 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..e5c8c05e91 --- /dev/null +++ b/scripts/aws-attach-layer-to-lambda-function.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash + +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..." +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..e492b8d184 --- /dev/null +++ b/scripts/aws-deploy-local-layer.sh @@ -0,0 +1,58 @@ +#!/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." + + +# 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 +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 +limits: + shutdown_timeout: 2 +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!" 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!"