There are more AWS SDK examples available in the AWS Doc SDK Examples
CloudWatch Synthetics examples using AWS CLI
The following code examples show you how to perform actions and implement common scenarios by using the AWS Command Line Interface with CloudWatch Synthetics.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to use associate-resource.
- AWS CLI
-
To associate a canary with a group
The following
associate-resourceexample associates a canary with a group nameddemo_group.aws synthetics associate-resource \ --group-identifierdemo_group\ --resource-arnarn:aws:synthetics:us-east-1:123456789012:canary:demo_canaryThis command produces no output.
For more information, see Synthetic monitoring (canaries) in the Amazon CloudWatch User Guide.
-
For API details, see AssociateResource
in AWS CLI Command Reference.
-
The following code example shows how to use create-canary.
- AWS CLI
-
To create a canary
The following
create-canaryexample creates a canary nameddemo_canary.aws synthetics create-canary \ --namedemo_canary\ --code '{"S3Bucket": "artifacts3bucket", "S3Key":"demo_canary.zip", "Handler": "index.lambda_handler"}' \ --artifact-s3-locations3://amzn-s3-demo-bucket/demo_canary.zip\ --execution-role-arnarn:aws:iam::123456789012:role/demo_canary_role\ --schedule Expression="rate(10 minutes)" \ --runtime-versionsyn-nodejs-puppeteer-9.1Output:
{ "Canary": { "Id": "a1b2c3d4-5678-90ab-cdef-example11111", "Name": "demo_canary", "Code": { "Handler": "index.lambda_handler" }, "ExecutionRoleArn": "arn:aws:iam::123456789012:role/demo_canary_role", "Schedule": { "Expression": "rate(10 minutes)", "DurationInSeconds": 0 }, "RunConfig": { "TimeoutInSeconds": 600, "MemoryInMB": 1000, "ActiveTracing": false }, "SuccessRetentionPeriodInDays": 31, "FailureRetentionPeriodInDays": 31, "Status": { "State": "CREATING", "StateReasonCode": "CREATE_PENDING" }, "Timeline": { "Created": "2024-10-15T19:03:08.826000+05:30", "LastModified": "2024-10-15T19:03:08.826000+05:30" }, "ArtifactS3Location": "amzn-s3-demo-bucket/demo_canary.zip", "RuntimeVersion": "syn-nodejs-puppeteer-9.1", "Tags": {} } }For more information, see Synthetic monitoring (canaries) in the Amazon CloudWatch User Guide.
-
For API details, see CreateCanary
in AWS CLI Command Reference.
-
The following code example shows how to use create-group.
- AWS CLI
-
To create a group
The following
create-groupexample creates a group nameddemo_group.aws synthetics create-group \ --namedemo_groupOutput:
{ "Group": { "Id": "example123", "Name": "demo_group", "Arn": "arn:aws:synthetics:us-east-1:123456789012:group:example123", "Tags": {}, "CreatedTime": "2024-10-15T14:47:23.811000+05:30", "LastModifiedTime": "2024-10-15T14:47:23.811000+05:30" } }For more information, see Synthetic monitoring (canaries) in the Amazon CloudWatch User Guide.
-
For API details, see CreateGroup
in AWS CLI Command Reference.
-
The following code example shows how to use delete-canary.
- AWS CLI
-
To permanently delete a canary
The following
delete-canaryexample deletes a canary nameddemo_canary.aws synthetics delete-canary \ --namedemo_canaryThis command produces no output.
For more information, see Synthetic monitoring (canaries) in the Amazon CloudWatch User Guide.
-
For API details, see DeleteCanary
in AWS CLI Command Reference.
-
The following code example shows how to use delete-group.
- AWS CLI
-
To delete a group
The following
delete-groupexample deletes a group nameddemo_group.aws synthetics delete-group \ --group-identifierdemo_groupThis command produces no output.
For more information, see Synthetic monitoring (canaries) in the Amazon CloudWatch User Guide.
-
For API details, see DeleteGroup
in AWS CLI Command Reference.
-
The following code example shows how to use describe-canaries-last-run.
- AWS CLI
-
To see information from the most recent run of each canary
The following
describe-canaries-last-runexample returns the most recent run of each canary that you have created.aws synthetics describe-canaries-last-runOutput:
{ "CanariesLastRun": [ { "CanaryName": "demo_canary", "LastRun": { "Id": "a1b2c3d4-5678-90ab-cdef-example11111", "Name": "demo_canary", "Status": { "State": "PASSED", "StateReason": "", "StateReasonCode": "" }, "Timeline": { "Started": "2024-10-15T19:20:39.691000+05:30", "Completed": "2024-10-15T19:20:58.211000+05:30" }, "ArtifactS3Location": "cw-syn-results-123456789012-us-east-1/canary/us-east-1/demo_canary-abc-example1234/2024/10/15/13/50-39-690" } } ] }For more information, see Synthetic monitoring (canaries) in the Amazon CloudWatch User Guide.
-
For API details, see DescribeCanariesLastRun
in AWS CLI Command Reference.
-
The following code example shows how to use describe-canaries.
- AWS CLI
-
To list canaries in your account
The following
describe-canariesexample lists the details of canaries in your account.aws synthetics describe-canariesOutput:
{ "Canaries": [ { "Id": "a1b2c3d4-5678-90ab-cdef-example11111", "Name": "demo_canary", "Code": { "SourceLocationArn": "arn:aws:lambda:us-east-1:123456789012:layer:cwsyn-demo_canary-a1b2c3d4-5678-90ab-cdef-example11111b8:1", "Handler": "pageLoadBlueprint.handler" }, "ExecutionRoleArn": "arn:aws:iam::123456789012:role/service-role/CloudWatchSyntheticsRole-demo_canary-a12-a123bc456789", "Schedule": { "Expression": "rate(5 minutes)", "DurationInSeconds": 0 }, "RunConfig": { "TimeoutInSeconds": 300, "MemoryInMB": 1000, "ActiveTracing": false }, "SuccessRetentionPeriodInDays": 31, "FailureRetentionPeriodInDays": 31, "Status": { "State": "RUNNING" }, "Timeline": { "Created": "2024-10-15T18:55:15.168000+05:30", "LastModified": "2024-10-15T18:55:40.540000+05:30", "LastStarted": "2024-10-15T18:55:40.540000+05:30" }, "ArtifactS3Location": "cw-syn-results-123456789012-us-east-1/canary/us-east-1/demo_canary-a12-a123bc456789", "EngineArn": "arn:aws:lambda:us-east-1:123456789012:function:cwsyn-demo_canary-a1b2c3d4-5678-90ab-cdef-example111118:1", "RuntimeVersion": "syn-nodejs-puppeteer-9.1", "Tags": { "blueprint": "heartbeat" } } ] }For more information, see Synthetic monitoring (canaries) in the Amazon CloudWatch User Guide.
-
For API details, see DescribeCanaries
in AWS CLI Command Reference.
-
The following code example shows how to use describe-runtime-versions.
- AWS CLI
-
To return a list of synthetics canary runtime versions
The following
describe-runtime-versionsexample returns the list of synthetics canary runtime versions.aws synthetics describe-runtime-versionsOutput:
{ "RuntimeVersions": [ { "VersionName": "syn-nodejs-puppeteer-9.1", "Description": "Security fixes and bug fix for date range error in har. Dependencies: Node JS 20.x, Puppeteer-core 22.12.1, Chromium 126.0.6478.126", "ReleaseDate": "2024-10-02T05:30:00+05:30" }, { "VersionName": "syn-nodejs-puppeteer-9.0", "Description": "Upgraded Chromium and Puppeteer. Dependencies: Node JS 20.x, Puppeteer-core 22.12.1, Chromium 126.0.6478.126", "ReleaseDate": "2024-07-22T05:30:00+05:30" }, { "VersionName": "syn-nodejs-puppeteer-8.0", "Description": "Upgraded Chromium and Puppeteer. Dependencies: Node JS 20.x, Puppeteer-core 22.10.0, Chromium 125.0.6422.112", "ReleaseDate": "2024-06-21T05:30:00+05:30" }, { "VersionName": "syn-nodejs-puppeteer-7.0", "Description": "Upgraded Chromium and Puppeteer. Dependencies: Node JS 18.x, Puppeteer-core 21.9.0, Chromium 121.0.6167.139", "ReleaseDate": "2024-03-08T05:30:00+05:30" }, { "VersionName": "syn-nodejs-puppeteer-6.2", "Description": "Updated shared libraries for Chromium and added ephemeral storage monitoring. Dependencies: Node JS 18.x, Puppeteer-core 19.7.0, Chromium 111.0.5563.146", "ReleaseDate": "2024-02-02T05:30:00+05:30" }, { "VersionName": "syn-nodejs-puppeteer-6.1", "Description": "Added puppeteer launch retry. Dependencies: Node JS 18.x, Puppeteer-core 19.7.0, Chromium 111.0.5563.146", "ReleaseDate": "2023-11-13T05:30:00+05:30", "DeprecationDate": "2024-03-08T13:30:00+05:30" }, { "VersionName": "syn-nodejs-puppeteer-6.0", "Description": "Reduced X-Ray traces of a canary run, improved duration metric and upgraded to NodeJS 18.x. Dependencies: Node JS 18.x, Puppeteer-core 19.7.0, Chromium 111.0.5563.146", "ReleaseDate": "2023-09-15T05:30:00+05:30", "DeprecationDate": "2024-03-08T13:30:00+05:30" }, { "VersionName": "syn-nodejs-puppeteer-5.2", "Description": "Updated shared libraries for Chromium. Dependencies: Node JS 16.x, Puppeteer-core 19.7.0, Chromium 111.0.5563.146", "ReleaseDate": "2024-02-01T05:30:00+05:30" }, { "VersionName": "syn-nodejs-puppeteer-5.1", "Description": "Fixes a bug about missing request headers in har. Dependencies: Node JS 16.x, Puppeteer-core 19.7.0, Chromium 111.0.5563.146", "ReleaseDate": "2023-08-09T05:30:00+05:30", "DeprecationDate": "2024-03-08T13:30:00+05:30" }, { "VersionName": "syn-nodejs-puppeteer-5.0", "Description": "Upgraded Puppeteer and Chromium. Dependencies: Node JS 16.x, Puppeteer-core 19.7.0, Chromium 111.0.5563.146", "ReleaseDate": "2023-07-21T05:30:00+05:30", "DeprecationDate": "2024-03-08T13:30:00+05:30" }, { "VersionName": "syn-nodejs-puppeteer-4.0", "Description": "Upgraded to NodeJS 16.x. Dependencies: Node JS 16.x, Puppeteer-core 5.5.0, Chromium 92.0.4512.0", "ReleaseDate": "2023-05-01T05:30:00+05:30", "DeprecationDate": "2024-03-08T13:30:00+05:30" } ] }For more information, see Synthetic monitoring (canaries) in the Amazon CloudWatch User Guide.
-
For API details, see DescribeRuntimeVersions
in AWS CLI Command Reference.
-
The following code example shows how to use disassociate-resource.
- AWS CLI
-
To remove a canary from a group
The following
disassociate-resourceexample removes a canary from the group nameddemo_group.aws synthetics disassociate-resource \ --group-identifierdemo_group\ --resource-arnarn:aws:synthetics:us-east-1:123456789012:canary:demo_canaryThis command produces no output.
For more information, see Synthetic monitoring (canaries) in the Amazon CloudWatch User Guide.
-
For API details, see DisassociateResource
in AWS CLI Command Reference.
-
The following code example shows how to use get-canary-runs.
- AWS CLI
-
To retrieve a list of runs for a specified canary
The following
get-canary-runsexample retrieves a list of runs for the canary nameddemo_canary.aws synthetics get-canary-runs \ --namedemo_canaryOutput:
{ "CanaryRuns": [ { "Id": "a1b2c3d4-5678-90ab-cdef-example11111", "Name": "demo_canary", "Status": { "State": "PASSED", "StateReason": "", "StateReasonCode": "" }, "Timeline": { "Started": "2024-10-16T10:38:57.013000+05:30", "Completed": "2024-10-16T10:39:25.793000+05:30" }, "ArtifactS3Location": "cw-syn-results-123456789012-us-east-1/canary/us-east-1/demo_canary-abc-example1234/2024/10/15/13/50-39-690" } ] }For more information, see Synthetic monitoring (canaries) in the Amazon CloudWatch User Guide.