Monitor website uptime with AWS for free !!!


website uptime

So, I decided to start a new blog. The main idea was born when my girlfriend (with whom I have been 3 years) continually asks me, what do I do in life?. Haha, as much as I have explained to her and she has already had a vague idea; It is very difficult to make myself understood due the technical terms I always have to use. Then I decided to create a blog. A simple blog where I collect in general my professional profile, my skills and my work.

This is not just for her. This is for everyone who can learn and is interested in the same stuff I do on my daily basis.

Additionally, as part of this project, I have decided to dedicate a small amount of time every month to writing a post. The idea is to share what I have learned and I do professionally as a Engineer in general. I have always believed, used and contributed to the Open Source movement (OSS: Open Source Software) and this is just another opportunity to contribute.

A lot of introduction, and I will apologize, is that it is my first post in my personal blog. Now if I begin to explain how I configured the AWS tools to monitor the uptime of my blog and notify me via Email when it is not Online due to some inconvenience.

The solution is very simple and consists of the following:

1. Let’s create a lambda function in AWS

  • Sign in in the AWS console
  • Select: Services -> Lambda -> Create function

AWS lambda function creation

Give it a name

AWS lambda function creation 2

Once it s created, just copy the code below changing http://www.yourwebsite.com for the FQDN of yours.

The lambda function:

'use strict';

var url = require('url');
var target = 'http://www.yourwebsite.com'; // Change this one

exports.handler = function(event, context, callback) {
  var urlObject = url.parse(target);
  var mod = require(
    urlObject.protocol.substring(0, urlObject.protocol.length - 1)
  );
  console.log('[INFO] - Checking ' + target);
  var req = mod.request(urlObject, function(res) {
    res.setEncoding('utf8');
    res.on('data', function(chunk) {
      console.log('[INFO] - Read body chunk');
    });
    res.on('end', function() {
      console.log('[INFO] - Response end');
      callback();
    });
  });

  req.on('error', function(e) {
    console.log('[ERROR] - ' + e.message);
    callback(e);
  });
  req.end();
};

Source

AWS lambda functionr

In the designer of the lambda function we are going to add a trigger:

  • Select: CloudWatch Events
  • Select: Create a new rule
  • For the schedule expresion I am going to set for 30 minutes

AWS lambda trigger

AWS lambda trigger 2

Your function designer should looks like this:

AWS lambda trigger 3

Let’s click on Monitoring on the Lambda function and confirm it is reporting our website as up:

AWS lambda metrics

2. Create SNS topic for send Email alert

In the AW console go to Services -> Simple Notification Service -> Topic -> Create topic

Create SNS topic

Once the topic has been created, let’s create an email subscription:

Create SNS email subscription

3. Create an Alarm in Cloudwatch!

So far we have:

  • A Lambda function that monitor the uptime of our website www.wecloudpro.com
  • A SNS topic with an email subscription to contact@wecloudpro.com
  • A Cloudwatch Alarm that monitors the errors in the lambda function (wich we are going to create just next)

In the AW console go to Services -> Cloudwatch -> Alarms -> Create alarm

For the metric select Lambda -> by Function Name -> wecloudpro_uptime (this is the name we gave to the lambda function we created in step 1)

AWS cloudwatch alarm

In the condition set greater than 0 as the image below:

Create cloudwatch alarm 2

Finally select the SN topic we just created previously:

Create cloudwatch alarm 3

4. Test! always test!

That’s all we need in order to get an email notification when our website is down. Let’s take www.wecloudpro.com down for our testing purposes and see what happens.

cloudwatch alarm

cloudwatch alarm 2

Pricing

This is cost of using a Lambda function…$0

aws lambda pricing

For the case of cloudwatch, it could cost $0,40/month if you are not part of the AWS free tier. See both cases below:

Cloudwatch free tier:

Cloudwatch free tier

Cloudwatch paid tier:

Cloudwatch paid tier

Improvements & additionals

  • -Set an alarm in Cloudwatch to notify when the website is back, meaning when Status is OK
  • -Create a cloudformation template to generate all these resources automatically
  • -Parse better the SNS email notifications

I will keep posting and working on these improvements, so keep watching my blog !.

Back to blog