Skip to content

Commit

Permalink
Update Python AWS Lambda integration docs. (#8246)
Browse files Browse the repository at this point in the history
Bringing all the information in the documentation for the AWS Lambda integration with Python up to date. Updated screenshots and made sure all the information is easy to follow. (Ignoring AWS Lambda integration using Docker images, because this is currently abandoned it seems)
---------

Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
Co-authored-by: Liza Mock <[email protected]>
  • Loading branch information
3 people authored Oct 31, 2023
1 parent 5e6f9d1 commit e44604a
Show file tree
Hide file tree
Showing 23 changed files with 237 additions and 153 deletions.
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.
80 changes: 42 additions & 38 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**.

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

<Note>

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

### Troubleshooting

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.
If you need help solving issues with your Sentry AWS Lamba 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.

#### 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

### Node
If you're using [Serverless framework](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/).

```
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 you'll have to do two things manually: maintain the latest layer version and add the reference to the SDK layer for every function you want "Sentrified".

**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
91 changes: 5 additions & 86 deletions src/platforms/python/integrations/aws-lambda/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,90 +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___",
enable_tracing=True,
integrations=[
AwsLambdaIntegration(),
]
)

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___",
enable_tracing=True,
integrations=[
AwsLambdaIntegration(
timeout_warning=True
),
],
)
```

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.
26 changes: 0 additions & 26 deletions src/platforms/python/integrations/aws-lambda/layer/index.mdx

This file was deleted.

Binary file not shown.
Binary file not shown.
Loading

1 comment on commit e44604a

@vercel
Copy link

@vercel vercel bot commented on e44604a Oct 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

sentry-docs – ./

sentry-docs.sentry.dev
docs.sentry.io
sentry-docs-git-master.sentry.dev

Please sign in to comment.