Skip to content

Commit

Permalink
adding a module for event-bridge-rule
Browse files Browse the repository at this point in the history
  • Loading branch information
parav24 committed Feb 21, 2025
1 parent 3327c6a commit f85737f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
52 changes: 52 additions & 0 deletions modules/aws/event_bridge/event_bridge_rule/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Create EventBridge event-bus - if required
resource "aws_cloudwatch_event_bus" "custom_event_bus" {
count = var.create_event_bus ? 1 : 0
name = var.event_bus_name
}

resource "aws_cloudwatch_event_rule" "event_rule" {
name = var.event_rule_name
description = var.event_rule_description
event_bus_name = var.event_bus_name
event_pattern = jsonencode({
source = var.event_sources
})
depends_on = [aws_cloudwatch_event_bus.custom_event_bus]
}

resource "aws_cloudwatch_event_target" "event_target" {
rule = aws_cloudwatch_event_rule.event_rule.name
event_bus_name = var.event_bus_name
arn = var.target_arn
role_arn = aws_iam_role.eventbridge_role.arn
}

resource "aws_iam_role" "eventbridge_role" {
name = var.role_name

assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Effect = "Allow"
Principal = { Service = "events.amazonaws.com" }
Action = "sts:AssumeRole"
}
]
})
}

resource "aws_iam_role_policy" "eventbridge_policy" {
role = aws_iam_role.eventbridge_role.id

policy = jsonencode({
Version = "2012-10-17",
Statement = [
{
Effect = "Allow"
Action = var.role_actions
Resource = var.target_arn
}
]
})
}
20 changes: 20 additions & 0 deletions modules/aws/event_bridge/event_bridge_rule/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
output "event_bus_arn" {
description = "ARN of the created EventBridge Event-Bus"
value = length(aws_cloudwatch_event_bus.custom_event_bus) > 0 ? aws_cloudwatch_event_bus.custom_event_bus[0].arn : null
}

output "event_rule_arn" {
description = "ARN of the created EventBridge rule"
value = aws_cloudwatch_event_rule.event_rule.arn
}

output "event_target_arn" {
description = "ARN of the EventBridge target"
value = aws_cloudwatch_event_target.event_target.arn
}

output "iam_role_arn" {
description = "ARN of the IAM role used by EventBridge"
value = aws_iam_role.eventbridge_role.arn
}

39 changes: 39 additions & 0 deletions modules/aws/event_bridge/event_bridge_rule/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
variable "create_event_bus" {
type = bool
description = "whether to create event-bus or not: true or false"
}

variable "event_rule_name" {
description = "Name of the EventBridge rule"
type = string
}

variable "event_rule_description" {
description = "Description of the EventBridge rule"
type = string
}

variable "event_bus_name" {
description = "Event bus name where rule is created"
type = string
}

variable "event_sources" {
description = "Event sources to match in the event pattern"
type = list(string)
}

variable "target_arn" {
description = "ARN of the target for the event rule"
type = string
}

variable "role_name" {
description = "IAM Role name for EventBridge"
type = string
}

variable "role_actions" {
description = "List of actions the IAM Role should allow"
type = list(string)
}

0 comments on commit f85737f

Please sign in to comment.