How to run a python script in the cloud every minute for free with AWS lambda
Tags: serverless, python, tutorial, aws
Published: 2019-09-22 10:32:33

---
title: How to run a python script in the cloud every minute for free with AWS lambda
published: true
tags: serverless,python,tutorial,aws
canonical_url: https://medium.com/@Gp2mv3/how-to-run-a-python-script-in-the-cloud-every-minute-for-free-with-aws-lambda-b1e070779ff5
---

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](https://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 in2days@notnow.co, 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 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](https://aws.amazon.com/free/?all-free-tier).

![](https://cdn-images-1.medium.com/max/1024/1*-TWCjvht6OKiKQeKtuw-0A.png)<figcaption>The free tier has 3 different offers</figcaption>

### 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.

![](https://cdn-images-1.medium.com/max/1024/1*GspPNzd_geqTUzk3iH_bYg.png)<figcaption>Example of my every-minute rule for CloudWatch Events</figcaption>

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:

![](https://cdn-images-1.medium.com/max/1024/1*ylcElXwybT4ZLJP3HkiE9g.png)<figcaption>My remindMeTest Lambda design</figcaption>

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 !