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: Set up the Elastic APM AWS 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.
The selected AWS region and the architecture must match the AWS region and architecture of your AWS Lambda function!
Add the Elastic APM AWS 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:
- Navigate to your function in the AWS Management Console
-
Scroll to the Layers section and click the Add a layer button
- Choose the Specify an ARN radio button
-
Copy and paste the following ARN of the Elastic APM AWS Lambda extension layer in the Specify an ARN text input:
EXTENSION_ARN - Click the Add button
To add the Elastic APM AWS Lambda extension Layer ARN through the AWS command line interface execute the following command:
aws lambda update-function-configuration --function-name yourLambdaFunctionName \ --layers EXTENSION_ARN
In your SAM template.yml
file add the Elastic APM AWS Lambda extension Layer ARN as follows:
... Resources: yourLambdaFunction: Type: AWS::Serverless::Function Properties: ... Layers: - EXTENSION_ARN ...
In your serverless.yml
file add the Elastic APM AWS Lambda extension Layer ARN to your function as follows:
... functions: yourLambdaFunction: handler: ... layers: - EXTENSION_ARN ...
To add the Elastic APM AWS Lambda extension Layer to your function add the ARN to the layers
property in your Terraform file:
... resource "aws_lambda_function" "your_lambda_function" { ... layers = ["EXTENSION_ARN"] } ...
To add the Elastic APM AWS Lambda extension to your container-based function extend the Dockerfile of your function image as follows:
FROM docker.elastic.co/observability/apm-lambda-extension-IMAGE_ARCH:latest AS lambda-extension # FROM ... <-- this is the base image of your Lambda function COPY --from=lambda-extension /opt/elastic-apm-extension /opt/extensions/elastic-apm-extension # ...
Step 2: Set up 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 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:
- Navigate to your function in the AWS Management Console
- Click on the Configuration tab
- Click on Environment variables
- 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 ELASTIC_APM_SEND_STRATEGY = background
To configure APM through the AWS command line interface execute the following command:
In your SAM template.yml
file configure the following environment variables:
In your serverless.yml
file configure the following environment variables:
In your Terraform file configure the following environment variables:
Environment variables configured for an AWS Lambda function are passed to the container running the lambda function. You can use one of the other options (through AWS Web Console, AWS CLI, etc.) to configure the following environment variables:
The |
You can optionally fine-tune the Python agent or the configuration of the Elastic APM AWS 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
.