AWS Lambda Instrumentation (serverless)
Capture runtime coverage from Node.js AWS Lambda functions with the SeaLights Node.js Lambda layer.
Applies to: Node.js AWS Lambda functions that are exercised by tests.
Lambda functions are covered by the SeaLights Node.js Lambda layer. The layer wraps your handler: on each invocation it turns on V8 coverage, runs your handler, and uploads the coverage for the open test stage before the invocation returns. You do not change your handler code.
Scan the function code
Scan the code you deploy, like any other back-end build. --awsConfigure additionally writes an sl-config.json file into each subdirectory of the scanned directory, which the layer reads.
printf '%s' "$SEALIGHTS_AGENT_TOKEN" > sltoken.txt
npx slnodejs config --tokenFile sltoken.txt --appName my-lambdas --branchName main --buildName "$BUILD_NUMBER"
npx slnodejs scan --tokenFile sltoken.txt --buildSessionIdFile buildSessionId \
--scanDir src --scmType git \
--awsConfigure --awsConfigKeysToIgnore token--awsConfigure is optional: you can set every value as a function environment variable instead (step 3).
Configure the function
AWS_LAMBDA_EXEC_WRAPPER
Yes
/opt/sealights-extension
SL_TOKEN
Yes (variable or sl-config.json)
The agent token. Inject it from your secret store.
SL_BUILD_SESSION_ID
Yes (variable or sl-config.json)
The build session ID of the scanned build.
SL_LAB_ID
No
Lab ID of the test environment. Without it, the build session ID is used.
SL_PROJECT_ROOT
No
The project root of the scan, when you gave the scan one.
SL_COLLECTOR_URL
No
A SeaLights Collector address. Required only with SL_RAW_COVERAGE=true.
SL_PROXY
No
Proxy for the layer's calls to SeaLights.
A function environment variable takes precedence over the same key in sl-config.json.
Serverless Framework example:
provider:
name: aws
runtime: nodejs20.x
environment:
AWS_LAMBDA_EXEC_WRAPPER: /opt/sealights-extension
SL_TOKEN: ${env:SEALIGHTS_AGENT_TOKEN}
SL_BUILD_SESSION_ID: ${env:SL_BUILD_SESSION_ID}
SL_LAB_ID: lambda-qa
layers:
- <SEALIGHTS_LAYER_ARN>
functions:
myFunction:
handler: src/index.handlerAWS CLI example (this command replaces the function's whole layer list and environment, so include what the function already has):
aws lambda update-function-configuration \
--function-name my-function \
--layers "<SEALIGHTS_LAYER_ARN>" \
--environment "Variables={AWS_LAMBDA_EXEC_WRAPPER=/opt/sealights-extension,SL_TOKEN=$SEALIGHTS_AGENT_TOKEN,SL_BUILD_SESSION_ID=$(cat buildSessionId),SL_LAB_ID=lambda-qa}"Open a test stage and invoke
The layer looks up the open test stage for its lab ID on every invocation. Without an open test stage, it collects no coverage.
npx slnodejs openTestStage --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --labId lambda-qa --testStage "Integration Tests"
# invoke the functions through your tests
npx slnodejs closeTestStage --tokenFile sltoken.txt --buildSessionIdFile buildSessionId --labId lambda-qa --testStage "Integration Tests"Coverage modes
V8 (default)
Nothing
Nothing beyond the setup above.
nyc
SL_USE_NYC=true
Code instrumented with nyc before deployment, and SL_PROJECT_ROOT.
Raw V8
SL_RAW_COVERAGE=true
SL_COLLECTOR_URL (a SeaLights Collector), and a scan with --rawCoverage.
Other Lambda wrappers (Dynatrace, OpenTelemetry)
Lambda accepts only one AWS_LAMBDA_EXEC_WRAPPER. The SeaLights wrapper runs other wrappers itself, in this order, before your function starts:
/opt/dynatrace, if present, unlessDISABLE_DYNATRACE=true./opt/otel-handler, if present, unlessDISABLE_OTEL_HANDLER=true.The scripts listed in
ENABLE_HANDLER(comma-separated absolute paths, no spaces).
If one of those scripts exits with a non-zero status, the function fails to initialize.
Troubleshooting
No Sealights Layer: lines in the logs
The layer ARN is attached, and AWS_LAMBDA_EXEC_WRAPPER is /opt/sealights-extension.
SL_TOKEN environment variable is required. or SL_BUILD_SESSION_ID environment variable is required.
Set the variable on the function, or keep the key in sl-config.json. An empty variable also wins over the file.
Failed to resolve execution data from API, please check that an execution is open.
No test stage is open for the function's lab ID (or for the build session ID when SL_LAB_ID is not set).
Collector setup is required when rawCoverage is enabled.
Set SL_COLLECTOR_URL, or turn off SL_RAW_COVERAGE.
Coverage appears but does not match the scan
SL_PROJECT_ROOT differs from the scan's project root.
The handler fails with an ES module error
Use the -esm layer for ES module handlers, and the -cjs layer for CommonJS handlers.
The configuration error does not stop the function: the layer logs it, skips coverage for that invocation, and your handler still runs.
Last updated
Was this helpful?

