For a side-project I built some months ago, I needed to run a small python script every minute (of around that), every day. I finally found the time to write about the implementation.
The project I built, NotNow.co, is a service that responds to emails it receives at a specific time, based on the email address used.
For instance, by sending (or forwarding) an email to firstname.lastname@example.org, the service will send your email back after 2 days. As an entrepreneur, I need that tool to remind me of important deadlines, bills, sales inquiries etc. I wanted a lightweight solution and didn’t want to pay for a VPS or something else.
I never played with Lambdas and Serverless in general so I gave it a try and developed the script. Wikipedia defines the Serverless concept as follow:
Serverless computing is a cloud-computing execution model in which the cloud provider runs the server, and dynamically manages the allocation of machine resources.
My project seems a typical case for Lambdas as I don’t want to manage anything around it. The script needs less than a second or two to execute, and doesn’t need a lot of resources, only an internet connexion.
AWS has a free tier, that includes 1 million lambda executions per month, yes, that’s a lot !
EC2 also includes 1GB of data transfered to the outer world (outside AWS services) per month, it’s thus possible to run my script free forever !
A month has less than 60 ⨯ 24 ⨯ 31 = 44640 minutes, hence 44640 lambda executions.
Besides that, the free tier includes much more AWS services.
After choosing the platform (AWS Lambda), the question was: How to trigger the function every minute ?
Execution of Lambda functions can be triggered by different methods, but to execute your function at a specific time, you need the “CloudWatch Events” trigger. The execution rules is defined like cron rules. You can also select a specific time of the day or a specific time.
For my project, I wanted to keep it simple, so I simply check every minute. A good optimization would be to also schedule sending via a second lambda to reduce the execution time.
After copy/pasting the script to AWS, then setting the environment variables and the trigger, the Lambda definition is as below:
Every execution is logged and alerts are sent if the function takes too much time, or if the script fails. This ensures that the function is always ran and that I don’t miss important reminders.
I hope this story can help understand how to execute a python function every minute for free. Don’t hesitate to leave a comment !