--- title: "Java APM with AWS Lambda" slug: "java-apm-with-aws-lambda" description: "Setup APM for AWS Lambda Java Functions using Stackify AWS Monitor. Add Stackify Layer, configure environment variables, and test the function " updated: 2020-04-08T20:11:49Z published: 2020-04-08T20:11:49Z canonical: "docs.stackify.com/java-apm-with-aws-lambda" --- > ## Documentation Index > Fetch the complete documentation index at: https://docs.stackify.com/llms.txt > Use this file to discover all available pages before exploring further. # Java APM with AWS Lambda This document details how to setup **APM** with your **AWS Lambda Java Functions**. ![image.png](https://cdn.document360.io/3fcaf473-a1ef-4778-aa2c-fd28411c2b01/Images/Documentation/image%285%29.png) ## Stackify AWS Monitor Setup **AWS Lambda** support requires a **Stackify AWS Monitor** server to forward application data to Stackify. A monitoring server is required for each AWS region you are using functions. Log into your AWS account and click the **Launch Stack** button below to deploy the **CloudFormation Stack Cloud Formation Template**. Set parameter **StackifyLambda** is set to `true`. [![image](https://s3.amazonaws.com/cloudformation-examples/cloudformation-launch-stack.png)](https://console.aws.amazon.com/cloudformation/home#/stacks/new?stackName=stackify-aws-monitor&templateURL=https://s3.amazonaws.com/download.stackify.com/aws-template/aws-monitor-cloudformation-template.yaml) ## Lambda Function Setup Stackify APM works by adding a **Lambda Layer** to your function. ### 1. Add Stackify Layer to Function On your function **Configuration** page, click the **Add a layer** button: ![image.png](https://cdn.document360.io/3fcaf473-a1ef-4778-aa2c-fd28411c2b01/Images/Documentation/image%288%29.png) Find the correct **StackifyJavaAPMLayer ARN** for the region you are working in below: | Region | Layer ARN | | --- | --- | | us-east-1 | arn:aws:lambda:us-east-1:686826687729:layer:StackifyJavaAPMLayer:2 | | us-east-2 | arn:aws:lambda:us-east-2:686826687729:layer:StackifyJavaAPMLayer:2 | | us-west-1 | arn:aws:lambda:us-west-1:686826687729:layer:StackifyJavaAPMLayer:2 | | us-west-2 | arn:aws:lambda:us-west-2:686826687729:layer:StackifyJavaAPMLayer:2 | | ap-south-1 | arn:aws:lambda:ap-south-1:686826687729:layer:StackifyJavaAPMLayer:2 | | ap-northeast-1 | arn:aws:lambda:ap-northeast-1:686826687729:layer:StackifyJavaAPMLayer:2 | | ap-northeast-2 | arn:aws:lambda:ap-northeast-2:686826687729:layer:StackifyJavaAPMLayer:2 | | ap-southeast-1 | arn:aws:lambda:ap-southeast-1:686826687729:layer:StackifyJavaAPMLayer:2 | | ap-southeast-2 | arn:aws:lambda:ap-southeast-2:686826687729:layer:StackifyJavaAPMLayer:2 | | ca-central-1 | arn:aws:lambda:ca-central-1:686826687729:layer:StackifyJavaAPMLayer:2 | | eu-central-1 | arn:aws:lambda:eu-central-1:686826687729:layer:StackifyJavaAPMLayer:2 | | eu-west-1 | arn:aws:lambda:eu-west-1:686826687729:layer:StackifyJavaAPMLayer:2 | | eu-west-2 | arn:aws:lambda:eu-west-2:686826687729:layer:StackifyJavaAPMLayer:2 | | eu-west-3 | arn:aws:lambda:eu-west-3:686826687729:layer:StackifyJavaAPMLayer:2 | | eu-north-1 | arn:aws:lambda:eu-north-1:686826687729:layer:StackifyJavaAPMLayer:2 | | sa-east-1 | arn:aws:lambda:sa-east-1:686826687729:layer:StackifyJavaAPMLayer:2 | ### 2. Configure Stackify Layer The **StackifyJavaAPMLayer** works by changing your function handler to our profiler's handler. The profiler is a wrapper and calls your lambda function set in the environment variable **STACKIFY_LAMBDA_HANDLER**. - Add **Environment Variables** - **STACKIFY_LAMBDA_HANDLER** - Your function handler ie) **com.acme.LambdaHandler::handleRequest** - **STACKIFY_APPLICATION_NAME** - Application Name to associate with function. - **STACKIFY_ENVIRONMENT_NAME** - Environment Name to associate with function. - Update **Handler** based on which interface your class implements: - `com.amazonaws.services.lambda.runtime.RequestHandler` - **com.stackify.apm.lambda.StackifyRequestHandler::handleRequest** - `com.amazonaws.services.lambda.runtime.RequestStreamHandler` - **com.stackify.apm.lambda.StackifyRequestStreamHandler::handleRequest** - **Save** your function. ### 3. Test Run a test execution of your function and verify that the **Log output** contains a `STACKIFY-TRACE` line. - After a few minutes of function traffic, goto **Retrace** > **Monitoring** > **Other Apps**. - Click **Add App** - Click **Existing Server** - Find your application by name and click **Add** ## AWS Costs - Each **Stackify AWS Monitor** runs inside an **t2.micro** ec2 instance by default. - The **StackifyJavaAPMLayer** writes logs to **CloudWatch** for each function execution. We suggest reducing retention on **CloudWatch LogGroups**.