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:
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.
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
Create AWS Lambda Layer yourself which contains SL Test Listener java agent jar. This layer can be reused by many AWS Lambda functions.
Configure your AWS Lambda Function to use the layer (with SL Java Agent).
During the creation of the AWS Lambda function execution environment, the layer content will be extracted into the /opt directory.
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.JAVA_TOOL_OPTIONS environment variable value can also contain sl properties which we need to pass to the agent, for example:
-javaagent:/opt/sl-test-listener.jar -Dsl.buildSessionIdFile=/var/task/buildSessionId.txt -Dsl.log.level=info -Dsl.log.enabled=true -Dsl.enableUpgrade=false -Dsl.log.toConsole=true -Dsl.featuresData.codeCoverageManagerVersion=v3 -Dsl.tags=aws-lambda
You can set other environment variables - that will be picked up automatically by the agent - like SL_TOKEN, and SL_BUILDSESSIONID (whose names match the SL properties by replacing “
.
" in the SL property name with "_
").
For each version of the function code update, you’re required to update
buildSessionId
configuration on AWS Lambda Function environment: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.
Deploy function code and update buildSessionId in AWS Lambda function configuration.
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)
you can pass buildSessionId to Sealights Java Agent by setting the environment variable SL_BUILDSESSIONID
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.