Last February, AWS added support for creating canaries for API Gateway to Cloudwatch Synthetics, which I’ve been using lately to monitor some REST APIS successfully.
Let’s review some technical concepts first:
- Canaries: Node scripts that run on schedule to monitor endpoints, following the action path of your users. Basically, canaries create Lambda functions using Node.js as a library, working with HTTP/HTTPS.
- CloudWatch Synthetics: allow to create canaries without coding, just using templates without writing any code. It’s really easy to implement, and you can use, and you should, use Cloudformation.
Problem to solve
After some hard work, now we have our brand new Serverless App deployed and ready to be tested, comprised of a REST API and a Microfrontend – Cloudfront + S3.
We need to monitor our endpoints for latency and resiliency; how do we do it right away? I have already answered the question: creating a synthetic canary.
Implementing a Canary
You can find the Canaries dashboard in the AWS console:
Cloudwatch > Insights > Application monitoring > Synthetic canaries
Creating a canary for API gateway from the console it’s really straightforward; you are given two options: select an API Gateway API and stage or a Swagger template.
We are presented then with a series of options:
- Selection of the API-Gateway in the present Account/Region
- Selection of Stage
The endpoint URL should be populated automatically:
The next step, adding a HTTP request, is also straightforward. The configuration – parameters, headers – depends on the method that is being tested:
Now you can finalize the creation of the canary:
- Runtime version: you can leave the default version.
- We are shown the generated code of the Lambda function implementing the canary.
- Enviroment variables: variables to pass to the Lambda function.
- Schedule: different ways to execute the canary. In this case, we choose Run continuosly.
In the following screen, we can see the canary that has been created and have been in execution for a while:
Finally, a few metrics are shown: Duration, Errors (4xx), Faults (5xx).
As promised, canaries are really easy to implement.