Jim Lynch Codes
  • Blog
  • About Jim

Writings about one coder's stories & experiences.

Making AWS Lambda Scheduled Jobs More Generic And Reusable With Rules & Targets

3/17/2018

0 Comments

 
Yep, this blog post is the result of yet another great discovery by yours truly! After making a few AWS Lambda services that automate Twitter activity I realized that I needed to deploy the same function many times, each as a scheduled event in AWS but with slightly different configurations (such as twitter access keys for the desired account, keywords for posts to like, etc). I wanted to keep the same core codebase of logic for each type of lambda function but somehow deploy multiple versions of it, and I wanted to be able to upload the code in one place and have all the places where it's used be updated without having to deploy to each one individually. Here's how I managed to do it!

When A Config File Doesn't Cut It

Being the emphatic little Clojurian that I am, I used the cljs-lambda leiningen template to scaffold out my aws lambda function which right out of the box creates a separate little "config" file for you to put constants, configuration data, and other things you might need to come in and tweak later. Of course this separation of concerns is not unique to clojure, and in fact this generalization of lambdas that I'm about to explain can be used in aws lambda functions written in javascript, python, java, or whatever else the aws platform decides to support. A config file is all well and good for a single aws lambda function that's meant to live as one thing, but what about when old Matty Scott wants you to deploy the same lambda function but with a slightly different config file containing his twitter credentials? Do you make a whole new git repo and duplicate the entire codebase? Surely once this grows to a few people it would turn into a nightmare to update something across all those projects, and juggling multiple config files in a single project doesn't sound fun either. What I really needed to do was completely rip the config file out of the source code and instead pass that data in as arguments to the lambda function. Welp, that's exactly what I did, and it turns out that even for scheduled events (think AWS cron jobs) you can pass parameters into your function when invoking it. Let's take a closer look at that.

AWS Lambda Functions As Scheduled Events With Input Parameters

If you haven't already, sign up for an aws account an log into your console. I'm assuming you already have a lambda function up there in aws and want to create some scheduled jobs to run it at some predefined times. As an example let's take my cljs twitter unfollower example and create one version for Jim and one version for Louie. Now, you might be tempted to try to add the the cloudwatch event right from the lambda section of AWS, but watch out because for some reason currently the cloudwatch event triggers rule creator does not give you the option to pass input params into the lambda function. 
Picture
Instead, go to Services in the menu bar at the top of the page and choose CloudWatch. Then under Events click on Rules. This will bring you to a page where you can enter the exact cron expression you want (under Event Source on the left-hand side) and then under Targets -> Configure input you can enter some JSON text, and HUZZAHH, you've put in some input parameters!! Finding this hidden little buried away hidden gem of an input field was the eureka moment for me that I realized that this was where I could put the config data instead of packaging it in with the source code for the lambda function.
Picture
After entering this information you can add a name and description for this aws "rule" that encompasses all the cron scheduling and input parameters. 
Picture
Once you have one or more cloudwatch scheduled events linked to your AWS Lambda function you can see them back in the Lambda section of AWS when you click on your function and then the box labeled "Cloudwatch Events". 
Picture

Key Takaways

The main thing to take away here is that in order to make it easy for your scheduled events to be leveraged by multiple clients or customers you should make one single, generic codebase that expects input parameters and then you create and AWS Rule for each client or customer. This gives you total control over the frequency of execution and configuration parameters. It's a simple tip, but I hope you take this mighty power and do something awesome with it!
0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    ​Author

    Picture
    The posts on this site are written and maintained by Jim Lynch. About Jim...
    Follow @JimLynchCodes
    Follow @JimLynchCodes

    Categories

    All
    Actionscript 3
    Angular
    AngularJS
    Automated Testing
    AWS Lambda
    Behavior Driven Development
    Blockchain
    Blogging
    Business Building
    C#
    C / C++
    ClojureScript / Clojure
    Coding
    Community Service
    CS Philosophy
    Css / Scss
    Dev Ops
    Firebase
    Fitness
    Flash
    Front End
    Functional Programming
    Git
    Go Lang
    Haskell
    Illustrations
    Investing
    Java
    Javascript
    Lean
    Life
    Linux
    Logic Pro
    Music
    Node.js
    Planning
    Productivity
    Professionalism
    Python
    React
    Redux / Ngrx
    Refactoring
    Reusable Components
    Rust
    Security
    Serverless
    Shell Scripting
    Swift
    Test Driven Development
    Things
    TypeScript
    Useful Sites
    Useful Tools
    Video
    Website Development
    WebStorm
    Writing

    Archives

    August 2021
    February 2021
    January 2021
    October 2020
    September 2020
    May 2020
    April 2020
    February 2020
    January 2020
    December 2019
    October 2019
    September 2019
    August 2019
    July 2019
    June 2019
    May 2019
    April 2019
    March 2019
    February 2019
    January 2019
    December 2018
    November 2018
    October 2018
    September 2018
    August 2018
    June 2018
    May 2018
    April 2018
    March 2018
    February 2018
    January 2018
    December 2017
    November 2017
    October 2017
    September 2017
    August 2017
    July 2017
    May 2017
    April 2017
    March 2017
    February 2017
    January 2017
    December 2016
    November 2016
    October 2016
    September 2016
    August 2016
    July 2016
    June 2016
    May 2016
    April 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015

    RSS Feed

  • Blog
  • About Jim
JimLynchCodes © 2021