Using Amazon Eventbridge Scheduler to build a serverless reminder application

In this post, we will see how to use AWS Eventbridge Scheduler. This service is similar to Eventbridge rules that run at schedule but it provides better customization and improved scalability.In addition to recurring tasks, Eventbridge Scheduler has th…


This content originally appeared on Level Up Coding - Medium and was authored by Zied Ben Tahar

In this post, we will see how to use AWS Eventbridge Scheduler. This service is similar to Eventbridge rules that run at schedule but it provides better customization and improved scalability.

In addition to recurring tasks, Eventbridge Scheduler has the ability to plan single tasks in the future by configuring a “one-time schedule”. In this article, we will mainly focus on this feature.

As a use case, we will build a serverless application where we can define reminders at a specified time in the future and receive them by SMS.

TL;DR

You can find the application repository with the complete github action CI/CD workflow here 👉https://github.com/ziedbentahar/aws-eventbridge-scheduler-sample

What are we going to build ?

reference architecture for “Awesome reminder”
  • The Api Gateway exposes a route integrated with “Register Reminder ” lambda. This route handles requests from a client: A reminder payload containing: A message, a due time and a message.
  • Register Reminder lambda is responsible for scheduling the reminders by creating a “one-time schedule” on the Eventbridge scheduler. We will define for each input sent to the scheduler a Target and a Target Role that allows the event bridge to write to that target. On our case we will define an SQS queue “Reminders Queue” as a target.
  • At due time, the Eventbridge Scheduler sends the reminders to the Reminders Queue. We configure this queue to trigger “Send Reminder” lambda.
  • Send Reminder publishes the reminder as an SMS by using the SNS Mobile text messaging feature. When a reminder is successfully published to SNS, this lambda function removes the processed reminder from the Eventbridge Scheduler.

On this example will be using node 16 runtime and typescript for Lambda code. We will also deploy the infrastructure using CloudFormation.

⚠️Note: By default, when working with SNS text messages, an AWS account will be in a sandbox mode. There are some limitations: Sending messages to only verified destination phone number, limited number of verified destination. On this article, I won’t detail the configuration required to enable sending SMS to any phone number. You can find here a nice guide on this topic.

Let’s see the code

1- The scheduler group

By default, Eventbridge scheduler will create schedules on a “default” group. We can define a custom scheduler group with a custom name where reminders tasks will be created. Here is the CloudFormation resource that creates a scheduler group:

Once created, the group appears in the “Schedule Groups” panel of the Eventbridge Scheduler console

2- Creating the execution role

An important step: We will need to setup an execution role that EventBridge Scheduler assumes, we will attach access policies to the role to provide EventBridge Scheduler access to invoke targets: In our case send messages to the “Reminders queue” SQS target.

You will find the complete template of this role here

3- Creating the “Register reminder” lambda

This lambda creates a new schedule input for a given reminder by calling scheduleReminder. Each reminder gets assigned an Id.

We use this Id to identity the schedule input on the Eventbridge Scheduler.

scheduleReminder uses the AWS SDK to create a new scheduled task on the Scheduler:

The relevant parts:

  • The target, target role and the scheduler group name are defined as environment variables defined in the lambda resource
  • The Target : It consists of the target input (the payload of our reminder), the Target Arn: “Reminders Queue” and the role arn of the IAM role that EventBridge Scheduler will use for this target when the schedule is invoked.
  • The Schedule input : We will use the Id of the reminder as a name, set the target and set the expression that defines when the schedule runs, here we will use at expression since we are creating a one-time schedule, this expression must have this format `at(yyyy-mm-ddThh:mm:ss). You can find more about the one-time schedule here

This lambda function needs to allow scheduler:CreateSchedule policy as well as allow iam:PassRole for the execution role to be assumed by Eventbridge:

You will find the full CloudFormation template of this lambda here

When reminders are registered they appear as schedules on the Eventbridge Scheduler console. As mentioned earlier, we used reminder Ids as schedule names.

“awesome reminders” schedules where target type is defined as SQS

4- Creating the “send reminder” lambda

This lambda defines the “Reminders queue” as an event source. When invoked, this lambda sends the received reminders by SMS and then deletes the related schedule by reminder Id once the operation is successful.

sendSms function sends the messages using SNS. Here is are details of this function

The “Send Reminder” lambda execution role attaches these policies :

Note: In order to be able to send messages, the send-sms policy must allow sns:Publishbut for resources that do not have an arn which is the case for SNS Text message service.

You will find the complete CloudFormation template of this lambda following this link.

Wrapping up

Eventbridge Scheduler is a good alternative to Eventbridge Scheduled rules or CloudWatch events. Its ad-hoc scheduling capabilities makes it a perfect service for applications that need to plan messages to be processed in the future.

⚠️ Important notes:

  • At the time of writing, you can can configure schedules with a minimum granularity of one minute.
  • This service provides at least once delivery to targets

You can find the complete repo with the github action CI/CD pipeline here 👉https://github.com/ziedbentahar/aws-scheduling-with-event-bridge

Further readings


Using Amazon Eventbridge Scheduler to build a serverless reminder application was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Zied Ben Tahar


Print Share Comment Cite Upload Translate Updates
APA

Zied Ben Tahar | Sciencx (2022-11-16T03:03:06+00:00) Using Amazon Eventbridge Scheduler to build a serverless reminder application. Retrieved from https://www.scien.cx/2022/11/16/using-amazon-eventbridge-scheduler-to-build-a-serverless-reminder-application/

MLA
" » Using Amazon Eventbridge Scheduler to build a serverless reminder application." Zied Ben Tahar | Sciencx - Wednesday November 16, 2022, https://www.scien.cx/2022/11/16/using-amazon-eventbridge-scheduler-to-build-a-serverless-reminder-application/
HARVARD
Zied Ben Tahar | Sciencx Wednesday November 16, 2022 » Using Amazon Eventbridge Scheduler to build a serverless reminder application., viewed ,<https://www.scien.cx/2022/11/16/using-amazon-eventbridge-scheduler-to-build-a-serverless-reminder-application/>
VANCOUVER
Zied Ben Tahar | Sciencx - » Using Amazon Eventbridge Scheduler to build a serverless reminder application. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/11/16/using-amazon-eventbridge-scheduler-to-build-a-serverless-reminder-application/
CHICAGO
" » Using Amazon Eventbridge Scheduler to build a serverless reminder application." Zied Ben Tahar | Sciencx - Accessed . https://www.scien.cx/2022/11/16/using-amazon-eventbridge-scheduler-to-build-a-serverless-reminder-application/
IEEE
" » Using Amazon Eventbridge Scheduler to build a serverless reminder application." Zied Ben Tahar | Sciencx [Online]. Available: https://www.scien.cx/2022/11/16/using-amazon-eventbridge-scheduler-to-build-a-serverless-reminder-application/. [Accessed: ]
rf:citation
» Using Amazon Eventbridge Scheduler to build a serverless reminder application | Zied Ben Tahar | Sciencx | https://www.scien.cx/2022/11/16/using-amazon-eventbridge-scheduler-to-build-a-serverless-reminder-application/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.