-
Notifications
You must be signed in to change notification settings - Fork 569
A way to locally run AWS Lambda functions #4128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6cab674
A way to locally run AWS Lambda functions
antonpirker 219cca7
Merge branch 'master' into antonpirker/local-aws-lambda
antonpirker 722550b
Cleanup
antonpirker 23fbefa
Update scripts/test-lambda-locally/README.md
antonpirker 0321570
Merge branch 'master' into antonpirker/local-aws-lambda
antonpirker 933fbbf
Merge branch 'master' into antonpirker/local-aws-lambda
antonpirker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| .envrc | ||
| .venv/ | ||
| package/ | ||
| lambda_deployment_package.zip |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
antonpirker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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: '' |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.