Test a canary locally
This section explains how to modify, test, and debug CloudWatch Synthetics canaries directly within the Microsoft Visual Studio code editor or the JetBrains IDE code editor. The local debugging environment uses a Serverless Application Model (SAM) container to simulate a Lambda function to emulate the behavior of a Synthetics canary.
Note
It is impractical to perform locally debug canaries that rely on visual monitoring. Visual monitoring rely on capturing base screenshots during an initial run, and then comparing these screenshots to the screenshots from subsequent runs. In a local development environment, runs are not stored or tracked, and each iteration is an independent, standalone run. The absence of a canary run history makes it impractical to debug canaries that rely on visual monitoring.
Prerequisites
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, to save costs. For more information, see Managing your storage lifecycle.
Set up a default AWS profile for your AWS account. For more information, see Configuration and credential file settings.
Set the debug environment's default AWS Region to your preferred Region, such as
us-west-2
.Install the AWS SAM CLI. For more information, see Installing the AWS SAM CLI.
Install Visual Studio Code Editor or JetBrains IDE. For more information, see Visual Studio Code
or JetBrains IDE Install Docker to work with the AWS SAM CLI. Make sure to start the docker daemon. For more information, see Installing Docker to use with the AWS SAM CLI.
Alternatively, You can install other container management software such as Rancher, as long as it uses the Docker runtime.
Install an AWS toolkit extension for your preferred editor. For more information, see Installing the AWS Toolkit for Visual Studio Code or Installing the AWS Toolkit for JetBrains.
Topics
Set up the testing and debugging environment
First, clone the Github repository that AWS provides by entering the following command. The repository contains code samples for both Node.js canaries and Python canaries.
git clone https://github.com/aws-samples/synthetics-canary-local-debugging-sample.git
Then do one of the following, depending on the language of your canaries.
For Node.js canaries
Go to the Node.js canary source directory by entering the following command.
cd synthetics-canary-local-debugging-sample/nodejs-canary/src
Enter the following command to install canary dependencies.
npm install
For Python canaries
Go to the Python canary source directory by entering the following command.
cd synthetics-canary-local-debugging-sample/python-canary/src
Enter the following command to install canary dependencies.
pip3 install -r requirements.txt -t .
Use Visual Studio Code IDE
The Visual Studio launch configuration file is located at .vscode/launch.json
. It contains configurations to allow the template file to be discovered by Visual
Studio code. It defines a Lambda payload with required parameters to invoke the canary successfully. Here’s the launch configuration for a Node.js 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": {} } } ] }
You can also optionally provide the following fields in the payload JSON:
s3EncryptionMode
Valid values:SSE_S3
|SSE_KMS
s3KmsKeyArn
Valid value:KMS Key ARN
activeTracing
Valid values:true
|false
canaryRunId
Valid value:UUID
This parameter is required if active tracing is enabled.
To debug the canary in Visual Studio, add breakpoints in the canary code where you want to pause execution. To add a breakpoint, choose the editor margin and go to Run and Debug mode in the editor. Run the canary by clicking on the play button. When the canary runs, 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 to step through code and inspect variable values, instance methods, object attributes, and the function call stack.
There is no cost incurred for running and debugging canaries locally, except for the artifacts stored in the Amazon S3 bucket and the CloudWatch metrics generated by each local run.
Use JetBrains IDE
After you have the AWS Toolkit for JetBrains extension installed, be sure that the Node.js plugin and JavaScript debugger are enabled to run, if you are debugging a Node.js canary. Then follow these steps.
Debug a canary using JetBrains IDE
In the left navigation pane of JetBrains IDE, choose Lambda, then choose the local configuration template.
Enter a name for the run configuration, such as
LocalSyntheticsCanary
Choose From template, choose the file browser in the template field, then select the template.yml file from the project, either from the nodejs directory or the python directory.
In the Input section, enter the payload for the canary as shown in the following screen.
{ "canaryName": "LocalSyntheticsCanary", "artifactS3Location": { "s3Bucket": "cw-syn-results-123456789012-us-west-2", "s3Key": "local-run-artifacts" }, "customerCanaryHandlerName": "heartbeat-canary.handler" }
You can also set other environment variables in the payload JSON, as listed in Use Visual Studio Code IDE.
Run a canary locally with the SAM CLI
Use the one of the following procedures to run your canary locally using the Serverless Application Model (SAM) CLI.
Be sure to specify your own Amazon S3 bucket name for s3Bucket
in event.json
To use the SAM CLI to run a Node.js canary
Go to the source directory by entering the following command.
cd synthetics-canary-local-debugging-sample/nodejs-canary
Enter the following commands.
sam build sam local invoke -e ../event.json
To use the SAM CLI to run a Python canary
Go to the source directory by entering the following command.
cd synthetics-canary-local-debugging-sample/python-canary
Enter the following commands.
sam build sam local invoke -e ../event.json
Integrate your local testing environment into an existing canary package
You can integrate local canary debugging into your existing canary package by copying three files:
Copy the
template.yml
file into your canary package root. Be sure to modify the path forCodeUri
to point to the directory where your canary code exists.If you're working with a Node.js canary, copy the
cw-synthetics.js
file to your canary source directory. If you're working with a Python canary, copy thecw-synthetics.py
to your canary source directory.Copy the launch configuration file .
vscode/launch.json
into the package root. Make sure to put it inside the.vscode
directory; create it if it doesn't exist already.
Change the CloudWatch Synthetics runtime
As part of your debugging, you might want to try running a canary with a different CloudWatch Synthetics runtime, instead of the latest runtime. To do so, find
the runtime that you want to use from one of the following tables. Be sure to select the runtime for the correct Region. Then paste the ARN for that runtime
into the appropriate place in your template.yml
file, and then run the canary.
Node.js runtimes
ARNs for syn-nodejs-puppeteer-7.0
The following table lists the ARNs to use for version syn-nodejs-puppeteer-7.0
of the
CloudWatch Synthetics runtime
in each AWS Region where it is available.
Region | ARN |
---|---|
US East (N. Virginia) |
|
US East (Ohio) |
|
US West (N. California) |
|
US West (Oregon) |
|
Africa (Cape Town) |
|
Asia Pacific (Hong Kong) |
|
Asia Pacific (Hyderabad) |
|
Asia Pacific (Jakarta) |
|
Asia Pacific (Melbourne) |
|
Asia Pacific (Mumbai) |
|
Asia Pacific (Osaka) |
|
Asia Pacific (Seoul) |
|
Asia Pacific (Singapore) |
|
Asia Pacific (Sydney) |
|
Asia Pacific (Tokyo) |
|
Canada (Central) |
|
Canada West (Calgary) |
|
China (Beijing) |
|
China (Ningxia); |
|
Europe (Frankfurt) |
|
Europe (Ireland) |
|
Europe (London) |
|
Europe (Milan) |
|
Europe (Paris) |
|
Europe (Spain) |
|
Europe (Stockholm) |
|
Europe (Zurich) |
|
Israel (Tel Aviv) |
|
Middle East (Bahrain) |
|
Middle East (UAE) |
|
South America (São Paulo) |
|
AWS GovCloud (US-East) |
|
AWS GovCloud (US-West) |
|
ARNs for syn-nodejs-puppeteer-6.2
The following table lists the ARNs to use for version syn-nodejs-puppeteer-6.2
of the
CloudWatch Synthetics runtime
in each AWS Region where it is available.
Region | ARN |
---|---|
US East (N. Virginia) |
|
US East (Ohio) |
|
US West (N. California) |
|
US West (Oregon) |
|
Africa (Cape Town) |
|
Asia Pacific (Hong Kong) |
|
Asia Pacific (Hyderabad) |
|
Asia Pacific (Jakarta) |
|
Asia Pacific (Melbourne) |
|
Asia Pacific (Mumbai) |
|
Asia Pacific (Osaka) |
|
Asia Pacific (Seoul) |
|
Asia Pacific (Singapore) |
|
Asia Pacific (Sydney) |
|
Asia Pacific (Tokyo) |
|
Canada (Central) |
|
Canada West (Calgary) |
|
China (Beijing) |
|
China (Ningxia); |
|
Europe (Frankfurt) |
|
Europe (Ireland) |
|
Europe (London) |
|
Europe (Milan) |
|
Europe (Paris) |
|
Europe (Spain) |
|
Europe (Stockholm) |
|
Europe (Zurich) |
|
Israel (Tel Aviv) |
|
Middle East (Bahrain) |
|
Middle East (UAE) |
|
South America (São Paulo) |
|
AWS GovCloud (US-East) |
|
AWS GovCloud (US-West) |
|
ARNs for syn-nodejs-puppeteer-5.2
The following table lists the ARNs to use for version syn-nodejs-puppeteer-5.2
of the
CloudWatch Synthetics runtime
in each AWS Region where it is available.
Region | ARN |
---|---|
US East (N. Virginia) |
|
US East (Ohio) |
|
US West (N. California) |
|
US West (Oregon) |
|
Africa (Cape Town) |
|
Asia Pacific (Hong Kong) |
|
Asia Pacific (Hyderabad) |
|
Asia Pacific (Jakarta) |
|
Asia Pacific (Melbourne) |
|
Asia Pacific (Mumbai) |
|
Asia Pacific (Osaka) |
|
Asia Pacific (Seoul) |
|
Asia Pacific (Singapore) |
|
Asia Pacific (Sydney) |
|
Asia Pacific (Tokyo) |
|
Canada (Central) |
|
Canada West (Calgary) |
|
China (Beijing) |
|
China (Ningxia); |
|
Europe (Frankfurt) |
|
Europe (Ireland) |
|
Europe (London) |
|
Europe (Milan) |
|
Europe (Paris) |
|
Europe (Spain) |
|
Europe (Stockholm) |
|
Europe (Zurich) |
|
Israel (Tel Aviv) |
|
Middle East (Bahrain) |
|
Middle East (UAE) |
|
South America (São Paulo) |
|
AWS GovCloud (US-East) |
|
AWS GovCloud (US-West) |
|
Python runtimes
ARNs for syn-python-selenium-3.0
The following table lists the ARNs to use for version syn-python-selenium-3.0
of the
CloudWatch Synthetics runtime
in each AWS Region where it is available.
Region | ARN |
---|---|
US East (N. Virginia) |
|
US East (Ohio) |
|
US West (N. California) |
|
US West (Oregon) |
|
Africa (Cape Town) |
|
Asia Pacific (Hong Kong) |
|
Asia Pacific (Hyderabad) |
|
Asia Pacific (Jakarta) |
|
Asia Pacific (Melbourne) |
|
Asia Pacific (Mumbai) |
|
Asia Pacific (Osaka) |
|
Asia Pacific (Seoul) |
|
Asia Pacific (Singapore) |
|
Asia Pacific (Sydney) |
|
Asia Pacific (Tokyo) |
|
Canada (Central) |
|
Canada West (Calgary) |
|
China (Beijing) |
|
China (Ningxia); |
|
Europe (Frankfurt) |
|
Europe (Ireland) |
|
Europe (London) |
|
Europe (Milan) |
|
Europe (Paris) |
|
Europe (Spain) |
|
Europe (Stockholm) |
|
Europe (Zurich) |
|
Israel (Tel Aviv) |
|
Middle East (Bahrain) |
|
Middle East (UAE) |
|
South America (São Paulo) |
|
AWS GovCloud (US-East) |
|
AWS GovCloud (US-West) |
|
ARNs for syn-python-selenium-2.1
The following table lists the ARNs to use for version syn-python-selenium-2.1
of the
CloudWatch Synthetics runtime
in each AWS Region where it is available.
Region | ARN |
---|---|
US East (N. Virginia) |
|
US East (Ohio) |
|
US West (N. California) |
|
US West (Oregon) |
|
Africa (Cape Town) |
|
Asia Pacific (Hong Kong) |
|
Asia Pacific (Hyderabad) |
|
Asia Pacific (Jakarta) |
|
Asia Pacific (Melbourne) |
|
Asia Pacific (Mumbai) |
|
Asia Pacific (Osaka) |
|
Asia Pacific (Seoul) |
|
Asia Pacific (Singapore) |
|
Asia Pacific (Sydney) |
|
Asia Pacific (Tokyo) |
|
Canada (Central) |
|
Canada West (Calgary) |
|
China (Beijing) |
|
China (Ningxia); |
|
Europe (Frankfurt) |
|
Europe (Ireland) |
|
Europe (London) |
|
Europe (Milan) |
|
Europe (Paris) |
|
Europe (Spain) |
|
Europe (Stockholm) |
|
Europe (Zurich) |
|
Israel (Tel Aviv) |
|
Middle East (Bahrain) |
|
Middle East (UAE) |
|
South America (São Paulo) |
|
AWS GovCloud (US-East) |
|
AWS GovCloud (US-West) |
|
Common errors
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 that the AWS default profile is set up.
More common errors
For more information about common errors with the SAM, see AWS SAM CLI troubleshooting .