Making AWS Lambda Scheduled Jobs More Generic And Reusable With Rules & Targets
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!
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.
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.
After entering this information you can add a name and description for this aws "rule" that encompasses all the cron scheduling and input parameters.
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".
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!
Your comment will be posted after it is approved.
Leave a Reply.
The posts on this site are written and maintained by Jim Lynch. About Jim...