如何使用terraform在输出中显示所有的alb-tg资源



My main.tf有以下代码:

resource "aws_lb_target_group" "dev-ext-test-msvc1-pub" {
name                 = "dev-ext-test-msvc1-pub"
port                 = "14003"
protocol             = "HTTP"
target_type          = "instance"
vpc_id               = data.aws_vpc.vpc.id
protocol_version     = "HTTP1"
deregistration_delay = 10
health_check {
enabled             = true
interval            = 10
path                = "/health"
port                = "32767"
healthy_threshold   = 2
unhealthy_threshold = 2
timeout             = 2
protocol            = "HTTP"
matcher             = "200"
}
tags = {
environment = "dev"
project     = "ext test 2"
Name        = "dev-ext-test-msvc1-pub"
}
}
resource "aws_lb_target_group" "dev-ext-test-msvc2-pub" {
name                 = "dev-ext-test-msvc2-pub"
port                 = "14004"
protocol             = "HTTP"
target_type          = "instance"
vpc_id               = data.aws_vpc.vpc.id
protocol_version     = "HTTP1"
deregistration_delay = 10
health_check {
enabled             = true
interval            = 10
path                = "/health"
port                = "32767"
healthy_threshold   = 2
unhealthy_threshold = 2
timeout             = 2
protocol            = "HTTP"
matcher             = "200"
}
tags = {
environment = "dev"
project     = "ext test 2"
Name        = "dev-ext-test-msvc2-pub"
}
}
resource "aws_lb_target_group" "dev-ext-test-msvc5-pub" {
name                 = "dev-ext-test-msvc5-pub"
port                 = "14002"
protocol             = "HTTP"
target_type          = "instance"
vpc_id               = data.aws_vpc.vpc.id
protocol_version     = "HTTP1"
deregistration_delay = 10
health_check {
enabled             = true
interval            = 10
path                = "/health"
port                = "32767"
healthy_threshold   = 2
unhealthy_threshold = 2
timeout             = 2
protocol            = "HTTP"
matcher             = "200"
}
tags = {
environment = "dev"
project     = "ext test 2"
Name        = "dev-ext-test-msvc5-pub"
}
}

resource "aws_lb_target_group" "dev-ext-test-msvc4-pub" {
name                 = "dev-ext-test-msvc4-pub"
port                 = "14001"
protocol             = "HTTP"
target_type          = "instance"
vpc_id               = data.aws_vpc.vpc.id
protocol_version     = "HTTP1"
deregistration_delay = 10
health_check {
enabled             = true
interval            = 10
path                = "/health"
port                = "32767"
healthy_threshold   = 2
unhealthy_threshold = 2
timeout             = 2
protocol            = "HTTP"
matcher             = "200"
}
tags = {
environment = "dev"
project     = "ext test 2"
Name        = "dev-ext-test-msvc4-pub"
}
}

输出.tf为:

output "lb_dns" {
value = module.dev-ext-test-pub-2-alb.lb_dns_name
}
output "lb_security_group" {
value = module.dev-ext-test-pub-2-alb-sg.security_group_id
}
output "lb_target_groups" {
value = aws_lb_target_group.*.name
}
output "lb_target_groups_arns" {
value = aws_lb_target_group.*.arn
}
output "worker_ami" {
value = data.aws_ami.k8s_msvcs_custom_ami.id
}
output "worker_security_group" {
value = data.aws_security_group.worker_k8s_sg.id
}
output "worker_subnet_id" {
value = data.aws_subnet.worker-subnet.id
}

但是aws_lb_target_group.*.name无法获取所有目标组。尽管我可以这样做来获取值:

output "lb_target_groups" {
value = [aws_lb_target_group.dev-ext-test-msvc1-pub.name, aws_lb_target_group.dev-ext-test-msvc4-pub.name,]
}
output "lb_target_groups_arns" {
value = [aws_lb_target_group.dev-ext-test-msvc1-pub.arn, aws_lb_target_group.dev-ext-test-msvc4-pub.arn,]
}

但是,有没有更有效的方法来获取所有目标群体?

但是有更有效的方法来获取所有目标组吗?

遗憾的是,由于您的代码设计,没有,因为您正在定义完全不同的aws_lb_target_group资源。

但是,如果您要重新组织代码,使其仅使用单个块aws_lb_target_groupcountfor_each,那么使用splet表达式获取所有名称就不会有任何问题。

例如:

variable "tgs" {
default = {
"dev-ext-test-msvc1-pub" = {
port = 14003
},
"dev-ext-test-msvc2-pub" = {
port = 14004
}
# the rest
}
}

resource "aws_lb_target_group" "tgs" {
for_each             = var.tgs
name                 = each.key
port                 = each.value.port
protocol             = "HTTP"
target_type          = "instance"
vpc_id               = data.aws_vpc.vpc.id
protocol_version     = "HTTP1"
deregistration_delay = 10
health_check {
enabled             = true
interval            = 10
path                = "/health"
port                = "32767"
healthy_threshold   = 2
unhealthy_threshold = 2
timeout             = 2
protocol            = "HTTP"
matcher             = "200"
}
tags = {
environment = "dev"
project     = "ext test 2"
Name        = each.key
}
}

然后

output "lb_target_groups" {
value = values(aws_lb_target_group.tgs)[*].name
}

由于您不是通过循环创建tg,因此无法在尝试时索引或使用splat。

您有两种选择,用tg数据创建一个变量类型的对象,并对该变量进行迭代,然后使用splat或创建一个具有所需属性的本地对象,然后对其进行迭代,类似于

locals {
all_tg = [ resource.aws_lb_target_group.dev-ext-test-msvc1-pub,
resource.aws_lb_target_group.dev-ext-test-msvc2-pub,
resource.aws_lb_target_group.dev-ext-test-msvc5-pub,
resource.aws_lb_target_group.dev-ext-test-msvc4-pub
]
output "all_tg_names" {
value = [ for tg in tolist(local.all_tg) : tg.name ]
} 

这里有一个例子:

terraform {
}
provider "random" {}
resource "random_pet" "a" {}
resource "random_pet" "b" {}
resource "random_pet" "c" {}
locals {
pet = [ resource.random_pet.a,
resource.random_pet.b,
resource.random_pet.c
]
}
output "name" {
value = [for pet in tolist(local.pet) : pet.id]
}

然后,在应用后,它将产生类似于的东西

Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
Outputs:
name = [
"mint-reptile",
"uncommon-coyote",
"in-mutt",
]

当然,如果你重新思考构建相同资源的方式,这个解决方案可能会更好。

我希望它能有所帮助。

相关内容

  • 没有找到相关文章

最新更新