我正在创建 4 个目标组和 4 个 nlb,计数基于屏幕记录器的数量 (01-04)。每个 AZ 也有 3 个 ASG。我想为每个 ASG 添加 4 个目标组。例如:fw_asg_az1 应该附加 4 个目标组,fw_asg_az2 和 fw_asg_az3 的方法相同,需要附加相同的目标组。
将来,如果我在变量中添加 server05 并重新运行 terraform,则应将新创建的目标组添加到所有三个 ASG 中
我的 main.tf 中的变量
screenrecorders = [
"server01",
"server02",
"server03",
"server04"
]
变量.tf
variable "screenrecorders" {
description = "List of screen recorders"
type = list(string)
}
主文件
resource "aws_autoscaling_group" "fw_asg_az1" {
name = "${var.deployment_prefix}-asg-${var.az1}"
availability_zones = [var.az1]
health_check_grace_period = 300
health_check_type = "ELB"
default_cooldown = 600
launch_template {
id = aws_launch_template.fw_launch_template_az1.id
version = "$Latest"
}
min_size = 1
max_size = 5
tag {
key = "Name"
value = "${var.deployment_prefix}-${var.az1}"
propagate_at_launch = true
}
target_group_arns = ["${aws_lb_target_group.tg_scr_vr.arn}"]
depends_on = [aws_lb_target_group.tg_scr_vr]
}
resource "aws_autoscaling_group" "fw_asg_az2" {
name = "${var.deployment_prefix}-asg-${var.az2}"
availability_zones = [var.az2]
health_check_grace_period = 300
health_check_type = "ELB"
default_cooldown = 600
launch_template {
id = aws_launch_template.fw_launch_template_az2.id
version = "$Latest"
}
min_size = 1
max_size = 5
tag {
key = "Name"
value = "${var.deployment_prefix}-${var.az2}"
propagate_at_launch = true
}
target_group_arns = ["${aws_lb_target_group.tg_scr_vr.arn}"]
depends_on = [aws_lb_target_group.tg_scr_vr]
}
resource "aws_autoscaling_group" "fw_asg_az3" {
name = "${var.deployment_prefix}-asg-${var.az3}"
availability_zones = [var.az3]
#availability_zones = [data.aws_availability_zones.all.names]
health_check_grace_period = 300
health_check_type = "ELB"
default_cooldown = 600
launch_template {
id = aws_launch_template.fw_launch_template_az3.id
version = "$Latest"
}
min_size = 1
max_size = 5
tag {
key = "Name"
value = "${var.deployment_prefix}-${var.az3}"
propagate_at_launch = true
}
target_group_arns = ["${aws_lb_target_group.tg_scr_vr.arn}"]
depends_on = [aws_lb_target_group.tg_scr_vr]
}
###############################################################################
# ELB Target Groups
###############################################################################
resource "aws_lb_target_group" "tg_scr_vr" {
count = length(var.screenrecorders)
name = "tg-${var.screenrecorders[count.index]}"
target_type = "instance"
protocol = "TCP"
port = "80"
vpc_id = var.vpc_id
health_check {
protocol = "TCP"
port = "traffic-port"
}
}
###############################################################################
# ELB
###############################################################################
resource "aws_lb" "lb_scr_vr" {
count = length(var.screenrecorders)
name = "lb-${var.screenrecorders[count.index]}"
load_balancer_type = "network"
internal = false
ip_address_type = "ipv4"
subnet_mapping {
subnet_id = var.az1_fw_public_subnet_id
}
subnet_mapping {
subnet_id = var.az2_fw_public_subnet_id
}
subnet_mapping {
subnet_id = var.az3_fw_public_subnet_id
}
}
我得到的错误
Error: Missing resource instance key
on .terraform/modules/fw/main.tf line 147, in resource "aws_autoscaling_group" "fw_asg_az1":
147: target_group_arns = ["${aws_lb_target_group.tg_scr_vr.arn}"]
Because aws_lb_target_group.tg_scr_vr has "count" set, its attributes must
be accessed on specific instances.
For example, to correlate with indices of a referring resource, use:
aws_lb_target_group.tg_scr_vr[count.index]
我按照错误中的建议尝试了以下操作,但仍然无效
aws_lb_target_group.tg_scr_vr[count.index]
我是 terraform 的新手,仍在学习,非常感谢来自 stackoverflow 社区的任何帮助。
代替
target_group_arns = ["${aws_lb_target_group.tg_scr_vr.arn}"]
它应该是 :
target_group_arns = aws_lb_target_group.tg_scr_vr[*].arn
如果您想将所有四个 TF 与单个 ASG 相关联。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句