AWS Lambda as the website monitoring tool.

Luke Dorosz
4 min readSep 6, 2016

--

AWS Lambda is a server less solutions to run some code without care about resources. You focus only on a logic of the function you want to run, the rest is on the AWS side. It’s possible to run function in periodic fashion or based on external events (CloudWatch).

Server less solutions are popular now because includes a lot of advantages. We don’t care about servers, operating systems etc. There is also cost savings aspect, you pay only for resources (like Memory) and time the function run. And what is most important, you pay only when function is executed.

Website Monitoring.

I’m a lazy person, so that’s why I’m always looking for easiest way to solve something. In this case, I’ve got some small test environment. One EC2 instance with database and Wordpress stack on it. Let’s skip discussion about purpose of this design. Let’s assume this is mandatory design for this test environment. This architecture is without redundancy, so I decided to add monitoring option. Maybe in the future some self-healing capabilities also. In this post I will focus on how I used AWS Lambda and CloudWatch for website state monitoring.

Requirements

The monitoring solutions should check particular URL and look for http code respond. I don’t won’t to do this directly from the server (like script and cron). I want to exclude potential server side issues. So I decided to do this on external resources and AWS Lambda fit this perfectly.

So how it works. For every few minutes Lambda verifies, if http code 200 is returned. In the next step the result is send to CloudWatch as custom metric. For simplifying, this custom metric can have this values:

  • If http respond code is 200 or 304, metric is set to 200.
  • If there is impossible to connect to website (for ex. httpd is death), metric is set to 100.
  • In any other case metric is set to 50.

In the next, there is an Alarm configured for this metric. If the metric value is lower than 200, the email notification will be send.

For simplicity and flexibility this solution is based on two Lambda functions:

  1. WebsiteCheck – this functions is trying to connect to specified URL and, check http code and send the value to the second function.
  2. SendMetric- this function sets CloudWatch metric based on received parameters: custom metric name and the metric value.

Configuration

So let’s jump into AWS Management Console.

On https://github.com/mrdoro/AWS-Lambda-website-monitoring you can find a code of this functions.

To fire up this we need two IAM roles:

  1. For WebsiteCheck function we can use pre-defined “AWSLambdaBasicExecutionRole” and permission to execute SendMetric (lambda:InvokeFunction).
  2. For SendMetric function Basic Execution will be enough.

Now let’s create a functions. Skip all blueprints and remember about proper IAM roles.

Don’t forget about Source Event for WebsiteCheck function. I used CloudWatch Events – Schedule

As a final step we need to define Alarm and email notification for this custom metric.

How much is it cost?

I assumed the website will be check every 5 minutes. That gives 8640 executions per month with average execution time 500 ms. So the cost is zero USD! (First 1 million requests per month is free). To be sure check the lambda calculator ( https://s3.amazonaws.com/lambda-tools/pricing-calculator.html).

Every client receives 10 CloudWatch metrics and 10 Alarms for free

“New and existing customers also receive 10 metrics (applicable to Detailed Monitoring for Amazon EC2 instances, Custom Metrics, or CloudWatch Logs*), 10 alarms, and 1 million API requests each month at no additional charge”

That’s it. I hope this idea will be useful for you.

This post appeared originally at my blog http://www.lukado.eu and was translated from Polish.

--

--

Luke Dorosz

New tech geek, traveler, 🏃‍♂️ and 🏌️‍♂️Independent Cloud Consultant&Architect. Leader of AWSUG PL. AWS Community Hero