Monitoring your website for free with Azure Functions and PowerShell with SMS alerts — done in 5 minutes
So called serverless computing has arrived to pretty much all major cloud computing platforms, including Azure under the “Azure Functions” name. As curious as I am, I was wondering what all the fuss was about so I came up with a use case to try it out.
One of the best things about Azure Functions is that I can code and test my PowerShell (or Bash!) script locally, then upload it to Functions just to have it running periodically without having to spin up a server anywhere.
Disclaimer: I am not a developer, so I tried to stick to something I feel comfortable with. That’s PowerShell :-)
My chosen use case was to get an SMS if one of my sites is down. Sounds useful enough, so let’s start building the solution.
Let’s first make sure you have a Twilio account (get one for free here) and that you have your account sid, auth token and Twilio number handy.
Now back to Azure Functions, let’s click on Try it for free and create any function as PowerShell is not an option on this screen (I guess because it’s in experimental phase right now):
Then go ahead and create a new TimeTrigger function for PowerShell:
I’ve also selected to run it every time instead of the default 5 minutes:
Once created, paste the below code and make sure you populate the $poorsodnumber variable with your phone number.
The code basically sends a GET request to https://bing.com and checks if the returned HTTP status code is 200. If it is, it just logs a message on the console, but if it’s not it will send an SMS to our number using Twilio’s API.
Don’t forget to save your changes!!
Now if you had a free Azure account and created your function app from the Azure Portal, you would be able to edit environment variables here:
As we just started playing without an account and can’t edit environment variables, we have to go back to our code and change $env:TWILIO_ACCOUNT_SID, $env:TWILIO_AUTH_TOKEN and $env:TWILIO_NUMBER with the actual values for each of them from our Twilio account.
Press save and run and that’s it! Our little function is running already! but unless your site is down, you won’t be getting any SMS any time soon. You should be seeing instead this sort of output on your console:
I wanted to make sure Twilio integration worked, so I’ve changed the expected status code in our function from “200” to “404”. That would tell my script to send me an SMS if we don’t get a 404 — as we know Bing is up, we won’t be getting a 404 and I’ll get an SMS.
Console will spill different output as per our PowerShell code:
It doesn’t get much more faster and cooler than that, really.