Skip to content
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

Update Python AWS Lambda integration docs. #8246

Merged
merged 36 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
007ecd7
Cleanup and updated screenshots
antonpirker Oct 16, 2023
7d460f4
Its called account id, not number
antonpirker Oct 16, 2023
f4a7787
Improved page structure
antonpirker Oct 16, 2023
707a2ec
style(lint): Auto commit lint changes
getsantry[bot] Oct 16, 2023
8091299
Added confige and verify to aws lambda layer setup
antonpirker Oct 16, 2023
866f416
style(lint): Auto commit lint changes
getsantry[bot] Oct 16, 2023
b368af6
Update AWS Lambda index page
antonpirker Oct 16, 2023
d0bf9f1
Improved manual confugration
antonpirker Oct 16, 2023
07180de
style(lint): Auto commit lint changes
getsantry[bot] Oct 16, 2023
28bd2c9
Improved the AWS Lambda product guide
antonpirker Oct 16, 2023
2b26c49
style(lint): Auto commit lint changes
getsantry[bot] Oct 16, 2023
3288532
again
antonpirker Oct 16, 2023
f2a13b7
moved troubleshooting to where it should be
antonpirker Oct 16, 2023
1b514f8
style(lint): Auto commit lint changes
getsantry[bot] Oct 16, 2023
0967ce0
now its in the correct place
antonpirker Oct 16, 2023
c1574f4
tiny fix
antonpirker Oct 16, 2023
d0e7a7b
Fixed link
antonpirker Oct 16, 2023
7b7776f
Apply suggestions from code review
antonpirker Oct 17, 2023
f85f4f3
Apply suggestions from code review
antonpirker Oct 17, 2023
297d2c2
style(lint): Auto commit lint changes
getsantry[bot] Oct 17, 2023
ec0dfff
style(lint): Auto commit lint changes
getsantry[bot] Oct 17, 2023
2e18e85
style(lint): Auto commit lint changes
getsantry[bot] Oct 17, 2023
8d2cab0
style(lint): Auto commit lint changes
getsantry[bot] Oct 17, 2023
d806f96
Suggestions from docs team
antonpirker Oct 17, 2023
cd53303
Better screenshots product guide
antonpirker Oct 17, 2023
f18075f
Updated screenshots in aws lambda integration
antonpirker Oct 17, 2023
820deeb
Small details
antonpirker Oct 17, 2023
888e741
style(lint): Auto commit lint changes
getsantry[bot] Oct 17, 2023
a54eba9
Apply suggestions from code review
antonpirker Oct 18, 2023
492a264
Merge branch 'master' into antonpirker/python-aws-lambda
antonpirker Oct 18, 2023
85a44f9
Added supported python versions
antonpirker Oct 24, 2023
9aa1b01
[getsentry/action-github-commit] Auto commit
getsantry[bot] Oct 24, 2023
3aca06c
Added some troubleshooting
antonpirker Oct 24, 2023
e6ce38a
[getsentry/action-github-commit] Auto commit
getsantry[bot] Oct 24, 2023
a5b44f3
Changed text of troubleshooting
antonpirker Oct 31, 2023
3de4f19
Merge branch 'master' into antonpirker/python-aws-lambda
antonpirker Oct 31, 2023
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: 2 additions & 2 deletions src/components/lambdaLayerDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function LambdaLayerDetail({canonical}: {canonical: string}) {
{arn && (
<ArnWrapper>
<ArnLabel>ARN</ArnLabel>
{arn}
<code>{arn}</code>
</ArnWrapper>
)}
</Wrapper>
Expand All @@ -85,7 +85,7 @@ export function LambdaLayerDetail({canonical}: {canonical: string}) {

// need a min-height so we don't get cropped at the bottom of the page
const Wrapper = styled('div')`
min-height: 200px;
min-height: 50px;
`;

const ArnWrapper = styled('div')`
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
78 changes: 41 additions & 37 deletions src/docs/product/integrations/cloud-monitoring/aws-lambda/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ Sentry owner, manager, or admin permissions are required to install this integra

1. Scroll to the bottom of the Create Stack page, check the box, then press **Create Stack** to create a CloudFormation stack in your current region. This stack allows Sentry to automatically instrument your Lambda functions.

![](cloudformation.png)
![](cloudformation-create-stack.png)

1. Wait for the stack to be created, which is confirmed by seeing the status change to **Create Complete**, then click **I've created the stack**.
1. Wait for the stack to be created. Once it's confirmed and the status changes to **Create Complete**, click **I've created the stack** in the Sentry popup.

![](aws_account_2.png)
![](cloudformation-stack-create-complete.png)

1. Now copy your AWS account number, select the region you want to use, and press **Next**.
1. Now copy your AWS Account ID, select the region you want to use, and press **Next**.
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

![](stack_created.png)
![](aws_account_2.png)

<Note>

Expand All @@ -66,59 +66,66 @@ Wait a few minutes as the Lambda functions are enabled. If any functions failed

If you need help solving issues with your Sentry AWS Lamba integration integration, you can read the edge cases documented here. If you need additional help, you can [ask on GitHub](https://github.com/getsentry/sentry-javascript/issues/new/choose). Customers on a paid plan may also contact support.
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

#### Error `SentryRole already exists`
- #### Error `SentryRole already exists`

The stack already exists on your account; you can proceed to the next step of adding your AWS account information.
The stack already exists in your account; you can proceed to the next step and add your AWS account information.

#### Error `Invalid existing layer`
- #### Error `Invalid existing layer`

Your Lambda function has a layer that no longer exists. Please remove the problematic layer, then enable Sentry on the Lambda in the configuration view for the integration.
Your Lambda function has a layer that no longer exists. Please remove the problematic layer, then enable Sentry on the Lambda in the configuration view for the integration.

#### Error `Please validate the CloudFormation stack was created successfully`
- #### Error `Please validate the CloudFormation stack was created successfully`

Sentry is unable to access your AWS account through your CloudFormation stack. This means either the stack has not been created or the external ID used doesn't match the one in your installation. You can go to the Parameters tab of the CloudFormation stack to copy the value of the external ID and paste it into the form field.
Sentry is unable to access your AWS account through your CloudFormation stack. This means either the stack has not been created or the external ID used doesn't match the one in your installation. You can go to the Parameters tab of the CloudFormation stack to copy the value of the external ID and paste it into the form field.

#### Error `Invalid role associated with the lambda function`
- #### Error `Invalid role associated with the lambda function`

The role associated with your lambda function no longer exists. Please add a new role to your lambda function, then enable Sentry on the Lambda in the configuration view for the integration.
The role associated with your Lambda function no longer exists. Please add a new role to your Lambda function, then enable Sentry on the Lambda in the configuration view for the integration.

## Configure

The integration configuration view allows you to do the following with your Lambda functions:
The integration configuration view in Sentry allows you to do the following with your Lambda functions:

- Enable Sentry
- Disable Sentry
- Update the Sentry Lambda Layer to the latest version

In the table view, you will also see the version of the Lambda layer below the name.

![](configure.png)
![](sentry-manage-functions.png)

## Verify

Run your AWS Lambda function. Performance data will be sent to Sentry and will show up on the **Performance** page in Sentry. If your function raises an error it will show up on the **Issues** page in Sentry.

## Remove Installation

Note that when the integration is uninstalled, your Lambda functions will still be instrumented with Sentry and you will continue to receive errors. If you want to disable Sentry for all your Lambda functions, you should disable Sentry for each function before uninstalling. Note, you can also uninstall and then reinstall if you would like to change the Sentry project for your Lambdas.

Learn more about [how this integration works](/product/integrations/cloud-monitoring/aws-lambda/how-it-works/).

## Serverless Framework
## Notes on Specific Frameworks

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.
### Serverless Framework
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

### Node
If you are using [Serverless framekwork](https://www.serverless.com/), note that this integration will configure your existing Lambda functions correctly to use the SDK layer and set the appropriate environment variables. Learn more about it [here](/product/integrations/cloud-monitoring/aws-lambda/how-it-works/).
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

```
You can confirm that the output works by inputting `aws lambda get-function-configuration --function-name <yourFunctionName>`. However, when you perform a subsequent `sls deploy` the environment variables aren't maintained even though it looks like the reference to the layer is retained.

You can set the layer definition and environment variables in `serverless.yml` as a workaround, but 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.
antonpirker marked this conversation as resolved.
Show resolved Hide resolved

**Serverless Framework Using Node:**

```yaml {filename:serverless.yml}
# other configuration

provider:

# more configuration
region: <AWS_REGION>

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

custom:
layers:
Expand All @@ -131,19 +138,17 @@ functions:
layers: ${self:custom.layers}
```

### Python
**Serverless Framework Using Python:**

```
```yaml {filename:serverless.yml}
# other configuration

provider:

# more configuration
region: <AWS_REGION>

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

custom:
layers:
Expand All @@ -154,18 +159,17 @@ functions:
<yourFunctionName>:
handler: sentry_sdk.integrations.init_serverless_sdk.sentry_lambda_handler
environment:
SENTRY_INITIAL_HANDLER: '<yourLambdaHandlerFunctionPath>'
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 definitely 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.
Currently, there's 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` hasn't been given the necessary permissions.

## SST
### SST

To use [this integration](/product/integrations/cloud-monitoring/aws-lambda/how-it-works/) with Node functions in [SST](https://serverless-stack.com), you'll need to:
If you use Node functions in [SST](https://serverless-stack.com) and you want to use this integration, you'll need to:

1. Import the [Lambda layer](/platforms/node/guides/aws-lambda/layer/) using the [`LayerVersion`](https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-lambda.LayerVersion.html) construct and set it by calling [`addDefaultFunctionLayers`](https://docs.serverless-stack.com/constructs/Stack#adddefaultfunctionlayers).
2. Then set the `NODE_OPTIONS`, `SENTRY_DSN`, and `SENTRY_TRACES_SAMPLE_RATE` environment variables with the [`addDefaultFunctionEnv`](https://docs.serverless-stack.com/constructs/Stack#adddefaultfunctionenv) method.

You can [read more about this over on the SST docs](https://docs.serverless-stack.com/monitoring-your-app-in-prod#sentry).
Read [how this integration works](/product/integrations/cloud-monitoring/aws-lambda/how-it-works/) and [the SST docs](https://docs.serverless-stack.com/monitoring-your-app-in-prod#sentry) for more information.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
5 changes: 5 additions & 0 deletions src/platform-includes/aws-lambda-environment/python.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
You can configure Sentry by setting these environment variables for your Lambda function:

- `SENTRY_INITIAL_HANDLER`: The handler function of your AWS Lambda funtion.
- `SENTRY_DSN`: This is set to the [DSN](/product/sentry-basics/concepts/dsn-explainer/) of your project
- `SENTRY_TRACES_SAMPLE_RATE`: This sets the [sampling rate](/platforms/node/configuration/sampling/#sampling-transaction-events) for transactions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
title: AWS Lambda Container Image (Python)
title: AWS Lambda Container Image
description: "Learn how to set up Sentry with a Container Image."
sidebar_order: 3000
---

You can also add Sentry to your [Container Image](https://docs.aws.amazon.com/lambda/latest/dg/images-create.html). To import Sentry's Layer Image, add the following to your Dockerfile:
Expand Down
98 changes: 5 additions & 93 deletions src/platforms/python/common/integrations/aws-lambda/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,97 +6,9 @@ redirect_from:
description: "Learn about using Sentry with AWS Lambda."
---

<Note>
You can instrument your AWS Lambda functions in several different ways:

Looking for instructions on how to add Sentry without modifying your code? [Check out these docs instead](/product/integrations/cloud-monitoring/aws-lambda/).

</Note>

Create a deployment package on your local machine and install the required dependencies in the deployment package.
For more information, see [AWS Lambda deployment package in Python](https://docs.aws.amazon.com/lambda/latest/dg/python-package.html).

Install our Python SDK using `pip`:

```bash
pip install --upgrade sentry-sdk
```

We also support [installing Sentry as a Container Image](/platforms/python/integrations/aws-lambda/container-image/) and [installing Sentry in Lambda Layer](/platforms/python/integrations/aws-lambda/layer/).

You can use the AWS Lambda integration for the Python SDK like this:

<SignInNote />

```python
import sentry_sdk
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration

sentry_sdk.init(
dsn="___PUBLIC_DSN___",
integrations=[
AwsLambdaIntegration(),
],

# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production,
traces_sample_rate=1.0,
)

def my_function(event, context):
# ...
```

Check out Sentry's [AWS sample apps](https://github.com/getsentry/examples/tree/master/aws-lambda/python) for detailed examples.

<Alert level="info" title="Note">

If you are using another web framework inside of AWS Lambda, the framework might catch those exceptions before we get to see them. Make sure to enable the framework specific integration as well, if one exists. See [_the Python main page_](/platforms/python/) for more information.

</Alert>

## Timeout Warning

The timeout warning reports an issue when the function execution time is near
the [configured timeout](https://docs.aws.amazon.com/lambda/latest/dg/configuration-console.html).

To enable the warning, update the SDK initialization to set `timeout_warning` to
`true`:

<SignInNote />

```python
sentry_sdk.init(
dsn="___PUBLIC_DSN___",
integrations=[
AwsLambdaIntegration(timeout_warning=True),
],

# Set traces_sample_rate to 1.0 to capture 100%
# of transactions for performance monitoring.
# We recommend adjusting this value in production,
traces_sample_rate=1.0,
)
```

The timeout warning is sent only if the timeout in the Lambda Function configuration is set to a value greater than one second.

## Behavior

With the AWS Lambda integration enabled, the Python SDK will:

- Automatically report all events from your Lambda Functions.
- You can <PlatformLink to="/configuration/filtering/">modify the transaction sample rate</PlatformLink> using `traces_sample_rate`.
- Event reports automatically include:

- A link to CloudWatch Logs
- Function details
- `sys.argv` for the function
- AWS Request ID
- Function execution time
- Function version
- You can add more data as described <PlatformLink to="/enriching-events/">here</PlatformLink>

- Request data is attached to all events: **HTTP method, URL, headers, form data, JSON payloads**. Sentry excludes raw bodies and multipart file uploads. Sentry also excludes personally identifiable information (such as user ids, usernames, cookies, authorization headers, IP addresses) unless you set `send_default_pii` to `True`.

- Each request has a separate scope. Changes to the scope within a view, for example setting a tag, will only apply to events sent as part of the request being handled.
- **Without touching your code.** This is configurable from within the Sentry product. See the [AWS Lambda Cloud Monitoring Product Guide](/product/integrations/cloud-monitoring/aws-lambda/).
- **By adding the Sentry Lambda Layer manually to your function.** See <PlatformLink to="/integrations/aws-lambda/manual-layer/">AWS Lambda Layer</PlatformLink>.
- **By manually adding Sentry in your function code.** See <PlatformLink to="/integrations/aws-lambda/manual-instrumentation/">AWS Lambda Manual Instrumentation</PlatformLink>.
- **By adding Sentry to your AWS Lambda container image.** See <PlatformLink to="/integrations/aws-lambda/container-image/">AWS Lambda Container Image</PlatformLink>.
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Loading
Loading