Sub topic 16
Monitoring endpoints with Synthetics canaries
This chapter assumes you can create CloudWatch alarms and SNS topics, read the AWS CLI, and know what an IAM role is. It covers CloudWatch Synthetics, which tests an endpoint from the outside.
Why probe from outside
Metrics such as CPU or request count show what your resources report about themselves. They can look healthy while users cannot reach the site, for example when DNS is broken or a certificate has expired. A canary is a script that runs on a schedule and acts like a user or API client. It checks that an endpoint answers correctly, and it records the result as CloudWatch metrics.
How a canary works
CloudWatch Synthetics runs each canary as a Lambda function. You supply the script in Node.js or Python. Each run does the following:
- The schedule (a rate such as every 5 minutes, or a cron expression) starts the run.
- The script requests a URL or runs a browser flow.
- The script passes or fails based on its checks.
- Synthetics stores artifacts (logs, screenshots, HAR files) in an S3 bucket you name.
- Synthetics publishes metrics to the
CloudWatchSyntheticsnamespace, with the dimensionCanaryName.
Metrics to alarm on
| Metric | Meaning |
|---|---|
| `SuccessPercent` | Share of runs in the period that passed |
| `Duration` | How long the run took, in milliseconds |
| `Failed` | Count of runs that failed |
A minimal API canary
This Python script checks that an endpoint returns HTTP 200. The handler name is set when you create the canary.
import urllib.request
from aws_synthetics.common import synthetics_logger as logger
def handler(event, context):
url = "https://example.com/health"
with urllib.request.urlopen(url, timeout=10) as response:
status = response.status
logger.info(f"Status {status}")
if status != 200:
raise Exception(f"Unexpected status {status}")
return "Canary passed"Raising an exception marks the run as failed. Package the script in a ZIP file under python/, upload it to S3, then create the canary:
aws synthetics create-canary \
--name checkout-health \
--artifact-s3-location s3://my-canary-artifacts/checkout/ \
--execution-role-arn arn:aws:iam::111122223333:role/CanaryRole \
--schedule Expression="rate(5 minutes)" \
--runtime-version syn-python-selenium-5.1 \
--code S3Bucket=my-canary-code,S3Key=checkout.zip,Handler=checkout.handlerThe runtime version string changes over time. Run aws synthetics describe-runtime-versions to see the current values. After creation, start the canary with aws synthetics start-canary --name checkout-health.
Alarming on a canary
Alarm on SuccessPercent so one transient failure does not page anyone:
aws cloudwatch put-metric-alarm \
--alarm-name checkout-canary-failing \
--namespace CloudWatchSynthetics \
--metric-name SuccessPercent \
--dimensions Name=CanaryName,Value=checkout-health \
--statistic Average \
--period 300 \
--evaluation-periods 3 \
--datapoints-to-alarm 2 \
--threshold 90 \
--comparison-operator LessThanThreshold \
--treat-missing-data breaching \
--alarm-actions arn:aws:sns:us-east-1:111122223333:ops-alertsHere breaching is deliberate. If the canary stops running, no data arrives, and you want that to count as a problem.
Common errors
- Execution role lacks permissions. The role needs access to write to the artifact bucket and to CloudWatch Logs. Without it the run fails before your script's checks.
- Canary in the wrong state. A new canary does not run until you start it.
- Private endpoints. To reach a resource inside a VPC, configure the canary with VPC subnets and security groups.
- Cost. Each run is billed, so a 1-minute schedule costs about five times a 5-minute one.
Exercise
Your canary runs every 5 minutes. It is accidentally stopped, so it publishes no data. The alarm uses treat-missing-data notBreaching. What happens, and how do you fix the alarm?
Show answer
The alarm stays out of ALARM because missing data is treated as healthy, so nobody learns the canary stopped. Change the setting to breaching (or missing combined with an alarm on the absence of data) so a silent canary raises an alert.