如何在terraform中组合2个局部变量?
我想要组合2个局部变量,因为我需要将信息传递给负载平衡器目标组附件资源如何在terraform中组合2个局部变量?,terraform,terraform-provider-aws,Terraform,Terraform Provider Aws,我想要组合2个局部变量,因为我需要将信息传递给负载平衡器目标组附件资源 locals { test-svc = {for idx,svc in local.merged_lbport_svc : "${svc.service}-${svc.port}" => svc ...} } locals { service_instance_map = merge([for env, value in data.aws_instances.ecom-instan
locals {
test-svc = {for idx,svc in local.merged_lbport_svc : "${svc.service}-${svc.port}" => svc ...}
}
locals {
service_instance_map = merge([for env, value in data.aws_instances.ecom-instances:
{
for id in value.ids:
"${env}-${id}" => {
"service" = env
"id" = id
}
}
]...)
}
2个局部变量的输出如下所示
locals-service-instance-map = {
"3ds-i-038ff8f67582ef64d" = {
"id" = "i-038ff8f67582ef64d"
"service" = "test"
}
"3ds-i-095f20e434879e241" = {
"id" = "i-095f20e434879e241"
"service" = "test"
}
"3ds-i-0d5692eacef255831" = {
"id" = "i-0d5692eacef255831"
"service" = "test"
}
"jsclient-i-0b30e97ca40f4359e" = {
"id" = "i-0b30e97ca40f4359e"
"service" = "jsc"
}
"jsclient-i-0e5fe6123c371d75d" = {
"id" = "i-0e5fe6123c371d75d"
"service" = "jsc"
}
"jsclient-i-0f108c650773bbc08" = {
"id" = "i-0f108c650773bbc08"
"service" = "jsc"
}
"validation-i-00d70dcd4fc72fff4" = {
"id" = "i-00d70dcd4fc72fff4"
"service" = "valid"
}
"validation-i-05cc0d238006cfe56" = {
"id" = "i-05cc0d238006cfe56"
"service" = "valid"
}
"validation-i-0ad6c30d56b0be26b" = {
"id" = "i-0ad6c30d56b0be26b"
"service" = "valid"
}
}
locals-test-svc = {
"test-443" = [
{
"port" = 443
"protocol" = "TCP"
"service" = "test"
},
]
"test-80" = [
{
"port" = 80
"protocol" = "TCP"
"service" = "test"
},
]
"jsc-443" = [
{
"port" = 443
"protocol" = "TCP"
"service" = "jsc"
},
]
"jsc-80" = [
{
"port" = 80
"protocol" = "TCP"
"service" = "jsc"
},
]
"valid-443" = [
{
"port" = 443
"protocol" = "TCP"
"service" = "valid"
},
]
"valid-80" = [
{
"port" = 80
"protocol" = "TCP"
"service" = "valid"
},
]
}
现在我想把这两个局部变量和我的结果变量组合起来,如下所示
"test-i-038ff8f67582ef64d" = {
"id" = "i-038ff8f67582ef64d"
"service" = "test"
"port" = 80
"protocol" = "TCP"
}
"test-i-095f20e434879e241" = {
"id" = "i-095f20e434879e241"
"service" = "test"
"port" = 80
"protocol" = "TCP"
}
"test-i-0d5692eacef255831" = {
"id" = "i-0d5692eacef255831"
"service" = "test"
"port" = 80
"protocol" = "TCP"
}
"jsc-i-0b30e97ca40f4359e" = {
"id" = "i-0b30e97ca40f4359e"
"service" = "jsc"
"port" = 80
"protocol" = "TCP"
}
"jsc-i-0e5fe6123c371d75d" = {
"id" = "i-0e5fe6123c371d75d"
"service" = "jsc"
"port" = 80
"protocol" = "TCP"
}
"jsc-i-0f108c650773bbc08" = {
"id" = "i-0f108c650773bbc08"
"service" = "jsc"
"port" = 80
"protocol" = "TCP"
}
"valid-i-00d70dcd4fc72fff4" = {
"id" = "i-00d70dcd4fc72fff4"
"service" = "valid"
"port" = 80
"protocol" = "TCP"
}
"valid-i-05cc0d238006cfe56" = {
"id" = "i-05cc0d238006cfe56"
"service" = "valid"
"port" = 80
"protocol" = "TCP"
}
"valid-i-0ad6c30d56b0be26b" = {
"id" = "i-0ad6c30d56b0be26b"
"service" = "valid"
"port" = 80
"protocol" = "TCP"
}
}
端口443和协议TCP也需要相同的组合。然后,我将把它传递给每个循环中的loadbalancer目标组附件资源
resource "aws_lb_target_group_attachment" "ecom-tga" {
for_each = local.service_instance_map
target_group_arn = aws_lb_target_group.ecom-nlb-tgp["${each.value.service}-${each.value.port}"].arn
port = each.value.port
target_id = each.value.id
depends_on = [aws_lb_target_group.ecom-nlb-tgp]
}
现在,我正在对端口值进行如下编码
resource "aws_lb_target_group_attachment" "ecom-tga80" {
for_each = local.service_instance_map
#for_each = null_resource.maps
target_group_arn = aws_lb_target_group.ecom-nlb-tgp["${each.value.service-name}-80"].arn
#port = aws_lb_target_group.ecom-nlb-tgp[each.value.service-name["port"]]
target_id = each.value.id
# depends_on = [
# aws_instance.ecom-validation-service
# ]
depends_on = [aws_lb_target_group.ecom-nlb-tgp]
}
resource "aws_lb_target_group_attachment" "ecom-tga443" {
for_each = local.service_instance_map
#for_each = null_resource.maps
target_group_arn = aws_lb_target_group.ecom-nlb-tgp["${each.value.service-name}-443-TCP"].arn
#port = aws_lb_target_group.ecom-nlb-tgp[each.value.service-name["port"]]
target_id = each.value.id
# depends_on = [
# aws_instance.ecom-validation-service
# ]
depends_on = [aws_lb_target_group.ecom-nlb-tgp]
}
即使我们有其他更好的想法,请根据什么标准向我推荐Marge
locals服务实例映射
和test svc
没有任何公用键。@Marcin-我们在两个本地都有一个名为-service的公用键。这些数据结构非常复杂,输出不清楚。为什么它只包含端口80?那么443端口被忽略了?到处都有端口80。我不知道为什么您不能将其添加为“已修复”。@Marcin 443没有被分解,我们也必须对端口443进行相同的循环。我只显示了端口80的示例