Cloudwatch Events as Cron micro service

So, the product which I am currently working with is an E-commerce project where we had a requirement for some scheduled jobs.

These scheduled jobs can be categorised into 2 types i.e one time or recursive. An example for a recursive jobs could be a daily report which a merchant expects on the day end at a specific time. And an example for one time job could be a pre order i.e customer places an order for food for next day at a defined time.

I believe you would be clear of the use case by now. For this we were using a third party library called Agenda. This library has hard dependency on monogdb. Now, due to Covid-19 we were cutting down infrastructure cost and decided to get rid of MongoDb as we were already using Mysql as our primary database.

Hence to solve the issue as quickly as possible we decided to explore cloudwatch events and found that with a little tweeking we can solve the issue without using any database.

The idea is to use aws-sdk to create a new event on cloudwatch events using cron expression or repeat interval and bind it to a target SQS and input some constant json which has details regarding final action which needs to be performed.

Then create a consumer for SQS and on recieving the messages from queue check if the job was a one time event or not based on the constant json which we entered while creating the rule.

If the job is one time event then delete the job from cloudwatch events and perform the end operation otherwise just perform the end action which could be calling an api or pushing message into other queue.

I have already created a public repository which will help you to add/delete/list a single and recursive job from cloudwatch events.

This micro service can post message on SQS, publish message on SNS and can also make a HTTP request based on the job request.

For someone who would like to use it in kubernetes, I have added a docker file, you just need to build and tag this image in your kubernetes repositry and you are ready to go.

Total cost of cloudwatch events is $1 per 1 million events

And that’s it, you don’t need a db any more for any kind or scheduled events.

Guys this is the very first story I am writing on medium and I hope you liked it.

Javascript full stack developer and Devops engineer and Limetray.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store