website monitoring using AWS Lambda functions

Website Status Checking Using AWS Lambda

Every time I work on my website either updating WordPress or changing the theme, I seem to lose my the code snippet for tracking my web traffic. So when I go to look at the statistics it regularly has been showing no data, 0 active users. So, in this blog, I’m going to detail how to create an alert system, so you can get an Email or SMS notification. The monitoring system I am detailing has 2 programs, one to check the website is available and the other sends an alert it the code for traffic analysis is missing.

A Google Analytics page showing no data.

Login to AWS

You will need an Amazon AWS account before you can access the https://aws.amazon.com/console/

If you have not used AWS before, then the very first thing you will want to do is choose your region. I recommend using a location that is very close to your website or if you are like me and hosting on Amazon, then in the same region. If you want to send SMS notifications, then you may have no choice but to choose another region that is supporting SMS.

In AWS you must first decide which region you are going to work in.

Step 1: Setup The Notifications

Search for and go into the Amazon Simple Notification Service (SNS).

You can get started with Amazon SNS in minutes by using the AWS Management Console. Search for SNS.

If it is your first time here you will see an introduction screen, just click on the blue “getting started” button, then create the topic.

In SNS click on create topic.

You want to create 2 new topics

  • website-offline
  • missing-analytics

You will be prompted for the “topic name” and if you want to setup SMS notification, then you need to come up with a 10 character “display name” too.

Create a new topic has 2 fields to fill out.

To get email notifications you’ll need to subscribe to the notification topic.

Click on the SNS create topic button.

Note Frankfurt currently doesn’t support SMS. Choose the “protocol” Email and provide an address that will be used to get notifications.

Choose email and provide your the address that will receive notifications.

You will need to go into your Email account and click on the subscribe button.

Confirming an SNS service email subscription.

The final step is to set up permissions to allow the Lambda, which will be checking the website. This is an important step or you will be getting AuthorizationErrorException messages, which actually lead to me wanting to write this article. It seems this setup is so trivial that nobody seems to have written about it in some time. I am actually also learning by documenting this and you can see I below I just give Everyone on the AWS account access. If you have a larger organisation, this might not be such a good idea.

In the edit topic policy dialog you need to select 'Everyone' under the basic view -> Allow these users to publish messages to this topic.

You might also want to allow other users to subscribe, in which case you’ll be adapting the settings under “allow these users to subscribe to this topic”.

I’ve only screen provided screenshots for setting up one topic. So repeat the above steps until you have both ready.

Step 2: Setup Lambda Functions

The two programs you’ll need can be found in my GitHub repository https://github.com/neilspink/aws-lambda-website-status-checking

Using the console search for “Lambda”

In the AWS Management Console search for Lambda.

In the Lambda management console, you now create new Lambda functions

  • website-offline
  • missing-analytics

In the Lambda management console click on create function.

When you create the first function you’ll also need to create a new custom role. This would be used to give your Lambda function permissions to do things on your account. We just need a blank one for this time.

Provide the function name and set hte languagr which is Python 2.7

When you choose ‘create a custom role’ you should immediately see the following screen, where you need to provide a good name for your role.

Create a role named lambda-website-checker and click the done button

Click on the create the function button and you’ll be looking at the Lambda editor, which can initially be overwhelming. When you see this JUST Scroll down.

The AWS lambda function editor

Then you can copy and paste in the Python script from https://github.com/neilspink/aws-lambda-website-status-checking (click on RAW view of the files when you want to copy)

Copy paste in your code and save.

Click SAVE and then we can test it.

A Lambda function can be given input parameters, we don’t need any, but still need one.

You first need to create a test event

We will just remove all the sample data; key 1 – 3. and click the create button found at the botton of the screen.

Remove sample data in test data dialog

Now we are ready to run a test and hopefully now see in the execution results “Website Is Alive!” ?  (Do leave a comment if you have problems)

Finally you can run a test

Create your next function and then we can do the third and final step.

Step 3: Schedule Monitoring

Using the console search for “CloudWatch”.

Search for AWS CloudWatch

Then you can create a new rule.

Select rules and create

The final task is to you have to do is define the schedule and select your Lambda function.

Define the schedule and choose the lambda function

Now repeat for the other function and your done.

Summary

Let me know if you have any difficulties or problems in the comments below, then I can maybe improve the instructions here. I also recorded this as a video here https://youtu.be/25LmRQE904w

 

Leave a Reply

Your email address will not be published. Required fields are marked *