How do I configure EventBridge rules to send messages to SQS?
Let’s configure AWS EventBridge (formerly CloudWatch Events) rules to send messages to an SQS queue. This involves setting up an EventBridge rule to capture specific events and forward them as messages to an SQS queue.
Explanation
- AWS SQS Queue: We’ll create an SQS queue where the messages will be sent.
- AWS IAM Role: We’ll define an IAM role with the necessary permissions for EventBridge to send messages to the SQS queue.
- AWS EventBridge Rule: This rule will capture specific events (e.g., invoking by a schedule or a pattern) and send 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;
Summary
In this example, we created an SQS queue, an IAM role with permissions for EventBridge to send messages to the SQS queue, and an EventBridge rule to capture events and forward them to the SQS queue. The resources were declared, and outputs were provided to show the SQS queue URL and EventBridge rule ARN.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.