Resource: aws_cloudwatch_event_rule

Provides an EventBridge Rule resource.

Example Usage

resource "aws_cloudwatch_event_rule" "console" {
  name        = "capture-aws-sign-in"
  description = "Capture each AWS Console Sign In"

  event_pattern = jsonencode({
    detail-type = [
      "AWS Console Sign In via CloudTrail"
    ]
  })
}

resource "aws_cloudwatch_event_target" "sns" {
  rule      = aws_cloudwatch_event_rule.console.name
  target_id = "SendToSNS"
  arn       = aws_sns_topic.aws_logins.arn
}

resource "aws_sns_topic" "aws_logins" {
  name = "aws-console-logins"
}

resource "aws_sns_topic_policy" "default" {
  arn    = aws_sns_topic.aws_logins.arn
  policy = data.aws_iam_policy_document.sns_topic_policy.json
}

data "aws_iam_policy_document" "sns_topic_policy" {
  statement {
    effect  = "Allow"
    actions = ["SNS:Publish"]

    principals {
      type        = "Service"
      identifiers = ["events.amazonaws.com"]
    }

    resources = [aws_sns_topic.aws_logins.arn]
  }
}

Argument Reference

This resource supports the following arguments:

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Import

In Terraform v1.5.0 and later, use an import block to import EventBridge Rules using the event_bus_name/rule_name (if you omit event_bus_name, the default event bus will be used). For example:

import {
  to = aws_cloudwatch_event_rule.console
  id = "example-event-bus/capture-console-sign-in"
}

Using terraform import, import EventBridge Rules using the event_bus_name/rule_name (if you omit event_bus_name, the default event bus will be used). For example:

% terraform import aws_cloudwatch_event_rule.console example-event-bus/capture-console-sign-in