1. Answers
  2. How to Configure AWS EventBridge Rules to Send Messages to SQS

How Do I Configure EventBridge Rules to Send Messages to SQS?

Introduction

AWS EventBridge is a serverless event bus service that makes it easy to connect applications using data from your own applications, integrated software as a service (SaaS) applications, and AWS services. By configuring EventBridge rules, you can route specific events to various AWS services, such as Amazon Simple Queue Service (SQS), which is a fully managed message queuing service. This guide will walk you through the process of setting up an EventBridge rule to send messages to an SQS queue.

Step-by-Step Explanation

  1. Create an SQS Queue: Start by creating an SQS queue where the messages from EventBridge will be sent. This queue will act as the destination for the events captured by the EventBridge rule.

  2. Define an IAM Role: An IAM role is needed to grant EventBridge the necessary permissions to send messages to the SQS queue. This role will have a policy attached that allows the sqs:SendMessage action.

  3. Set Up an EventBridge Rule: The EventBridge rule is configured to capture specific events, such as those triggered by a particular AWS service or matching a certain pattern. The rule will then forward these events to the SQS queue.

Example Program

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create the SQS Queue to receive EventBridge messages
const eventbridgeTargetQueue = new aws.sqs.Queue("eventbridge_target_queue", {name: "eventbridge-target-queue"});
// Create an IAM Role for EventBridge to assume to send messages to SQS
const eventbridgeIamRole = new aws.iam.Role("eventbridge_iam_role", {
    name: "eventbridge-to-sqs-role",
    assumeRolePolicy: JSON.stringify({
        Version: "2012-10-17",
        Statement: [{
            Effect: "Allow",
            Principal: {
                Service: "events.amazonaws.com",
            },
            Action: "sts:AssumeRole",
        }],
    }),
});
// Attach a policy to the IAM role allowing EventBridge to send messages to the SQS Queue
const eventbridgePolicy = new aws.iam.RolePolicy("eventbridge_policy", {
    name: "eventbridge-to-sqs-policy",
    role: eventbridgeIamRole.id,
    policy: pulumi.jsonStringify({
        Version: "2012-10-17",
        Statement: [{
            Effect: "Allow",
            Action: "sqs:SendMessage",
            Resource: eventbridgeTargetQueue.arn,
        }],
    }),
});
// Create an EventBridge rule to capture events and forward them to the SQS Queue
const eventbridgeRule = new aws.cloudwatch.EventRule("eventbridge_rule", {
    name: "eventbridge-rule",
    description: "Rule to send events to SQS queue",
    eventPattern: JSON.stringify({
        source: ["aws.ec2"],
    }),
});
// Add the SQS Queue as a target for the EventBridge rule
const eventbridgeRuleTarget = new aws.cloudwatch.EventTarget("eventbridge_rule_target", {
    rule: eventbridgeRule.name,
    targetId: "sqs-target",
    arn: eventbridgeTargetQueue.arn,
    roleArn: eventbridgeIamRole.arn,
});
export const sqsQueueUrl = eventbridgeTargetQueue.url;
export const eventbridgeRuleArn = eventbridgeRule.arn;

Key Points

  • SQS Queue: Acts as a destination for messages from EventBridge.
  • IAM Role: Grants permissions for EventBridge to interact with the SQS queue.
  • EventBridge Rule: Captures and routes specific events to the SQS queue.

Conclusion

By following these steps, you have successfully configured AWS EventBridge to send messages to an SQS queue. This setup allows you to efficiently route and manage events within your AWS environment, enabling seamless integration between services. The provided TypeScript code demonstrates how to create the necessary resources and establish the connections between them.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up