Writing a Serverless Cron Job
9/Feb 2020Benefits of a serverless cron job
What’s great about going serverless with a cron job is that there is no need to setup the environment, configure crontab, logging, error detection, etc. And you don’t have to worry about if the VM is down. It is also easier on your wallet.
Here I am using AWS Lambda for my serverless cron job.
Configuration and tips
AWS Lambda function can be triggered periodically using CloudWatch events. See this tutorial.
The logs (stdout and stderr) will be sent to CloudWatch Logs.
Python uncaught exceptions will be caught by Lambda and it will count as a failing run.
There’s a catch on error handling, that is it defaults to automatically retry the function after a failure. If the lambda function is vulnerable to rate limiting, make sure this option is turned off.
To ease development and deployment, make sure you use a script to pack the code and dependencies into a zip and upload it using awscli. This will save you a lot of time. I’ll share my script here:
#!/bin/bash MYPWD=`pwd` rm -f function.zip cd venv/lib/python3.7/site-packages zip -r9 ${MYPWD}/function.zip . cd $MYPWD zip -g function.zip main/main.py aws lambda update-function-code --function-name my-lambda-function --zip-file fileb://function.zip