Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions scripts/test-lambda-locally/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.envrc
.venv/
package/
lambda_deployment_package.zip
28 changes: 28 additions & 0 deletions scripts/test-lambda-locally/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Test AWS Lambda functions locally

An easy way to run an AWS Lambda function with the Sentry SDK locally.

This is a small helper to create a AWS Lambda function that includes the
currently checked out Sentry SDK and runs it in a local AWS Lambda environment.

Currently only embedding the Sentry SDK into the Lambda function package
is supported. Adding the SDK as Lambda Layer is not possible at the moment.

## Prerequisites

- Set `SENTRY_DSN` environment variable. The Lambda function will use this DSN.
- You need to have Docker installed and running.

## Run Lambda function

- Update `lambda_function.py` to include your test code.
- Run `./deploy-lambda-locally.sh`. This will:
- Install [AWS SAM](https://aws.amazon.com/serverless/sam/) in an virtual Python environment
- Create a lambda function package in `package/` that includes
- The currently checked out Sentry SDK
- All dependencies of the Sentry SDK (certifi and urllib3)
- The actual function defined in `lamdba_function.py`.
- Zip everything together into lambda_deployment_package.zip
- Run a local Lambda environment that serves that Lambda function.
- Point your browser to `http://127.0.0.1:3000` to access your Lambda function.
- Currently GET and POST requests are possible. This is defined in `template.yaml`.
25 changes: 25 additions & 0 deletions scripts/test-lambda-locally/deploy-lambda-locally.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env bash

# exit on first error
set -xe

# Setup local AWS Lambda environment

# Install uv if it's not installed
if ! command -v uv &> /dev/null; then
curl -LsSf https://astral.sh/uv/install.sh | sh
fi

uv sync

# Create a deployment package of the lambda function in `lambda_function.py`.
rm -rf package && mkdir -p package
pip install -e ../../../sentry-python -t package/ --upgrade
cp lambda_function.py package/
cd package && zip -r ../lambda_deployment_package.zip . && cd ..

# Start the local Lambda server with the new function (defined in template.yaml)
uv run sam local start-api \
--skip-pull-image \
--force-image-build \
--parameter-overrides SentryDsn=$SENTRY_DSN
24 changes: 24 additions & 0 deletions scripts/test-lambda-locally/lambda_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import logging
import sentry_sdk

from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
from sentry_sdk.integrations.logging import LoggingIntegration

def lambda_handler(event, context):
sentry_sdk.init(
dsn="https://[email protected]/5461230",
attach_stacktrace=True,
integrations=[
LoggingIntegration(level=logging.INFO, event_level=logging.ERROR),
AwsLambdaIntegration(timeout_warning=True)
],
traces_sample_rate=1.0,
debug=True,
)

try:
my_dict = {"a" : "test"}
value = my_dict["b"] # This should raise exception
except:
logging.exception("Key Does not Exists")
raise
8 changes: 8 additions & 0 deletions scripts/test-lambda-locally/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
name = "test-lambda-locally"
version = "0"
requires-python = ">=3.12"

dependencies = [
"aws-sam-cli>=1.135.0",
]
29 changes: 29 additions & 0 deletions scripts/test-lambda-locally/template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
SentryLambdaFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: lambda_deployment_package.zip
Handler: lambda_function.lambda_handler
Runtime: python3.12
Timeout: 30
Environment:
Variables:
SENTRY_DSN: !Ref SentryDsn
Events:
ApiEventGet:
Type: Api
Properties:
Path: /
Method: get
ApiEventPost:
Type: Api
Properties:
Path: /
Method: post

Parameters:
SentryDsn:
Type: String
Default: ''
Loading
Loading