How to use AWS EventBridge to schedule tasks
metacollective
Posted on February 16, 2022
In my last blog, I wrote about how you can create your own Twitter bot using the serverless framework and deploy it on AWS. And, now that I have that function already deployed on AWS, I wish to have an automated process of running this lambda function periodically (like a scheduled job or corn tasks)
One way of doing it on AWS is by using their EventBridge service. This is how AWS describes EventBridge
Amazon EventBridge is a serverless event bus that makes it easier to build event-driven applications at scale using events generated from your applications, integrated Software-as-a-Service (SaaS) applications, and AWS services.
To get started, log in to your AWS console and go to the EventBridge page.
Click on the "Create rule" button and that should take you to a new rule form. Fill in the name and description field. Then select the "Schedule" option from the Define pattern filed like this -
There are 2 ways of scheduling your event. One is at a fixed rate of every x minute/hours/days where x is a numeric value or you can also set them up as a cron expression. You can read all about their pattern rules - https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html#CronExpressions
I am going to select cron expression because I want to send 2 tweets every day. One at 9 am in the morning and the other at 5 pm in the evening.
If you are new to corn expression, then AWS has given a very nice and easy table to understand each entry
Since I want to trigger my event twice a day at a particular hour of the day, my corn job will look like this - (0 9,17 * * ? *)
Note the comma between 9 & 17
AWS will also give you the next 10 triggers so you can test your corn setting before saving them. Yes, AWS can be helpful at times 😉
Next is to select your target for it to be called by event bridge. Choose Target as Lambda function and then search for your lambda from the dropdown of functions
Click create at the bottom of the page and your event bridge is ready to call lambda function.
Please note that earlier (and even now) you could do this from Cloudwatch but AWS has moved it to the EventBridge service. At the moment, this is what they show you when you go to the Cloudwatch's event
After reading all this, if you are intrigued/excited to receive developer jokes like this then you can follow me on Twitter
Posted on February 16, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.