From e0209db8076aaf4d2f90d83fe5379f8591c5d8ee Mon Sep 17 00:00:00 2001 From: Neel Shah Date: Wed, 10 May 2023 13:47:36 +0200 Subject: [PATCH] Remove relay extension from AWS Layer (#2068) we're reverting back to the older setup since the whole 'relay as AWS extension' experiment didn't really work out. * revert port override in DSN * remove gh action that bundles relay * zip in place as part of `make build_aws_lambda_layer` part of https://github.com/getsentry/team-webplatform-meta/issues/58 --- .github/workflows/ci.yml | 12 ------ Makefile | 1 + scripts/aws-delete-lamba-layer-versions.sh | 2 +- scripts/aws-deploy-local-layer.sh | 47 +++------------------- scripts/build_aws_lambda_layer.py | 28 +++++++++++-- scripts/init_serverless_sdk.py | 10 +---- 6 files changed, 33 insertions(+), 67 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7cbf7f36b6..8c397adabb 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -68,18 +68,6 @@ jobs: pip install virtualenv # This will also trigger "make dist" that creates the Python packages make aws-lambda-layer - - echo "Saving SDK_VERSION for later" - export SDK_VERSION=$(grep "VERSION = " sentry_sdk/consts.py | cut -f3 -d' ' | tr -d '"') - echo "SDK_VERSION=$SDK_VERSION" - echo "SDK_VERSION=$SDK_VERSION" >> $GITHUB_ENV - - name: Upload Python AWS Lambda Layer - uses: getsentry/action-build-aws-lambda-extension@v1 - with: - artifact_name: ${{ github.sha }} - zip_file_name: sentry-python-serverless-${{ env.SDK_VERSION }}.zip - build_cache_paths: ${{ env.CACHED_BUILD_PATHS }} - build_cache_key: ${{ env.BUILD_CACHE_KEY }} - name: Upload Python Packages uses: actions/upload-artifact@v3 with: diff --git a/Makefile b/Makefile index 339a68c069..a4d07279da 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ help: dist: .venv rm -rf dist dist-serverless build + $(VENV_PATH)/bin/pip install wheel $(VENV_PATH)/bin/python setup.py sdist bdist_wheel .PHONY: dist diff --git a/scripts/aws-delete-lamba-layer-versions.sh b/scripts/aws-delete-lamba-layer-versions.sh index 5e1ea38a85..f467f9398b 100755 --- a/scripts/aws-delete-lamba-layer-versions.sh +++ b/scripts/aws-delete-lamba-layer-versions.sh @@ -8,7 +8,7 @@ set -euo pipefail # override default AWS region export AWS_REGION=eu-central-1 -LAYER_NAME=SentryPythonServerlessSDKLocalDev +LAYER_NAME=SentryPythonServerlessSDK-local-dev VERSION="0" while [[ $VERSION != "1" ]] diff --git a/scripts/aws-deploy-local-layer.sh b/scripts/aws-deploy-local-layer.sh index 9e2d7c795e..3f213849f3 100755 --- a/scripts/aws-deploy-local-layer.sh +++ b/scripts/aws-deploy-local-layer.sh @@ -9,55 +9,20 @@ set -euo pipefail # Creating Lambda layer -echo "Creating Lambda layer in ./dist-serverless ..." +echo "Creating Lambda layer in ./dist ..." make aws-lambda-layer -echo "Done creating Lambda layer in ./dist-serverless." - -# IMPORTANT: -# Please make sure that this part 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 - -echo "Downloading relay..." -mkdir -p dist-serverless/relay -curl -0 --silent \ - --output dist-serverless/relay/relay \ - "$(curl -s https://release-registry.services.sentry.io/apps/relay/latest | jq -r .files.\"relay-Linux-x86_64\".url)" -chmod +x dist-serverless/relay/relay -echo "Done downloading relay." - -echo "Creating start script..." -mkdir -p dist-serverless/extensions -cat > dist-serverless/extensions/sentry-lambda-extension << EOT -#!/bin/bash -set -euo pipefail -exec /opt/relay/relay run \ - --mode=proxy \ - --shutdown-timeout=2 \ - --upstream-dsn="\$SENTRY_DSN" \ - --aws-runtime-api="\$AWS_LAMBDA_RUNTIME_API" -EOT -chmod +x dist-serverless/extensions/sentry-lambda-extension -echo "Done creating start script." - -# Zip Lambda layer and included Lambda extension -echo "Zipping Lambda layer and included Lambda extension..." -cd dist-serverless/ -zip -r ../sentry-python-serverless-x.x.x-dev.zip \ - . \ - --exclude \*__pycache__\* --exclude \*.yml -cd .. -echo "Done Zipping Lambda layer and included Lambda extension to ./sentry-python-serverless-x.x.x-dev.zip." - +echo "Done creating Lambda layer in ./dist" # Deploying zipped Lambda layer to AWS -echo "Deploying zipped Lambda layer to AWS..." +ZIP=$(ls dist | grep serverless | head -n 1) +echo "Deploying zipped Lambda layer $ZIP to AWS..." aws lambda publish-layer-version \ --layer-name "SentryPythonServerlessSDK-local-dev" \ --region "eu-central-1" \ - --zip-file "fileb://sentry-python-serverless-x.x.x-dev.zip" \ + --zip-file "fileb://dist/$ZIP" \ --description "Local test build of SentryPythonServerlessSDK (can be deleted)" \ + --compatible-runtimes python3.6 python3.7 python3.8 python3.9 --no-cli-pager echo "Done deploying zipped Lambda layer to AWS as 'SentryPythonServerlessSDK-local-dev'." diff --git a/scripts/build_aws_lambda_layer.py b/scripts/build_aws_lambda_layer.py index d694d15ba7..829b7e31d9 100644 --- a/scripts/build_aws_lambda_layer.py +++ b/scripts/build_aws_lambda_layer.py @@ -17,6 +17,7 @@ def __init__( # type: (...) -> None self.base_dir = base_dir self.python_site_packages = os.path.join(self.base_dir, PYTHON_SITE_PACKAGES) + self.out_zip_filename = f"sentry-python-serverless-{SDK_VERSION}.zip" def make_directories(self): # type: (...) -> None @@ -57,16 +58,35 @@ def create_init_serverless_sdk_package(self): "scripts/init_serverless_sdk.py", f"{serverless_sdk_path}/__init__.py" ) + def zip(self): + # type: (...) -> None + subprocess.run( + [ + "zip", + "-q", # Quiet + "-x", # Exclude files + "**/__pycache__/*", # Files to be excluded + "-r", # Recurse paths + self.out_zip_filename, # Output filename + PYTHON_SITE_PACKAGES, # Files to be zipped + ], + cwd=self.base_dir, + check=True, # Raises CalledProcessError if exit status is non-zero + ) -def build_layer_dir(): + shutil.copy( + os.path.join(self.base_dir, self.out_zip_filename), + os.path.abspath(DIST_PATH) + ) + +def build_packaged_zip(): with tempfile.TemporaryDirectory() as base_dir: layer_builder = LayerBuilder(base_dir) layer_builder.make_directories() layer_builder.install_python_packages() layer_builder.create_init_serverless_sdk_package() - - shutil.copytree(base_dir, "dist-serverless") + layer_builder.zip() if __name__ == "__main__": - build_layer_dir() + build_packaged_zip() diff --git a/scripts/init_serverless_sdk.py b/scripts/init_serverless_sdk.py index 05dd8c767a..e2c9f536f8 100644 --- a/scripts/init_serverless_sdk.py +++ b/scripts/init_serverless_sdk.py @@ -18,17 +18,9 @@ from typing import Any -def extension_relay_dsn(original_dsn): - dsn = Dsn(original_dsn) - dsn.host = "localhost" - dsn.port = 5333 - dsn.scheme = "http" - return str(dsn) - - # Configure Sentry SDK sentry_sdk.init( - dsn=extension_relay_dsn(os.environ["SENTRY_DSN"]), + dsn=os.environ["SENTRY_DSN"], integrations=[AwsLambdaIntegration(timeout_warning=True)], traces_sample_rate=float(os.environ["SENTRY_TRACES_SAMPLE_RATE"]), )