Creating a Scheduled AWS Event Bridge Rule with Terraform to Send SQS Messages Every 5 Minutes
Why do we do this?
Scheduled tasks, also known as cron jobs, are a powerful tool for automating repetitive tasks and ensuring that data is processed in a timely manner. By scheduling specific tasks to run at specific intervals, you can ensure that time-sensitive tasks are completed on time and automate repetitive processes.
In the context of AWS, scheduled tasks can be used for a wide range of applications, such as data warehousing, real-time data processing, and infrastructure management. For example, you could schedule a task to run every day that exports data from a database to an S3 bucket, or schedule a task that runs every hour to check the status of your EC2 instances.
This guide will walk you through the process of deploying an AWS Event Bridge rule that sends SQS messages every 5 minutes. By the end of this guide, you will have a fully functioning scheduled event that can trigger your own custom logic or trigger other AWS services, and also use Terraform for infrastructure as code.
SQS
With the SQS queue “scheduled-event-q” in place, our application can now listen for notifications from the queue, alerting it when the scheduled trigger time has arrived. This allows for seamless integration between our scheduled tasks and the application, ensuring that the necessary actions are taken at the appropriate time.
resource "aws_sqs_queue" "scheduled-event-q" {
name = "scheduled-event-q"
delay_seconds = 0
max_message_size = 2048
message_retention_seconds = 86400
receive_wait_time_seconds = 10
tags = {
Name = "scheduled-event-q"
Environment = var.environment
}
}
In order to allow AWS Event Bridge to send messages to our “scheduled-event-q” SQS queue, we must create an appropriate policy. This policy will define the permissions for Event Bridge to access the queue and send messages. By the end of this step, we will have the necessary permissions in place for Event Bridge to send messages to our SQS queue, triggering the desired actions within our application.
resource "aws_sqs_queue_policy" "schedule-event-policy" {
queue_url = aws_sqs_queue.scheduled-event-q.id
policy = <<POLICY
{
"Version": "2012-10-17",
"Id": "sqspolicy",
"Statement": [
{
"Sid": "First",
"Effect": "Allow",
"Principal": "*",
"Action": ["sqs:SendMessage", "sqs:ReceiveMessage"],
"Resource": "${aws_sqs_queue.scheduled-event-q.arn}"
}
]
}
POLICY
}
Event Bridge
This step involves the creation of an IAM role that enables AWS Event Bridge to access and interact with other AWS resources.
resource "aws_iam_role" "event_bridge_sqs_role" {
name = "event_bridge_sqs_role"
assume_role_policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Effect" : "Allow",
"Principal" : {
"Service" : "scheduler.amazonaws.com"
},
"Action" : "sts:AssumeRole"
}
]
})
tags = {
Name = "event_bridge_sqs_role"
Environment = var.environment
}
}
Next, we will create an AWS Event Bridge rule that will send messages to our “scheduled-event-q” SQS queue at regular intervals. This Event Bridge rule will be configured to send messages to the queue every 5 minutes, ensuring that the scheduled task is triggered on time. By the end of this step, we will have a fully configured and operational Event Bridge rule, sending messages to our SQS queue on a regular basis.
resource "aws_scheduler_schedule" "every5minutes" {
name = "every5minutes"
group_name = "default"
flexible_time_window {
mode = "OFF"
}
schedule_expression = "rate(5 minutes)"
target {
arn = aws_sqs_queue.scheduled-event-q.arn
role_arn = aws_iam_role.event_bridge_sqs_role.arn
input = jsonencode({
MessageBody = "{\"key\":\"value\"}"
})
}
}
Conclusion
In conclusion, scheduled tasks, such as AWS Event Bridge rules, are essential for automating repetitive tasks and ensuring that data is processed in a timely manner. By deploying an AWS Event Bridge rule that sends SQS messages every 5 minutes, you can simplify and streamline your data processing workflows. This article provided a step-by-step guide for creating this rule using Terraform, from creating an IAM role to defining the scheduled events. With the successful implementation of this rule, you can take advantage of the benefits that come with automated scheduling and efficiently manage your AWS environment.
You can check the GitHub repository.
Disclaimer: Please note that I am not responsible for any AWS charges or problems that may arise from the usage of this solution in a production environment. However, feel free to reach out to me with further questions.