This repository contains code examples and related resources showing you how to run, test and debug Synthetics Canary locally on your computer.
The local testing environment uses Serverless Application Model (SAM), to simulate a AWS Lambda function in a local Docker container to emulate the behavior of a Synthetics canary.
You should have the following prerequisites:
An Amazon S3 bucket for canary artifacts.
Note: Choose or create an Amazon S3 bucket to use to store artifacts from local canary test runs, such as HAR files and screenshots. This requires you to be provisioned with IAM. If you skip setting up Amazon S3 buckets you can still test your canary locally, but you will see an error message about the missing bucket and you won't have access to canary artifacts.
If you use an Amazon S3 bucket, we recommend that you set the bucket lifecycle to delete objects after a few days. For more information, see Managing your storage lifecycle.
us-west-2
.Clone this repository using this cmd.
git clone https://github.com/aws-samples/synthetics-canary-local-debugging-sample.git
The repository contains code samples for both NodeJS canary and Python canary.
cd synthetics-canary-local-debugging-sample/nodejs-canary/src
npm install
to install canary dependencies.cd synthetics-canary-local-debugging-sample/python-canary/src
pip3 install -r requirements.txt -t .
to install canary dependencies.The launch configuration file is located at .vscode/launch.json
. It contains configuration to allow the template file to be discovered by VS Code. It defines Lambda payload with required parameters to invoke the canary successfully. Here’s the launch configuration for NodeJS canary.
{
...
...
"lambda": {
"payload": {
"json": {
// Canary name. Provide any name you like.
"canaryName": "LocalSyntheticsCanary",
// Canary artifact location
"artifactS3Location": {
"s3Bucket": "cw-syn-results-123456789012-us-west-2",
"s3Key": "local-run-artifacts",
},
// Your canary handler name
"customerCanaryHandlerName": "heartbeat-canary.handler"
}
},
// Environment variables to pass to the canary code
"environmentVariables": {}
}
}
]
}
Other optional fields that you can provide in payload JSON are:
s3EncryptionMode
: valid values: SSE_S3
| SSE_KMS
.s3KmsKeyArn
: activeTracing
: valid values: true | false.canaryRunId
: Add breakpoints in the canary code where you wish to pause execution by clicking on the editor margin and go to Run and Debug mode in the editor. Execute the canary by clicking on the play button. When the canary executes, the logs will be tailed in the debug console, providing you with real-time insights into the canary's behavior. If you added breakpoints the canary execution will pause at each breakpoint, allowing you step through code and inspect variable values, instance methods, object attributes, function call stack etc.
There is no cost incurred for running and debugging canary locally except for the artifacts stored in S3 bucket and the CloudWatch metrics generated by each local run.
If you use JetBrains IDE then you will require AWS Toolkit for IntelliJ IDEA extension is installed and bundled Node.js plugin and JavaScript Debugger are enabled to run and debug canary (required for NodeJS canary). Once you have the extension installed follow these steps.
template.yml
file from the project (either from nodejs directory or python directory).{
"canaryName": "LocalSyntheticsCanary",
"artifactS3Location": {
"s3Bucket": "cw-syn-results-123456789012-us-west-2",
"s3Key": "local-run-artifacts"
},
"customerCanaryHandlerName": "heartbeat-canary.handler"
}
s3Bucket
in event.json
.nodejs-canary
directory and run following commands.
sam build
sam local invoke -e ../event.json
python-canary
directory and run same commands as above.Debugging visual monitoring canaries poses a challenge due to the reliance on base screenshots captured during the initial run for subsequent comparisons. In a local development environment, runs are neither stored nor tracked, treating each iteration as an independent, standalone run. Consequently, the absence of a run history makes it impractical to perform debugging for canaries relying on visual monitoring.
You can quickly integrate local canary debugging into your existing canary package by copying following three files:
template.yml
file into your canary package root. Be sure to modify the path for CodeUri
to point to the directory where your canary code exists.cw-synthetics.js
file to your canary source directory. If you're working with a Python canary, copy the cw-synthetics.py
to your canary source directory..vscode/launch.json
in the package root. Make sure to put it inside .vscode
directory; create it if it does not exist already.Error: Running AWS SAM projects locally requires Docker. Have you got it installed and running?
Make sure to start docker on your computer.
SAM local invoke failed: An error occurred (ExpiredTokenException) when calling the GetLayerVersion operation: The security token included in the request is expired
Make sure AWS default profile is set up.
For more information about common errors with the SAM, see AWS SAM CLI troubleshooting.
The Lambda layer ARN for Synthetics runtimes are listed in AWS documentation
This project is licensed under the MIT-0 License. See the LICENSE file.