Monitor your website using CloudWatch Synthetics
“You can use Amazon CloudWatch Synthetics to create canaries, configurable scripts that run on a schedule, to monitor your endpoints and APIs. Canaries follow the same routes and perform the same actions as a customer, which makes it possible for you to continually verify your customer experience.” 
In this TeraTip, we are going to use a canary for monitoring a specific URL in order to know quickly if the website is up. So, let’s go to configure our canary…
Creating the Canary
The first step is to access the CloudWatch console. Here we can find Synthetics Canaries under the “Application monitoring” section in the left menu.
Then, we are going to create a canary by doing click on the orange button “Create canary”, and
luckily for us, already exists a blueprint called “heartbeat monitoring” which does exactly what we want: monitor the health of a website.
Now, let’s take a look at the configuration options:
First, we have the Name of the Canary, and after that, the most important value: the URL of the webpage that we want to monitor. As you see, it’s possible to add up to five endpoints to this canary.
The “Take screenshot” option is, as its name suggests, for taking a screenshot of the website every time the canary runs. This is what the customer sees when they access the URL.
The following section is the “Script editor”, and this is amazing because using the blueprint saves us from writing the whole script.
In this case, the script is written in Nodejs and makes use of the Puppeteer library.
Then, we need to define a schedule to run the canary. Here I’m choosing to run it continuously every 5 minutes and start immediately after the creation:
Some important to mention is that the canaries have a (very low) cost for each execution. This is minimal, but you need to know it. You can check this cost in this AWS URL:
Now is necessary to define a retention period and an S3 bucket for saving the results executions. As I just want to use this canary as a health check, I choose the minimum retention possible: 1 day for failure and successful executions.
This S3 bucket is automatically created if is the first canary that you are creating in the account. Otherways, the existing bucket is used.
Following the configuration, we need to set up the permissions for this canary.
I choose “Create a new role” and this automatically creates the following role and policy attached to it:
This role is assumed by the Lambda function that runs the script.
Finally, we need to set up what we want to do when the health check fails. There are two possibilities: create a CloudWatch alarm and/or send a notification using SNS. I choosed to create an alarm that will be triggered every time the health check fails in the last 5 minutes.
This alarm can be configured as usual, by setting up some action. For example, It’s possible to notify using an SNS topic, trigger some action over the EC2 instances that contains the web server or trigger a lambda function.
To finish, click on the orange button "Create canary". Now, let’s take a look at what was created and what we can see in the monitoring of the canary.
Resources created by the canary wizard
We already see the policy and role used by the canary, and also the S3 bucket defined to store the information:
This bucket will be created when we add the first canary and reused by the following ones.
As well, we have the alarm that was defined in the canary:
Fortunately, our website is up :), but if not, we will know quickly…
But there is one more resource involved, and probably the most important: the lambda function that runs the script every time the canary is executed. If you re-read the definition of Canary in the very first paragraph of this tip, it says:
“You can use Amazon CloudWatch Synthetics to create canaries, configurable scripts that run on a schedule, to monitor your endpoints and APIs…”
And here is such script:
You can check the code generated by using the blueprint chosen.
The first we will find on the Synthetics Canaries dashboard is a summary of the current state of all canaries that we have. In this case, there is just one: