在 terraform 中更改 eventbridge cron 规则名称

Change eventbridge cron rule name in terraform

我有一个用 EventBridge 触发的 lambda。

我的 lambda_function 中有 allowed_triggers:

  allowed_triggers = {
    "RunDaily" = {
      principal  = "events.amazonaws.com"
      source_arn = module.eventbridge.eventbridge_rule_arns["crons"]
    }
  }

我有一个 eventbridge 模块:

module "eventbridge" {
  source = "terraform-aws-modules/eventbridge/aws"
  version = "1.14.0"

  create_bus   = false
  create_role  = false
  create_rules = true

  rules = {
    crons = {
      description         = "deafault"
      schedule_expression = "rate(1 day)"
    }
  }

  targets = {
    crons = [
      {
        arn   = module.lambda_function.lambda_function_arn
        input = jsonencode({ "job" : "crons" })
      }
    ]
  }
}

现在,这很好用,因为规则已创建并正确附加。

但是当我想更改规则的名称及其描述时,terraform 拾音器只会更改描述:

module "eventbridge" {
  ...
  rules = {
    crons = {
      description         = "My custom cron rule"
      schedule_expression = "rate(1 day)"
    }
  }

  targets = {
    crons = [
      {
        name  = "my-custom-cron-rule-name"
        arn   = module.lambda_function.lambda_function_arn
        input = jsonencode({ "job" : "crons" })
      }
    ]
  }
}

计划:

Terraform will perform the following actions:

  # module.eventbridge.aws_cloudwatch_event_rule.this["crons"] will be updated in-place
  ~ resource "aws_cloudwatch_event_rule" "this" {
      ~ description         = "deafault" -> "My custom cron rule"
        id                  = "crons-rule"
        name                = "crons-rule"
        tags                = {
            "Name" = "crons-rule"
        }
        # (5 unchanged attributes hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

问题: 如何更改 eventbridge 规则的 name 属性?

根据模块定义 [1],aws_cloudwatch_event_rule 名称派生自 rules 块的 key 的值,即:

  rules = {
    crons = {
      description         = "My custom cron rule"
      schedule_expression = "rate(1 day)"
    }
  }

根据 GitHub 存储库中的行,名称由以下组成:

locals {
  eventbridge_rules = flatten([
    for index, rule in var.rules :
    merge(rule, {
      "name" = index
      "Name" = "${replace(index, "_", "-")}-rule"
    })
  ])
... # rest of locals goes here
}

如果您查看您的定义和这部分代码,您会发现名称将是 crons-rule,这在 nametags.Name 中都可见参数:

        name                = "crons-rule"
        tags                = {
            "Name" = "crons-rule"
        }

因此,为了更改规则的名称,您必须更改规则块的键,即:

  rules = {
    very-nice-new-crons = { # <----- here is where the change should be made
      description         = "My custom cron rule"
      schedule_expression = "rate(1 day)"
    }
  }

您可以通过查看 [2] 来验证这一点:

resource "aws_cloudwatch_event_rule" "this" {
  for_each = { for k, v in local.eventbridge_rules : v.name => v if var.create && var.create_rules }

  name        = each.value.Name
  ...

  tags = merge(var.tags, {
    Name = each.value.Name
  })
}

编辑:如前所述,更改名称后还需要进行两项更改:

  1. Lambda 函数的 allowed_triggers 现在应该使用新键来引用允许触发它的事件规则。它必须从
  2. 更改
source_arn = module.eventbridge.eventbridge_rule_arns["crons"]

source_arn = module.eventbridge.eventbridge_rule_arns["very-nice-new-crons"]
  1. targets 块中也必须使用相同的名称更改,即 targets 中的 crons 键必须替换为相同的键名在 rules 块中:
  targets = {
    very-nice-new-crons = [
      {
        name  = "my-custom-cron-rule-name"
        arn   = module.lambda_function.lambda_function_arn
        input = jsonencode({ "job" : "crons" })
      }
    ]
  }

[1] https://github.com/terraform-aws-modules/terraform-aws-eventbridge/blob/master/main.tf#L2-L6

[2] https://github.com/terraform-aws-modules/terraform-aws-eventbridge/blob/master/main.tf#L44