Monitoring AWS Lambda Python Functionsedit

The Python APM Agent can be used with AWS Lambda to monitor the execution of your AWS Lambda functions.

Note: The Centralized Agent Configuration on the Elasticsearch APM currently does NOT support AWS Lambda.

Prerequisitesedit

You need an APM Server to send APM data to. Follow the APM Quick start if you have not set one up yet. For the best-possible performance, we recommend setting up APM on Elastic Cloud in the same AWS region as your AWS Lambda functions.

Step 1: Select the AWS Region and Architectureedit

Pick the right ARN from this release table for the APM Lambda Extension Layer.

In addition, pick the right ARN from this release table for the APM Agent Layer.

Select the AWS region and architecture of your Lambda function. This documentation will update based on your selections.
region:
architecture:

The selected AWS region and the architecture must match the AWS region and architecture of your AWS Lambda function!

Step 2: Add the APM Layers to your Lambda functionedit

Both the Elastic APM AWS Lambda extension and the Python APM Agent are added to your Lambda function as AWS Lambda Layers. Therefore, you need to add the corresponding Layer ARNs (identifiers) to your Lambda function.

To add the layers to your Lambda function through the AWS Management Console:

  1. Navigate to your function in the AWS Management Console
  2. Scroll to the Layers section and click the Add a layer button image of layer configuration section in AWS Console
  3. Choose the Specify an ARN radio button
  4. Copy and paste the following ARNs of the Elastic APM AWS Lambda extension layer and the APM agent layer in the Specify an ARN text input:
    APM Extension layer:
    EXTENSION_ARN
    APM agent layer:
    AGENT_ARN image of choosing a layer in AWS Console
  5. Click the Add button

Step 3: Configure APM on AWS Lambdaedit

The Elastic APM AWS Lambda extension and the APM Python agent are configured through environment variables on the AWS Lambda function.

For the minimal configuration, you will need the APM Server URL to set the destination for APM data and an APM Secret Token. If you prefer to use an APM API key instead of the APM secret token, use the ELASTIC_APM_API_KEY environment variable instead of ELASTIC_APM_SECRET_TOKEN in the following configuration.

For production environments, we recommend using the AWS Secrets Manager to store your APM authentication key instead of providing the secret value as plaintext in the environment variables.

To configure APM through the AWS Management Console:

  1. Navigate to your function in the AWS Management Console
  2. Click on the Configuration tab
  3. Click on Environment variables
  4. Add the following required variables:
AWS_LAMBDA_EXEC_WRAPPER       = /opt/python/bin/elasticapm-lambda  # use this exact fixed value
ELASTIC_APM_LAMBDA_APM_SERVER = <YOUR-APM-SERVER-URL>              # this is your APM Server URL
ELASTIC_APM_SECRET_TOKEN      = <YOUR-APM-SECRET-TOKEN>            # this is your APM secret token
ELASTIC_APM_SEND_STRATEGY     = background                         

Python environment variables configuration section in AWS Console

The ELASTIC_APM_SEND_STRATEGY defines when APM data is sent to your Elastic APM backend. To reduce the execution time of your lambda functions, we recommend to use the background strategy in production environments with steady load scenarios.

You can optionally fine-tune the Python agent or the configuration of the Elastic APM AWS Lambda extension.

That’s it. After following the steps above, you’re ready to go! Your Lambda function invocations should be traced from now on. Spans will be captured for supported technologies. You can also use capture_span to capture custom spans, and you can retrieve the Client object for capturing exceptions/messages using get_client.