Monitoring AWS Lambda Python Functionsedit

Incorporating Elastic APM into your AWS Lambda functions is easy! Follow the steps below to setup Elastic APM for your AWS Lambda Python functions.

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: Setup the APM Lambda Extensionedit

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!

Add the APM Lambda Extension as an AWS Lambda Layer to your AWS Lambda function.

To add a layer to a 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 ARN of the APM Lambda Extension layer in the Specify an ARN text input:
    EXTENSION_ARN image of choosing a layer in AWS Console
  5. Click the Add button

Step 2: Setup the APM Python Agentedit

You need to add elastic-apm as a dependency for your python function. Depending on your deployment strategy, this could be as easy as adding elastic-apm to your requirements.txt file, or installing it in the directory you plan to deploy using pip:

$ pip install -t <target_dir> elastic-apm

Once the library is included as a dependency in your function, you must import the capture_serverless decorator and apply it to your handler:

from elasticapm import capture_serverless

@capture_serverless()
def handler(event, context):
    return {"statusCode": r.status_code, "body": "Success!"}

Step 3: Configure APM on AWS Lambdaedit

The APM 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.

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

Python environment variables configuration section in AWS Console

You can optionally fine-tune the Python agent or the configuration of the APM Lambda Extension.

That’s it; Once the agent is installed and working, 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.