Using AWS Lambda to verify site uptime

Recently I had to start looking into the AWS Lambda as it might become part of a portfolio of cloud services we shall start depending on.

As you might already know currently Lambdas can be written only using:

  • node.js (which I passionately hate);
  • Java (boring: I’d need to package a jar);
  • Python (which I chose not to learn);
  • no native Go support as of now (although at some point in time maybe Amazon reacts to this forum post requesting exactly that). Yes I can “package my Go binary“ but that seems a bit non-native way of doing it.

I wanted the simplest, quickest POC… so, node it is.

After I have defined a hammer, then came the search for a perfect candidate :). I chose a very simple use case: testing if my website is still up.

Lambda code

This is what I came up after couple of hours.

This code is also copy/paste friendly — I didn’t use any fancy extra library like async, Future or whatever is considered cool by the hipster node developers, sorry about that (but these kinds of choices is exactly the reason why I hate node that much).

What it does is:

  1. goes through the list of sites, verifies response is as expected
  2. expects all sites to be https (this might not be suitable your case — you might want to use require(‘http’) in place of https below)
  3. sends an email if at least one failure is detected

Trigger

As a trigger, I chose “CloudWatch Events — Schedule” which seems to be a cron-like way of triggering Lambdas in AWS. You just set up the timeout of for example 15 minutes (1 minute is minimum though).

Maybe the coolest thing about it is that you get automatically logging since all CloudWatch rule executions are being logged. Cool, right?

Role

You might also need to add suitable role, since sending emails is of course not enabled by default, I ended up writing a quick policy extension like this:

{
"Statement": [
{
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Effect": "Allow",
"Resource": "arn:aws:logs:*:*:*"
},
{
"Action": [
"ses:SendEmail"
],
"Effect": "Allow",
"Resource": "arn:aws:ses:us-east-1:<accountid>:identity/alarmemail@gmail.com"
}
],
"Version": "2012-10-17"
}

Billing

Seems like the price for Lambdas (well, at least for now) is so low that this kind of scripts basically ends up being executed for free.

Of course, it goes without saying that you should be limiting the execution time to something reasonable just to be sure, and that you don’t trigger it too often, but that’s it — I stay far far far below $1 per month and that’s good!

Profit and have fun

I hope Lambda stays cheap as it is since this is very simple/extensive way of doing simple stuff like checking a web site!

I still think it sucks for complex use cases, but… that’s a completely different rant there!

Show your support

Clapping shows how much you appreciated Milan Aleksić’s story.