Nicolas Cognaux - Gp2mv3

I share about development, productivity, open source and entrepreneurship.

How to run a python script in the cloud every minute for free with AWS lambda

2019-06-05 Nicolas Cognaux 3 min readDevelopment

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

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 [email protected], 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.

The AWS free tier

AWS has a free tier, that includes 1 million lambda executions per month, yes, that’s a lot!

EC2 also includes 1GB of data transferred 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.

aws 1

The free tier has 3 different offers

Now, how to trigger it every minute?

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.

aws 2

Example of my every-minute rule for CloudWatch Events

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.

The final design

After copy/pasting the script to AWS, then setting the environment variables and the trigger, the Lambda definition is as below:

aws 3

My remindMeTest Lambda design

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 run 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!