# AWS Lambda Support

To collect AWS Lambda Function code coverage, we need to configure the AWS Lambda to attach Sealights java agent to the JVM startup command in the AWS Lambda Function execution environment:

1. We need the AWS Lambda Layer with Sealights test listener jar. We can use the one published by Sealights or create the new one. The layer and the AWS Lambda Function need to be in the same AWS region.
   1. Use one of Sealights java AWS Lambda layers

      `arn:aws:lambda:eu-west-1:442677231940:layer:sl-java-agent:1`\
      `arn:aws:lambda:eu-west-2:442677231940:layer:sl-java-agent:1`\
      `arn:aws:lambda:us-east-1:442677231940:layer:sl-java-agent:1`\
      `arn:aws:lambda:us-west-2:442677231940:layer:sl-java-agent:1`
   2. Create AWS Lambda Layer yourself which contains SL Test Listener java agent jar. This layer can be reused by many AWS Lambda functions.<br>

      <figure><img src="https://4057366433-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAjqTCMRYvHhDgsdPLUnc%2Fuploads%2FDAsEmzK8RoFZKAjQhpY4%2FScreenshot%20from%202023-12-27%2009-04-38.png?alt=media&#x26;token=bcc080dd-23e0-435b-a7c0-27fea242de19" alt=""><figcaption></figcaption></figure>
2. Configure your AWS Lambda Function to use the layer (with SL Java Agent).

   <figure><img src="https://4057366433-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAjqTCMRYvHhDgsdPLUnc%2Fuploads%2FajxwE4pBBQUpkGdnkA81%2Fimage-20231127-105122.png?alt=media&#x26;token=ac592da1-73b2-4d04-8c57-0ee755895ac2" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
During the creation of the AWS Lambda function execution environment, the layer content will be extracted into the **/opt** directory.
{% endhint %}

3. Create or modify **JAVA\_TOOL\_OPTIONS** environment variable on AWS Lambda Function configuration page: add `-javaagent` parameter pointing to SL Test Listener which is placed in /opt directory.
   1. JAVA\_TOOL\_OPTIONS environment variable value can also contain sl properties that we need to pass to the agent, for example:

      <pre data-overflow="wrap" data-line-numbers><code> -javaagent:/opt/sl-test-listener.jar -Dsl.buildSessionIdFile=/var/task/buildSessionId.txt -Dsl.enableUpgrade=false -Dsl.tags=aws-lambda -Dsl.collectorUrl=http://mycollector.dns
      </code></pre>

      <figure><img src="https://4057366433-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FAjqTCMRYvHhDgsdPLUnc%2Fuploads%2FA3I7FRfOTB4y2tz9XpxQ%2FScreenshot%20from%202023-12-22%2011-43-45.png?alt=media&#x26;token=c6a61d56-8e76-4adc-aa7a-a59d7634de07" alt=""><figcaption></figcaption></figure>
   2. You can set other environment variables - that will be picked up automatically by the agent - like **SL\_TOKEN**, **SL\_BUILDSESSIONID, SL\_LAB\_ID** & **SL\_COLLECTOR\_URL** (whose names match the SL properties by replacing “`.`" in the SL property name with "`_`").
4. For each version of the function code update, you’re required to update `buildSessionId` configuration on AWS Lambda Function environment:
   1. Scan the function code outside the AWS Lambda environment with the Sealights Java build Scanner (or maven/gradle plugin). The buildSessionId will be generated and its bytecode metadata (code changes) will be sent to Sealights Cloud Services.
   2. Deploy function code and update buildSessionId in AWS Lambda function configuration.
      1. If you pack your function code together with buildSessionId.txt and sltoken.txt and deploy it then buildSessionId.txt and sltoken.txt will be available in /var/task in AWS Lambda function execution environment (as the AWS Lambda function jar’s content is extracted into **/var/task** directory)
      2. you can pass buildSessionId to Sealights Java Agent by setting the environment variable **SL\_BUILDSESSIONID**
   3. You can now execute your tests: The Sealights Test listener will be attached to JVM on the AWS Lambda Function execution environment to collect coverage information and send it to the Sealights Cloud Platform.
