Skip to content

feat(aws-lambda): Add Serverless Framework code snippet for Python #3830

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 5 commits into from
Jul 19, 2021
Merged
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,59 @@ Learn more about [how this integration works](/product/integrations/cloud-monito

## Serverless Framework

Note that this integration will configure your existing lambda functions correctly to use the SDK layer and set the appropriate environment variables as per [how this integration works](/product/integrations/cloud-monitoring/aws-lambda/how-it-works/). You can check the output `aws lambda get-function-configuration --function-name <yourFunctioName>` and this works.
Note that this integration will configure your existing lambda functions correctly to use the SDK layer and set the appropriate environment variables as per [how this integration works](/product/integrations/cloud-monitoring/aws-lambda/how-it-works/). You can check the output `aws lambda get-function-configuration --function-name <yourFunctionName>` and this works.
However when you perform a subsequent `sls deploy` the environment variables are not maintained but it appears the reference to the layer is retained. A workaround exists where you can set the layer definition and environment variables in `serverless.yml`. There are two problems with this, you have to manually add the reference to the SDK layer for every function you want "Sentrified" and you can't maintain the latest layer version automatically.

### Node
```
# other configuration

provider:

# more configuration
region: <AWS_REGION>

environment:
SENTRY_TRACES_SAMPLE_RATE: '1.0'
SENTRY_DSN: '<your sentry dsn>'
SENTRY_DSN: '<SENTRY_DSN>'
NODE_OPTIONS: '-r @sentry/serverless/dist/awslambda-auto'

custom:
layers:
- arn:aws:lambda:${self:provider.region}:943013980633:layer:SentryNodeServerlessSDK:8
- arn:aws:lambda:${self:provider.region}:943013980633:layer:SentryNodeServerlessSDK:26
# - arn:aws:lambda:${self:provider.region}:943013980633:layer:SentryNodeServerlessSDK:latest

myHandler:
handler: src/handlers/myHandler.handler
layers: ${self:custom.layers}
functions:
<yourFunctionName>:
handler: <yourLambdaHandlerFunctionPath>
layers: ${self:custom.layers}
```

### Python
```
# other configuration

provider:

# more configuration
region: <AWS_REGION>

environment:
SENTRY_TRACES_SAMPLE_RATE: '1.0'
SENTRY_DSN: '<SENTRY_DSN>'

custom:
layers:
- arn:aws:lambda:${self:provider.region}:943013980633:layer:SentryPythonServerlessSDK:6
# - arn:aws:lambda:${self:provider.region:943013980633:layer:SentryPythonServerlessSDK:latest

functions:
<yourFunctionName>:
  handler: sentry_sdk.integrations.init_serverless_sdk.sentry_lambda_handler
  environment:
  SENTRY_INITIAL_HANDLER: '<yourLambdaHandlerFunctionPath>'
  layers: ${self:custom.layers}
```

Currently there is no way of using the [latest layer plugin](https://www.npmjs.com/package/serverless-latest-layer-version) since the lambda:ListLayerVersions on resource: arn:aws:lambda:eu-west-1:943013980633:layer:SentryNodeServerlessSDK has not been given the necessary permissions.
It is a relatively new integration but there is defintely a case to write a serverless plugin to make this super-smooth. There is an [older version](https://www.serverless.com/plugins/serverless-sentry-plugin) but it requires code changes to the handler and doesn't use `NODE_OPTIONS`. A new plugin could also add the Sentry layer to every function automatically.