Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed May 3, 2023
1 parent 8d4ac5a commit daa3804
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 67 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion scripts/aws-delete-lamba-layer-versions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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" ]]
Expand Down
47 changes: 6 additions & 41 deletions scripts/aws-deploy-local-layer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'."
Expand Down
28 changes: 24 additions & 4 deletions scripts/build_aws_lambda_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
10 changes: 1 addition & 9 deletions scripts/init_serverless_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]),
)
Expand Down

0 comments on commit daa3804

Please sign in to comment.