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.
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:
- goes through the list of sites, verifies response is as expected
- 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)
- sends an email if at least one failure is detected
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?
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:
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!