Terraform 创建具有多个目标的 Cloudwatch 规则



来自 Terraform 文档 - https://www.terraform.io/docs/providers/aws/r/cloudwatch_event_target.html

我没有看到将多个目标映射到同一 Cloudwatch 规则的选项。它只需要一个接受一个资源的arn字段。我正在尝试将 5 个 Lambda 映射到相同的 Cloudwatch 规则。Terraform是否支持此功能?

编辑:如何仅附加 5 个 lambda?如果我创建了 15 个 lambda,我想将每个 5 个附加到 3 个 cloudwatch 规则。

让它工作了!当我将目标分配给规则时,我必须将规则计数除以 5。大致是这样的:

resource "aws_cloudwatch_event_rule" "arule" {
  count       = "${ceil(length(var.lambda_arns) / 5.0)}"  // Needs to be 5.0 to force float computation
  name        = "${var.rule_name}${format("-%d", count.index)}"
  is_enabled  = true
}
resource "aws_cloudwatch_event_target" "atarget" {
  depends_on = ["aws_cloudwatch_event_rule.arule"]
  count = "${length(var.lambda_arns)}"
  rule  = "${aws_cloudwatch_event_rule.arule.*.name[count.index / 5]}"
  arn   = "${var.lambda_arns[count.index]}"
}

我根据 lambda 的数量创建了事件规则(即,如果有 10 个 lambda,则创建 2 个规则(。

我根据 lambda 的数量创建了目标(即,如果有 10 个 lambda,则创建 10 个目标(。

我通过将count.index除以 5(与用于确定规则计数的逻辑相同(在规则之间按比例分配目标。

假设您使用具有计数的同一地形资源创建了所有 lamba,您也可以对此使用 count:

resource "aws_cloudwatch_event_target" "cw_target" {
    count = length(aws_lambda_function.my_lambdas)
    rule = "${aws_cloudwatch_event_rule.my_rule.name}"
    arn = "${aws_lambda_function.my_lambdas.*.arn[count.index]}"
}

这是我所做的。我忽略了资源"aws_cloudwatch_event_target"(非常重要(的"target_id",并使用局部变量(定义本地变量,此示例:"目标"(,并循环本地变量,并创建多个aws_cloudwatch_event_target和多个评估模板。

locals {
  stack_name_prefix = "Inspector"
  rules_package_arn_cis = "arn:aws:inspector:ap-southeast-2:454640832652:rulespackage/0-Vkd2Vxjq"
  default_target = {
    rules : [local.rules_package_arn_cis],
    duration : 3600
  }
  targets = [
    merge(local.default_target, {
      name : "data_indexer",
      tags : {
        Function = "LR - DX"
      },
    }),
    merge(local.default_target, {
      name : "ai_engine",
      tags : {
        Function = "LR - AIE"
      },
    }),
    merge(local.default_target, {
      name : "data_processor",
      tags : {
        Function = "LR - Data Processor"
      },
    }),
    merge(local.default_target, {
      name : "platform_manager",
      tags : {
        Function = "LR - PM"
      },
    })
  ]
}
resource "aws_inspector_assessment_template" "assessment_template" {
  count              = length(local.targets)
  name               = "${local.stack_name_prefix}_${local.targets[count.index]["name"]}_assessment_template"
  target_arn         = aws_inspector_assessment_target.assessment[count.index].arn
  duration           = local.default_target.duration
  rules_package_arns = [local.rules_package_arn_cis]
}
resource "aws_cloudwatch_event_target" "event_target_for_inspector_assessment_template" {
  count = length(local.targets)
  rule  = aws_cloudwatch_event_rule.event_rule_for_inspector_assessment_template.name
  //  target_id = "amazon_inspector_assessment" ## Don't USE target_id, it will mess up the cloudwatch event target, and only generated one target instead of 4
  arn      = aws_inspector_assessment_template.assessment_template[count.index].arn
  role_arn = aws_iam_role.inspector_assessment_template.arn
}
    module "eventbridgetarget" {
      for_each  = var.rule_and_target_details
      source    = "git::ssh://git@bitbucket.int.ally.com/tf/terraform-modules-aws-eventbridge.git//modules/target?ref=v1"
      rule_arn  = module.eventbridgerule.rule.arn
      name      = each.value.name
      namespace = module.namespace.lower_short_name
      tags      = module.namespace.tags
      #arn = module.lambda.arn
      arn = each.value.arn
    }
Now in the tfvars pass the value like below:
rule_and_target_details = {
  "firsttarget" = {
    name = "getentities"
    arn = "arn:aws:execute-api:us-east-1:2XXXXXXX:92mchkioeh/api/GET/getEntities"
  }
  "secondtarget" = {
    name = "getactivitylog"
    arn = "arn:aws:execute-api:us-east-1:2XXXXXX:92mchkioeh/api/GET/getActivityLog"
  }
  "thirdtarget" = {
    name = "searchactivitylog"
    arn = "arn:aws:execute-api:us-east-1:XXXXXX:92mchkioeh/api/GET/searchActivityLog"
  }
}

相关内容

  • 没有找到相关文章

最新更新