<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nicolas Cognaux</title>
    <description>The latest articles on DEV Community by Nicolas Cognaux (@gp2mv3).</description>
    <link>https://dev.to/gp2mv3</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F231163%2Fa4404115-5907-4ddc-aa9a-46ba5357171b.jpeg</url>
      <title>DEV Community: Nicolas Cognaux</title>
      <link>https://dev.to/gp2mv3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gp2mv3"/>
    <language>en</language>
    <item>
      <title>How to run a python script in the cloud every minute for free with AWS lambda</title>
      <dc:creator>Nicolas Cognaux</dc:creator>
      <pubDate>Wed, 05 Jun 2019 16:44:05 +0000</pubDate>
      <link>https://dev.to/gp2mv3/how-to-run-a-python-script-in-the-cloud-every-minute-for-free-with-aws-lambda-1k2j</link>
      <guid>https://dev.to/gp2mv3/how-to-run-a-python-script-in-the-cloud-every-minute-for-free-with-aws-lambda-1k2j</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  The project
&lt;/h3&gt;

&lt;p&gt;The project I built, &lt;a href="https://notnow.co"&gt;NotNow.co&lt;/a&gt;, is a service that responds to emails it receives at a specific time, based on the email address used.&lt;/p&gt;

&lt;p&gt;For instance, by sending (or forwarding) an email to &lt;a href="mailto:in2days@notnow.co"&gt;in2days@notnow.co&lt;/a&gt;, 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.&lt;/p&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Serverless computing is a cloud-computing execution model in which the cloud provider runs the server, and dynamically manages the allocation of machine resources.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  The AWS free tier
&lt;/h3&gt;

&lt;p&gt;AWS has a free tier, that includes 1 million lambda executions per month, yes, that’s a lot !&lt;/p&gt;

&lt;p&gt;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 !&lt;/p&gt;

&lt;p&gt;A month has less than 60 ⨯ 24 ⨯ 31 = 44640 minutes, hence 44640 lambda executions.&lt;/p&gt;

&lt;p&gt;Besides that, the free tier includes much &lt;a href="https://aws.amazon.com/free/?all-free-tier"&gt;more AWS services&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nION8ayI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A-TWCjvht6OKiKQeKtuw-0A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nION8ayI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2A-TWCjvht6OKiKQeKtuw-0A.png" alt=""&gt;&lt;/a&gt;The free tier has 3 different offers&lt;/p&gt;

&lt;h3&gt;
  
  
  Now, how to trigger it every minute ?
&lt;/h3&gt;

&lt;p&gt;After choosing the platform (AWS Lambda), the question was: How to trigger the function every minute ?&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GGv2lt_0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AGspPNzd_geqTUzk3iH_bYg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GGv2lt_0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AGspPNzd_geqTUzk3iH_bYg.png" alt=""&gt;&lt;/a&gt;Example of my every-minute rule for CloudWatch Events&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h3&gt;
  
  
  The final design
&lt;/h3&gt;

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

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6KSu4fnO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AylcElXwybT4ZLJP3HkiE9g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6KSu4fnO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1024/1%2AylcElXwybT4ZLJP3HkiE9g.png" alt=""&gt;&lt;/a&gt;My remindMeTest Lambda design&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;I hope this story can help understand how to execute a python function every minute for free. Don’t hesitate to leave a comment !&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>python</category>
      <category>tutorial</category>
      <category>aws</category>
    </item>
  </channel>
</rss>
