Skip to content

Commit 42ad8df

Browse files
A way to locally run AWS Lambda functions (#4128)
This gives us a way to locally run and test our AWS Lambda integration, without needing a real AWS Lambda account. This should make development of AWS Lambda support better. --------- Co-authored-by: Ivana Kellyer <[email protected]>
1 parent e8be8ed commit 42ad8df

File tree

7 files changed

+1358
-0
lines changed

7 files changed

+1358
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.envrc
2+
.venv/
3+
package/
4+
lambda_deployment_package.zip

scripts/test-lambda-locally/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Test AWS Lambda functions locally
2+
3+
An easy way to run an AWS Lambda function with the Sentry SDK locally.
4+
5+
This is a small helper to create a AWS Lambda function that includes the
6+
currently checked out Sentry SDK and runs it in a local AWS Lambda environment.
7+
8+
Currently only embedding the Sentry SDK into the Lambda function package
9+
is supported. Adding the SDK as Lambda Layer is not possible at the moment.
10+
11+
## Prerequisites
12+
13+
- Set `SENTRY_DSN` environment variable. The Lambda function will use this DSN.
14+
- You need to have Docker installed and running.
15+
16+
## Run Lambda function
17+
18+
- Update `lambda_function.py` to include your test code.
19+
- Run `./deploy-lambda-locally.sh`. This will:
20+
- Install [AWS SAM](https://aws.amazon.com/serverless/sam/) in a virtual Python environment
21+
- Create a lambda function package in `package/` that includes
22+
- The currently checked out Sentry SDK
23+
- All dependencies of the Sentry SDK (certifi and urllib3)
24+
- The actual function defined in `lamdba_function.py`.
25+
- Zip everything together into lambda_deployment_package.zip
26+
- Run a local Lambda environment that serves that Lambda function.
27+
- Point your browser to `http://127.0.0.1:3000` to access your Lambda function.
28+
- Currently GET and POST requests are possible. This is defined in `template.yaml`.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
# exit on first error
4+
set -xeuo pipefail
5+
6+
# Setup local AWS Lambda environment
7+
8+
# Install uv if it's not installed
9+
if ! command -v uv &> /dev/null; then
10+
curl -LsSf https://astral.sh/uv/install.sh | sh
11+
fi
12+
13+
uv sync
14+
15+
# Create a deployment package of the lambda function in `lambda_function.py`.
16+
rm -rf package && mkdir -p package
17+
pip install ../../../sentry-python -t package/ --upgrade
18+
cp lambda_function.py package/
19+
cd package && zip -r ../lambda_deployment_package.zip . && cd ..
20+
21+
# Start the local Lambda server with the new function (defined in template.yaml)
22+
uv run sam local start-api \
23+
--skip-pull-image \
24+
--force-image-build \
25+
--parameter-overrides SentryDsn=$SENTRY_DSN
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import logging
2+
import os
3+
import sentry_sdk
4+
5+
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
6+
from sentry_sdk.integrations.logging import LoggingIntegration
7+
8+
def lambda_handler(event, context):
9+
sentry_sdk.init(
10+
dsn=os.environ.get("SENTRY_DSN"),
11+
attach_stacktrace=True,
12+
integrations=[
13+
LoggingIntegration(level=logging.INFO, event_level=logging.ERROR),
14+
AwsLambdaIntegration(timeout_warning=True)
15+
],
16+
traces_sample_rate=1.0,
17+
debug=True,
18+
)
19+
20+
try:
21+
my_dict = {"a" : "test"}
22+
value = my_dict["b"] # This should raise exception
23+
except:
24+
logging.exception("Key Does not Exists")
25+
raise
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[project]
2+
name = "test-lambda-locally"
3+
version = "0"
4+
requires-python = ">=3.12"
5+
6+
dependencies = [
7+
"aws-sam-cli>=1.135.0",
8+
]
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Transform: AWS::Serverless-2016-10-31
3+
Resources:
4+
SentryLambdaFunction:
5+
Type: AWS::Serverless::Function
6+
Properties:
7+
CodeUri: lambda_deployment_package.zip
8+
Handler: lambda_function.lambda_handler
9+
Runtime: python3.12
10+
Timeout: 30
11+
Environment:
12+
Variables:
13+
SENTRY_DSN: !Ref SentryDsn
14+
Events:
15+
ApiEventGet:
16+
Type: Api
17+
Properties:
18+
Path: /
19+
Method: get
20+
ApiEventPost:
21+
Type: Api
22+
Properties:
23+
Path: /
24+
Method: post
25+
26+
Parameters:
27+
SentryDsn:
28+
Type: String
29+
Default: ''

0 commit comments

Comments
 (0)