Monitor your website using CloudWatch Synthetics
Updated: Jul 11
“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.” [1]
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, there 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.
Monitoring
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:
By accessing the canary will find specific and historical information about the last and previous executions:
Even, we can find the screenshot of the website (if you remember to check “Take screenshot” in the canary configuration) and related logs:
The aim of this TeraTip is to show an easy (as you could see), but not widely known, way to implement a health check monitoring of your URLs. As we can see, Canaries are very useful and they provide a lot of information that can help us to know the state of our website and take action faster.
References
Ignacio Rubio
Cloud Engineer
Teracloud
If you want to know more about Cloudwatch, we suggest going check Create Custom Metrics for AWS Glue Jobs. To learn more about cloud computing, visit our blog for first-hand insights from our team. If you need an AWS-certified team to deploy, scale, or provision your IT resources to the cloud seamlessly, send us a message here