

# Profiling your applications that run on AWS Lambda
<a name="python-lambda"></a>

CodeGuru Profiler integration for AWS Lambda is currently available for applications that run on Python 3.7 up to Python 3.9. To start CodeGuru Profiler in your application running on Lambda, you can either apply the CodeGuru Profiler function decorator to your handler function, update your Lambda function configuration by adding layers, or enable profiling in the Lambda console.

If you enabled profiling in the Lambda console, you don't have to complete the procedure outlined in the following sections. To learn more about enabling profiling from the Lambda console, see [Set up in the Lambda console](setting-up-short.md).

**Note**  
You can profile your Lambda functions running in Python if they are called often enough for CodeGuru Profiler to gather enough samples. CodeGuru Profiler collects data once per second, aggregated into 5-minute sampling buckets. For Lambda functions running for fewer than 5 minutes, your application must run multiple times so CodeGuru Profiler can collect enough data. If it runs too infrequently, CodeGuru Profiler can't generate enough data to provide recommendations and flame graphs. For long-running Lambda applications, processing can take up to 15 minutes to display graphs and information. If you are running your application in shorter durations, processing takes longer to display information.

**Topics**
+ [Apply the CodeGuru Profiler function decorator to your handler function](python-lambda-command-line.md)
+ [Use AWS Lambda layers](python-lambda-layers.md)



# Apply the CodeGuru Profiler function decorator to your handler function
<a name="python-lambda-command-line"></a>

Pull your `codeguru_profiler_agent` dependency to your local environment through `pip` and include it in the .zip file for Lambda.

The only required configuration option to start the agent is the profiling group name. You can find this in the **Settings** section of your profiling group on the CodeGuru Profiler console. You can also provide the Region if you want to use a profiling group that was created in a different region than the one where Lambda is running. You can also provide the profiling group ARN directly, which contains both the name and Region. Either the profiling group name or ARN must be provided. 


| Option | Environment variable key | Environment variable value | Decorator argument example | 
| --- | --- | --- | --- | 
|  Profiling group name  |  `AWS_CODEGURU_PROFILER_GROUP_NAME`  |  `MyGroupName`  |  `@with_lambda_profiler(profiling_group_name="MyGroupName")`  | 
|  Profiling group ARN  |  `AWS_CODEGURU_PROFILER_GROUP_ARN`  |  `arn:aws:codeguru-profiler:us-east-1:123456789123:profilingGroup/MyGroupName`  |  An ARN cannot be passed as a decorator argument  | 
|  Region  |  `AWS_CODEGURU_PROFILER_TARGET_REGION`  |  `us-east-1`  |  `@with_lambda_profiler(region_name="us-east-1")`  | 

Decorate your handler function with `@with_lambda_profiler()`. The following example shows what your handler function code looks like with CodeGuru Profiler turned on.

```
from codeguru_profiler_agent import with_lambda_profiler

@with_lambda_profiler(profiling_group_name="MyGroupName")
def handler_name(event, context):
    return "Hello World"
```

Only decorate your handler function. You do not have to decorate other internal functions. You can pass the profiling group name directly in the decorator, or with environment variables.

# Use AWS Lambda layers
<a name="python-lambda-layers"></a>

There are two ways you can use layers to enable CodeGuru in Lambda functions using Python. The preferred method uses a wrapper script and only works for applications that run on Python 3.8 or 3.9. The second method works for Python 3.7, and can also be used for Python 3.8 and 3.9 if you already have a lambda wrapper or if the preferred method otherwise does not work.

**For applications that run on Python 3.8 or 3.9 (preferred)**

1. Add the CodeGuru Profiler layer to Lambda. Choose **Specify an ARN** and add `arn:aws:lambda:region:157417159150:layer:AWSCodeGuruProfilerPythonAgentLambdaLayer:11`. For more information on adding a Lambda layer, see [AWS Lambda layers](https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html).

1. Add the following environment variable: `AWS_LAMBDA_EXEC_WRAPPER=/opt/codeguru_profiler_lambda_exec`

1. Add an environment variable with your profiling group name or ARN. For information on using your ARN, see the table listed in [Apply the CodeGuru Profiler function decorator to your handler function](https://docs.aws.amazon.com/codeguru/latest/profiler-ug/python-lambda-command-line.html).

**Note**  
You can only have one Lambda wrapper script. If you are currently using one, try the solution for applications that run on Python 3.7, 3.8 or 3.9.

**For applications that run on Python 3.7, 3.8 or 3.9**

1. Add the CodeGuru Profiler layer to Lambda. Choose **Specify an ARN** and `arn:aws:lambda:region:157417159150:layer:AWSCodeGuruProfilerPythonAgentLambdaLayer:11`. For more information on adding a Lambda layer, see [AWS Lambda layers](https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html).

1. Set the environment variable, `HANDLER_ENV_NAME_FOR_CODEGURU` to your handler function.

1. Change the Lambda handler function to `codeguru_profiler_agent.aws_lambda.lambda_handler.call_handler`.

1. Add an environment variable with your profiling group name or ARN. For information on using your ARN, see the table listed in [Apply the CodeGuru Profiler function decorator to your handler function](https://docs.aws.amazon.com/codeguru/latest/profiler-ug/python-lambda-command-line).

**Note**  
You can only have up to five layers for a Lambda function. If you are already using five layers, see [Apply the CodeGuru Profiler function decorator to your handler function](https://docs.aws.amazon.com/codeguru/latest/profiler-ug/python-lambda-command-line).