Reputation: 185
i am creating 4 target groups and 4 nlb , count is based on number of screenrecorders (01-04). Also 3 ASG one for each AZ. I want to add 4 target groups to each ASG. for example : fw_asg_az1 should have 4 target groups attached, same way for fw_asg_az2 and fw_asg_az3 same target groups needs to be attached.
in future if i add server05 in my variable and re run terraform, newly created target group should be added to all three ASG's
variables in my main.tf
screenrecorders = [
"server01",
"server02",
"server03",
"server04"
]
variable.tf
variable "screenrecorders" {
description = "List of screen recorders"
type = list(string)
}
main.tf
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 i am getting
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]
i tried below as suggested in the error, still didn't work
aws_lb_target_group.tg_scr_vr[count.index]
i am new to terraform, still learning, any help from stackoverflow community is greatly appreciated.
Upvotes: 1
Views: 1277
Reputation: 238111
Instead of
target_group_arns = ["${aws_lb_target_group.tg_scr_vr.arn}"]
it should be :
target_group_arns = aws_lb_target_group.tg_scr_vr[*].arn
if you want to associate all four TFs with a single ASG.
Upvotes: 1