Skip to content

Commit abc2400

Browse files
feat: Build dist ZIP for AWS Lambda layers (#1001)
1 parent 7ba60bd commit abc2400

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525

2626
- run: |
2727
pip install virtualenv
28-
make dist
28+
make aws-lambda-layer-build
2929
3030
- uses: actions/upload-artifact@v2
3131
with:

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +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"
1213
@echo
1314
@echo "Also make sure to read ./CONTRIBUTING.md"
1415
@false
@@ -58,3 +59,7 @@ apidocs-hotfix: apidocs
5859
@$(VENV_PATH)/bin/pip install ghp-import
5960
@$(VENV_PATH)/bin/ghp-import -pf docs/_build
6061
.PHONY: apidocs-hotfix
62+
63+
aws-lambda-layer-build: dist
64+
$(VENV_PATH)/bin/python -m scripts.build-awslambda-layer
65+
.PHONY: aws-lambda-layer-build

scripts/build-awslambda-layer.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import os
2+
import subprocess
3+
import tempfile
4+
import shutil
5+
from sentry_sdk.consts import VERSION as SDK_VERSION
6+
7+
8+
DIST_DIRNAME = "dist"
9+
DIST_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", DIST_DIRNAME))
10+
DEST_ZIP_FILENAME = f"sentry-python-serverless-{SDK_VERSION}.zip"
11+
WHEELS_FILEPATH = os.path.join(
12+
DIST_DIRNAME, f"sentry_sdk-{SDK_VERSION}-py2.py3-none-any.whl"
13+
)
14+
15+
# Top directory in the ZIP file. Placing the Sentry package in `/python` avoids
16+
# creating a directory for a specific version. For more information, see
17+
# https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-path
18+
PACKAGE_PARENT_DIRECTORY = "python"
19+
20+
21+
class PackageBuilder:
22+
def __init__(self, base_dir) -> None:
23+
self.base_dir = base_dir
24+
self.packages_dir = self.get_relative_path_of(PACKAGE_PARENT_DIRECTORY)
25+
26+
def make_directories(self):
27+
os.makedirs(self.packages_dir)
28+
29+
def install_python_binaries(self):
30+
subprocess.run(
31+
[
32+
"pip",
33+
"install",
34+
"--no-cache-dir", # Disables the cache -> always accesses PyPI
35+
"-q", # Quiet
36+
WHEELS_FILEPATH, # Copied to the target directory before installation
37+
"-t", # Target directory flag
38+
self.packages_dir,
39+
],
40+
check=True,
41+
)
42+
43+
def zip(self, filename):
44+
subprocess.run(
45+
[
46+
"zip",
47+
"-q", # Quiet
48+
"-x", # Exclude files
49+
"**/__pycache__/*", # Files to be excluded
50+
"-r", # Recurse paths
51+
filename, # Output filename
52+
PACKAGE_PARENT_DIRECTORY, # Files to be zipped
53+
],
54+
cwd=self.base_dir,
55+
check=True, # Raises CalledProcessError if exit status is non-zero
56+
)
57+
58+
def get_relative_path_of(self, subfile):
59+
return os.path.join(self.base_dir, subfile)
60+
61+
62+
def build_packaged_zip():
63+
with tempfile.TemporaryDirectory() as tmp_dir:
64+
package_builder = PackageBuilder(tmp_dir)
65+
package_builder.make_directories()
66+
package_builder.install_python_binaries()
67+
package_builder.zip(DEST_ZIP_FILENAME)
68+
shutil.copy(package_builder.get_relative_path_of(DEST_ZIP_FILENAME), DIST_DIR)
69+
70+
71+
build_packaged_zip()

0 commit comments

Comments
 (0)