How to use AWS EventBridge to schedule tasks
Tags: aws, lambda, cron, serverless
Published: 2022-02-16 16:17:07


![AWS Event Bridge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z0xbwqkzrsaoeqe823dw.png)

In my last [blog](https://dev.to/metacollective/twitter-bot-to-send-joke-and-image-lambda-typescript-4cj5), I wrote about how you can create your own [Twitter bot](https://dev.to/metacollective/twitter-bot-to-send-joke-and-image-lambda-typescript-4cj5) 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.


![AWS EventBridge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/taoqxnyf0rib6rz5388a.png)

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 -


![AWS EventBridge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gxv439blb6v24po2vfod.png)

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


![AWS EventBridge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vpf2ucyg3v0fvegeqm2i.png)

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 😉


![AWS EventBridge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gkg3xvhq798ijof5yycw.png)

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


![AWS EventBridge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zfzpi25lyg4r7ch9lirj.png)

Click create at the bottom of the page and your event bridge is ready to call lambda function.


![AWS EventBridge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0rkoz4qahe65z1p0e4oc.png)

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


![AWS EventBridge](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5bdxf4850zyxosap6dep.png)

After reading all this, if you are intrigued/excited to receive developer jokes like this then you can follow me on [Twitter](https://twitter.com/metacollective9/)

{%twitter 1493248927361032198 %}