从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规则。
答案 0 :(得分:0)
假设您使用具有count的同一地形资源创建了所有的lambas,那么您也可以在此上使用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]}"
}
答案 1 :(得分:0)
让它正常工作!在为规则分配目标时,必须将规则的计数除以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(与确定规则数量相同的逻辑)在规则中按比例分配目标。
答案 2 :(得分:0)
这就是我所做的。我忽略资源“ aws_cloudwatch_event_target”的“ target_id”(非常重要),并使用局部变量(定义您的本地变量,此示例:“ targets”),并为本地变量循环,并创建多个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
}