Get a Python Lambda to be Heartbeat Check for Your Website
What
I finally had to deal with Lambda! I realized I could utilize a truly free tier in AWS's otherwise dumpster fire of micro payments forever paradigm avoidance bias. So I decided to use it as practically as I could, which would be a simple heartbeat check (see if the domain is still alive) that I run as a cronjob from a virtual server that already exists in Digital Ocean.
Going in, this is just integrating Lambda without SQS / SNS / or Eventbridge. Just a bare bones, deploy a Lambda function and get it working. The reason being is, turning on those 4 other services will accrue costs per month. This setup will keep your tier completely free.
Basics
Get yourself an AWS account and create an IAM user just to ensure you don't always login as the root for administration. This is bad for security reasons and separation of concerns. The new user only needs Lambda access that you setup in security groups. Or, you could use the default security group settings if you don't really care.
Get yourself a domain that you want to monitor.
Get yourself an external server (or even your own personal machine) that would run the automated cron every n minutes or hours depending on the frequency at which you want to monitor the site.
Jump In
Create a new Lambda function using a blank template. You can find the Lamba section by looking in the search bar at the top of the AWS dashboard.
Add a base layer off the shelf from Amazon AMI's. There is a nice AWSDataWrangler-Python39
that should be there in the sidebar. It at least was there when I published this in July 2022.
Once you add the layer and leave the defaults in for the test it forces you to create, you are ready to paste in the few lines of python to get this working.
Since you probably just want to copy pasta the code from here, I won't type anymore instructions.
import requests
def lambda_handler(event, context):
r = requests.get("https://duckduckgo.com")
return r.status_code
Hit the deploy button. Run the test. Confirm you get back 200 or something similar.
Logfile setup
Contrary to popular script-kiddie belief, logs are stored in /var/log
, so create a subfolder under there like sudo mkdir /var/log/duckduckgocheck/
or similar and then sudo chown -R $USER:$USER /var/log/duckduckgocheck/
. Congrats you are ready for your bash script.
Bash script
Create a bash script on your local machine or server as check.sh or similar in a location you will reference later with the following:
#!/bin/bash
status=$(curl -s <your aws url>)
if [[ $status != 200 ]]; then
echo "fail!"
fi
today="$(date '+%Y-%m-%d %H:%M:%S')"
echo "$today $status" >> /var/log/duckduckgostatus/logfile
Crontab setup
Run crontab -e
and add the following at the bottom:
0 * * * * /bin/bash <full path to your script>/check.sh
This means it will run every hour.
You should start seeing a date and status 200 in the location /var/log/duckduckgostatus/logfile
at the next hour interval.
Sign up for the newsletter to email any questions or comments you may have. Enjoy your new lambda function.