【发布时间】:2021-07-29 11:23:51
【问题描述】:
我有 3 个名为 valid、test、jsc 的服务,每个都有 3 个 ec2 实例,每个都有 1 个负载均衡器目标组。现在我想将每个 3 个相应的 ec2 实例附加到它们的目标组
variable "service-names" {
type = list
default = ["valid","jsc","test"]
}
locals {
helper_map = {for idx, val in setproduct(var.service-names, range(var.instance_count)):
idx => {service_name = val[0]}
}
}
resource "aws_lb_target_group" "ecom-tgp" {
for_each = toset(var.service-names)
name = "${each.value}-tgp"
port = 80
protocol = "TCP"
vpc_id = aws_vpc.ecom-vpc.id
target_type = "instance"
deregistration_delay = 90
health_check {
interval = 30
port = 80
protocol = "TCP"
healthy_threshold = 3
unhealthy_threshold = 3
}
tags = {
"Name" = "${each.value}-tgp"
}
}
output "ecom-instance-details" {
value = data.aws_instances.ecom-instances
}
以下是来自 terraform 输出的示例 i 服务实例详细信息
ecom-instance-details = {
"test" = {
"filter" = toset(null) /* of object */
"id" = "ap-south-1"
"ids" = tolist([
"i-0fab28125d684f9d2",
"i-0b90e3501715681df",
"i-066bff51352660006",
])
"instance_state_names" = toset([
"running",
"stopped",
])
"instance_tags" = tomap({
"Name" = "test-service"
})
"private_ips" = tolist([
"10.0.3.11",
"10.0.3.10",
"10.0.3.8",
])
"public_ips" = tolist([])
}
vpc-id = "vpc-051198b7ebaa3bd53"
我正在尝试如下。但由于它在一个列表中有 3 个 id,它不接受传递给 target_id 这基本上是一个字符串
resource "aws_lb_target_group_attachment" "ecom-tga" {
for_each = local.helper_map
target_group_arn = aws_lb_target_group.ecom-tgp[each.value.service_name].arn
port = 80
target_id = data.aws_instances.ecom-instances[each.value.service_name].ids
}
你能指导我吗
【问题讨论】:
-
当前代码有什么问题?有什么错误吗?
-
错误:属性值类型不正确││在 main.tf 第 500 行,资源“aws_lb_target_group_attachment”“ecom-tga”中:│500:target_id = data.aws_instances.ecom-instances[each.value .service_name].ids │ ├────────────── │ │ data.aws_instances.ecom-instances 是具有 3 个属性的对象
-
因为有 3 个 id 无法传递每个 id。需要一些循环
标签: amazon-web-services terraform