Skip to content

Commit 0352c79

Browse files
Serverless V2 (#1450)
* Build new Lambda extension (#1383) * Use new GitHub action for creating Lambda layer zip. * Use new GitHub action for creating zip. * Replace original DSN host/port with localhost:3000 (#1414) * Added script for locally building/release Lambda layer * Added script to attach layer to function Co-authored-by: Neel Shah <neel.shah@sentry.io>
1 parent 3d38329 commit 0352c79

11 files changed

+295
-180
lines changed

.github/workflows/ci.yml

Lines changed: 68 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: ci
1+
name: CI
22

33
on:
44
push:
@@ -11,55 +11,16 @@ on:
1111
permissions:
1212
contents: read
1313

14-
jobs:
15-
dist:
16-
name: distribution packages
17-
timeout-minutes: 10
18-
runs-on: ubuntu-latest
19-
20-
steps:
21-
- uses: actions/checkout@v3
22-
- uses: actions/setup-node@v3
23-
- uses: actions/setup-python@v3
24-
with:
25-
python-version: 3.9
26-
27-
- run: |
28-
pip install virtualenv
29-
make aws-lambda-layer-build
30-
31-
- uses: actions/upload-artifact@v3
32-
with:
33-
name: ${{ github.sha }}
34-
path: |
35-
dist/*
36-
dist-serverless/*
37-
38-
docs:
39-
timeout-minutes: 10
40-
name: build documentation
41-
runs-on: ubuntu-latest
42-
43-
steps:
44-
- uses: actions/checkout@v3
45-
- uses: actions/setup-node@v3
46-
- uses: actions/setup-python@v3
47-
with:
48-
python-version: 3.9
49-
50-
- run: |
51-
pip install virtualenv
52-
make apidocs
53-
cd docs/_build && zip -r gh-pages ./
54-
55-
- uses: actions/upload-artifact@v3
56-
with:
57-
name: ${{ github.sha }}
58-
path: docs/_build/gh-pages.zip
14+
env:
15+
BUILD_CACHE_KEY: ${{ github.sha }}
16+
CACHED_BUILD_PATHS: |
17+
${{ github.workspace }}/dist-serverless
5918
19+
jobs:
6020
lint:
61-
timeout-minutes: 10
21+
name: Lint Sources
6222
runs-on: ubuntu-latest
23+
timeout-minutes: 10
6324

6425
steps:
6526
- uses: actions/checkout@v3
@@ -72,9 +33,10 @@ jobs:
7233
tox -e linters
7334
7435
test:
75-
continue-on-error: true
76-
timeout-minutes: 45
36+
name: Run Tests
7737
runs-on: ${{ matrix.linux-version }}
38+
timeout-minutes: 45
39+
continue-on-error: true
7840
strategy:
7941
matrix:
8042
linux-version: [ubuntu-latest]
@@ -128,7 +90,7 @@ jobs:
12890
with:
12991
python-version: ${{ matrix.python-version }}
13092

131-
- name: setup
93+
- name: Setup Test Env
13294
env:
13395
PGHOST: localhost
13496
PGPASSWORD: sentry
@@ -137,7 +99,7 @@ jobs:
13799
psql -c 'create database test_travis_ci_test;' -U postgres
138100
pip install codecov tox
139101
140-
- name: run tests
102+
- name: Run Tests
141103
env:
142104
CI_PYTHON_VERSION: ${{ matrix.python-version }}
143105
timeout-minutes: 45
@@ -147,3 +109,58 @@ jobs:
147109
coverage combine .coverage*
148110
coverage xml -i
149111
codecov --file coverage.xml
112+
113+
build_lambda_layer:
114+
name: Build AWS Lambda Layer
115+
runs-on: ubuntu-latest
116+
timeout-minutes: 10
117+
118+
steps:
119+
- uses: actions/checkout@v2
120+
- uses: actions/setup-node@v1
121+
- uses: actions/setup-python@v2
122+
with:
123+
python-version: 3.9
124+
- name: Setup build cache
125+
uses: actions/cache@v2
126+
id: build_cache
127+
with:
128+
path: ${{ env.CACHED_BUILD_PATHS }}
129+
key: ${{ env.BUILD_CACHE_KEY }}
130+
- run: |
131+
echo "Creating directory containing Python SDK Lambda Layer"
132+
pip install virtualenv
133+
make aws-lambda-layer
134+
135+
echo "Saving SDK_VERSION for later"
136+
export SDK_VERSION=$(grep "VERSION = " sentry_sdk/consts.py | cut -f3 -d' ' | tr -d '"')
137+
echo "SDK_VERSION=$SDK_VERSION"
138+
echo "SDK_VERSION=$SDK_VERSION" >> $GITHUB_ENV
139+
- uses: getsentry/action-build-aws-lambda-extension@v1
140+
with:
141+
artifact_name: ${{ github.sha }}
142+
zip_file_name: sentry-python-serverless-${{ env.SDK_VERSION }}.zip
143+
build_cache_paths: ${{ env.CACHED_BUILD_PATHS }}
144+
build_cache_key: ${{ env.BUILD_CACHE_KEY }}
145+
146+
docs:
147+
name: Build SDK API Doc
148+
runs-on: ubuntu-latest
149+
timeout-minutes: 10
150+
151+
steps:
152+
- uses: actions/checkout@v2
153+
- uses: actions/setup-node@v1
154+
- uses: actions/setup-python@v2
155+
with:
156+
python-version: 3.9
157+
158+
- run: |
159+
pip install virtualenv
160+
make apidocs
161+
cd docs/_build && zip -r gh-pages ./
162+
163+
- uses: actions/upload-artifact@v2
164+
with:
165+
name: ${{ github.sha }}
166+
path: docs/_build/gh-pages.zip

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pip-log.txt
1212
/build
1313
/dist
1414
/dist-serverless
15+
sentry-python-serverless*.zip
1516
.cache
1617
.idea
1718
.eggs

CONTRIBUTING-aws-lambda.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Contributing to Sentry AWS Lambda Layer
2+
3+
All the general terms of the [CONTRIBUTING.md](CONTRIBUTING.md) apply.
4+
5+
## Development environment
6+
7+
You need to have a AWS account and AWS CLI installed and setup.
8+
9+
We put together two helper functions that can help you with development:
10+
11+
- `./scripts/aws-deploy-local-layer.sh`
12+
13+
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.
14+
15+
The Lambda layer will have the name `SentryPythonServerlessSDK-local-dev`
16+
17+
- `./scripts/aws-attach-layer-to-lambda-function.sh`
18+
19+
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.)
20+
21+
With this two helper scripts it should be easy to rapidly iterate your development on the Lambda layer.

Makefile

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ help:
99
@echo "make test: Run basic tests (not testing most integrations)"
1010
@echo "make test-all: Run ALL tests (slow, closest to CI)"
1111
@echo "make format: Run code formatters (destructive)"
12-
@echo "make aws-lambda-layer-build: Build serverless ZIP dist package"
12+
@echo "make aws-lambda-layer: Build AWS Lambda layer directory for serverless integration"
1313
@echo
1414
@echo "Also make sure to read ./CONTRIBUTING.md"
1515
@false
@@ -19,9 +19,8 @@ help:
1919
$(VENV_PATH)/bin/pip install tox
2020

2121
dist: .venv
22-
rm -rf dist build
22+
rm -rf dist dist-serverless build
2323
$(VENV_PATH)/bin/python setup.py sdist bdist_wheel
24-
2524
.PHONY: dist
2625

2726
format: .venv
@@ -46,7 +45,6 @@ lint: .venv
4645
echo "Bad formatting? Run: make format"; \
4746
echo "================================"; \
4847
false)
49-
5048
.PHONY: lint
5149

5250
apidocs: .venv
@@ -60,8 +58,8 @@ apidocs-hotfix: apidocs
6058
@$(VENV_PATH)/bin/ghp-import -pf docs/_build
6159
.PHONY: apidocs-hotfix
6260

63-
aws-lambda-layer-build: dist
61+
aws-lambda-layer: dist
6462
$(VENV_PATH)/bin/pip install urllib3
6563
$(VENV_PATH)/bin/pip install certifi
66-
$(VENV_PATH)/bin/python -m scripts.build_awslambda_layer
67-
.PHONY: aws-lambda-layer-build
64+
$(VENV_PATH)/bin/python -m scripts.build_aws_lambda_layer
65+
.PHONY: aws-lambda-layer
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Attaches the layer `SentryPythonServerlessSDK-local-dev` to a given lambda function.
4+
#
5+
6+
set -euo pipefail
7+
8+
# Check for argument
9+
if [ $# -eq 0 ]
10+
then
11+
SCRIPT_NAME=$(basename "$0")
12+
echo "ERROR: No argument supplied. Please give the name of a Lambda function!"
13+
echo ""
14+
echo "Usage: $SCRIPT_NAME <lambda-function-name>"
15+
echo ""
16+
exit 1
17+
fi
18+
19+
FUNCTION_NAME=$1
20+
21+
echo "Getting ARN of newest Sentry lambda layer..."
22+
LAYER_ARN=$(aws lambda list-layer-versions --layer-name SentryPythonServerlessSDK-local-dev --query "LayerVersions[0].LayerVersionArn" | tr -d '"')
23+
echo "Done getting ARN of newest Sentry lambda layer $LAYER_ARN."
24+
25+
echo "Attaching Lamba layer to function $FUNCTION_NAME..."
26+
echo "Warning: This remove all other layers!"
27+
aws lambda update-function-configuration \
28+
--function-name "$FUNCTION_NAME" \
29+
--layers "$LAYER_ARN" \
30+
--no-cli-pager
31+
echo "Done attaching Lamba layer to function '$FUNCTION_NAME'."
32+
33+
echo "All done. Have a nice day!"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Deletes all versions of the layer specified in LAYER_NAME in one region.
4+
#
5+
6+
set -euo pipefail
7+
8+
# override default AWS region
9+
export AWS_REGION=eu-central-1
10+
11+
LAYER_NAME=SentryPythonServerlessSDKLocalDev
12+
VERSION="0"
13+
14+
while [[ $VERSION != "1" ]]
15+
do
16+
VERSION=$(aws lambda list-layer-versions --layer-name $LAYER_NAME | jq '.LayerVersions[0].Version')
17+
aws lambda delete-layer-version --layer-name $LAYER_NAME --version-number $VERSION
18+
done

scripts/aws-deploy-local-layer.sh

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Builds and deploys the Sentry AWS Lambda layer (including the Sentry SDK and the Sentry Lambda Extension)
4+
#
5+
# The currently checked out version of the SDK in your local directory is used.
6+
# The latest version of the Lambda Extension is fetched from the Sentry Release Registry.
7+
#
8+
9+
set -euo pipefail
10+
11+
# Creating Lambda layer
12+
echo "Creating Lambda layer in ./dist-serverless ..."
13+
make aws-lambda-layer
14+
echo "Done creating Lambda layer in ./dist-serverless."
15+
16+
# IMPORTANT:
17+
# Please make sure that this part does the same as the GitHub action that
18+
# is building the Lambda layer in production!
19+
# see: https://github.com/getsentry/action-build-aws-lambda-extension/blob/main/action.yml#L23-L40
20+
21+
echo "Downloading relay..."
22+
mkdir -p dist-serverless/relay
23+
curl -0 --silent \
24+
--output dist-serverless/relay/relay \
25+
"$(curl -s https://release-registry.services.sentry.io/apps/relay/latest | jq -r .files.\"relay-Linux-x86_64\".url)"
26+
chmod +x dist-serverless/relay/relay
27+
echo "Done downloading relay."
28+
29+
echo "Creating start script..."
30+
mkdir -p dist-serverless/extensions
31+
cat > dist-serverless/extensions/sentry-lambda-extension << EOT
32+
#!/bin/bash
33+
set -euo pipefail
34+
exec /opt/relay/relay run \
35+
--mode=proxy \
36+
--shutdown-timeout=2 \
37+
--upstream-dsn="\$SENTRY_DSN" \
38+
--aws-runtime-api="\$AWS_LAMBDA_RUNTIME_API"
39+
EOT
40+
chmod +x dist-serverless/extensions/sentry-lambda-extension
41+
echo "Done creating start script."
42+
43+
# Zip Lambda layer and included Lambda extension
44+
echo "Zipping Lambda layer and included Lambda extension..."
45+
cd dist-serverless/
46+
zip -r ../sentry-python-serverless-x.x.x-dev.zip \
47+
. \
48+
--exclude \*__pycache__\* --exclude \*.yml
49+
cd ..
50+
echo "Done Zipping Lambda layer and included Lambda extension to ./sentry-python-serverless-x.x.x-dev.zip."
51+
52+
53+
# Deploying zipped Lambda layer to AWS
54+
echo "Deploying zipped Lambda layer to AWS..."
55+
56+
aws lambda publish-layer-version \
57+
--layer-name "SentryPythonServerlessSDK-local-dev" \
58+
--region "eu-central-1" \
59+
--zip-file "fileb://sentry-python-serverless-x.x.x-dev.zip" \
60+
--description "Local test build of SentryPythonServerlessSDK (can be deleted)" \
61+
--no-cli-pager
62+
63+
echo "Done deploying zipped Lambda layer to AWS as 'SentryPythonServerlessSDK-local-dev'."
64+
65+
echo "All done. Have a nice day!"

0 commit comments

Comments
 (0)