

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

To start CodeGuru Profiler in your application running on AWS Lambda, you can either update your Lambda function configuration or modify your application code. The former option is available only for Java 8 on Amazon Linux 2 and Java 11 and Java 17 (Corretto) runtimes, while the latter is available for all Java runtimes.

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 Java 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**
+ [

# All Java runtimes
](lambda-custom.md)
+ [

# Easier option for Java 8 on Amazon Linux 2 and Java 11 and Java 17 (Corretto) runtimes
](lambda-simple.md)

# All Java runtimes
<a name="lambda-custom"></a>

If you're profiling applications that run on AWS Lambda, add the following environment variables to your Lambda function.
+ `AWS_CODEGURU_PROFILER_GROUP_ARN` – Identifies the profiling group ARN.
+ `AWS_CODEGURU_PROFILER_ENABLED` – Enables profiling when set to `TRUE`. To disable profiling, set this variable to `FALSE`. Default value is `TRUE`.

For information about setting environment variables in the Lambda console, see [Using AWS Lambda environment variables](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html).

Add a dependency to the CodeGuru Profiler profiling agent library. You can do this manually by adding a dependency in your Maven or Gradle configuration files. For more information about adding dependencies, see [Enabling the agent with code](https://docs.aws.amazon.com/codeguru/latest/profiler-ug/enabling-the-agent-with-code).

## Make code changes to start profiling your AWS Lambda functions
<a name="lambda-code-change"></a>

If you have been using handlers provided by AWS Lambda, then you can alter your code to use handlers provided by CodeGuru to enable profiling. For information about your Lambda function's header, see [AWS Lambda function handler in Java](https://docs.aws.amazon.com/lambda/latest/dg/java-handler.html). 

**Note**  
No need to change your Lambda configuration\$1 The handler function should still be `handleRequest`. This function is implemented by the CodeGuru class and calls the `requestHandler` after setting up the profiler.

If your Lambda function uses Lambda’s `RequestHandler`, you can replace it with CodeGuru Profiler's `RequestHandlerWithProfiling` to enable profiling by default. `RequestHandlerWithProfiling` is a generic type that takes two parameters: the input type and the output type. Both types must be objects. When you use `RequestHandlerWithProfiling`, the Java runtime deserializes the event into an object with the input type, and serializes the output into text. Use this interface when the built-in serialization works with your input and output types. The following example provides a sample code snippet to enable profiling by default.

```
package example;

import java.util.Map;

import com.amazonaws.services.lambda.runtime.Context;

import software.amazon.codeguruprofilerjavaagent.RequestHandlerWithProfiling;

public class Handler extends RequestHandlerWithProfiling<Map<String, String>, String> {

    @Override
    public String requestHandler(Map<String, String> input, Context context) {
        // Your function code here
    }
}
```

If you are using `RequestStreamHandler`, then you can replace it with CodeGuru Profiler's `RequestStreamHandlerWithProfiling`. To use your own serialization, implement the `RequestStreamHandlerWithProfiling` interface, with which Lambda passes your handler an input stream and output stream. The handler reads bytes from the input stream, writes to the output stream, and returns void. 

```
package example;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.amazonaws.services.lambda.runtime.Context;

import software.amazon.codeguruprofilerjavaagent.RequestStreamHandlerWithProfiling;

public class StreamHandler extends RequestStreamHandlerWithProfiling {

    @Override
    public void requestHandler(InputStream input, OutputStream output, Context context) throws IOException {
        // Your function code here
    }
}
```

If you don't use handlers provided by AWS Lambda, then add the following code to start profiling your AWS Lambda functions. Wrap your AWS Lambda function’s logic inside a utility from the CodeGuru Profiler profiling agent.

```
package example;

import com.amazonaws.services.lambda.runtime.Context;

import software.amazon.codeguruprofilerjavaagent.LambdaProfiler;

public class MyHandler {

    // This is the handler function we use in the lambda
    public Output handleRequest(Input input, Context context) {
        return LambdaProfiler.profile(input, context, this::myHandlerFunction);
    }
    
    private Output myHandlerFunction(Input input, Context context) {
        // your function code here
    }
}
```

Your Lambda function runs the way it typically does, while the CodeGuru Profiler profiling agent runs in parallel. After running for 5 minutes, the agent submits your first profile. Processing can take up to 15 minutes.

# Easier option for Java 8 on Amazon Linux 2 and Java 11 and Java 17 (Corretto) runtimes
<a name="lambda-simple"></a>

You can enable CodeGuru Profiler from the AWS console by setting environment variables and updating configuration for your AWS Lambda function. This method works for Java 8 on Amazon Linux 2 and Java 11 and Java 17 (Corretto) runtimes.

If you're profiling applications that run on Lambda, set the following environment variables to your Lambda function.
+ `AWS_CODEGURU_PROFILER_GROUP_NAME` – Identifies the profiling group name.
+ `AWS_CODEGURU_PROFILER_TARGET_REGION` – Identifies the target region of the profiling group.
+ `AWS_CODEGURU_PROFILER_HEAP_SUMMARY_ENABLED` – Optional. Set this variable to `true` to enable heap summary. The default is `false`.
+ `JAVA_TOOL_OPTIONS` – Set this variable to `-javaagent:/opt/codeguru-profiler-java-agent-standalone.jar`.

For information about setting environment variables in the AWS Lambda console, see [Using AWS Lambda environment variables](https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html).

Add a layer to your Lambda function using the following layer ARN. For more information on Lambda layers, see [AWS Lambda Layers](https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html#configuration-layers-using).

```
arn:aws:lambda:LAMBDA-FUNCTION-REGION-CODE:157417159150:layer:AWSCodeGuruProfilerJavaAgentLayer:11
```

For example, if your Lambda function is in Region `us-east-1`, then the ARN would be the following.

```
arn:aws:lambda:us-east-1:157417159150:layer:AWSCodeGuruProfilerJavaAgentLayer:11
```

The CodeGuru Profiler heap summary is an optional feature that shows your application's heap usage over time. For more information on the heap summary, see [Understanding the heap summary](working-with-visualizations-heap-summary.md).

Your Lambda function runs normally with the CodeGuru Profiler agent running in parallel. The agent submits your first profile after running for a total of 5 minutes. Processing can take up to 15 minutes.