terraform - 如何在 terraform 中组合 2 个本地变量?
问题描述
我想组合 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-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"
},
]
}
现在我想将这 2 个本地人和我的结果变量结合起来,如下所示
"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 协议也需要相同的组合。然后我将把它传递给 for_each 循环中的负载均衡器目标组附件资源
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]
}
截至目前,我正在对端口值进行 hartcoding,如下所示
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]
}
即使我们有任何其他更好的想法,请建议我
解决方案
推荐阅读
- javascript - 使函数 CRUD DeletebyID(id) Javascript 按其 id 数而不是按索引
- arm - 在 arm DDT 中提交作业
- javascript - 为什么 JavaScript 属性访问器允许使用包含单个属性名称的任意嵌套数组?
- python - TemplateDoesNotExist 位于 /boards/home.html
- excel - 使用 VBA 向折线图添加标签
- git - github api 在 git bash 中不起作用,但在 cmd 中起作用
- typescript - 这种检查两种类型是否属于同一类型的方法如何工作?
- bash - 尝试在 bash 的 for 循环中使用选择/案例
- c++ - 升级 .sln 文件后,“E225 __clrcall 不允许使用省略号参数的函数”
- winapi - 如何检测鼠标悬停在窗口的非客户端部分?