0
votes

I am creating multiple resources using count in terraform. for ex:

resource "aws_subnet" "rSubnetMGMT" {
  count = length(var.vletter)
  availability_zone = "${var.vRegion}${var.vletter[count.index]}"
  vpc_id     = aws_vpc.rVPCMGMT.id
  cidr_block = var.vSubnetMGMTCIDR
  map_public_ip_on_launch = "true"

  tags = {
    Name = "${var.vWorkloadShortCode}-mgmt-MGMT- ${var.vRegion}${var.vletter[count.index]}"
    SubnetType = "MGMT"

  }
}

here vletter = ["a","b","c"] so this creates 3 subnets per availability zone now I want to create an "aws_autoscaling_group" which requires a list of vpc_zone_identifier i.e. the subnet ids that I created

resource "aws_autoscaling_group" "rAutoScalingGroup" {
  count = lenght(var.vletter)
  max_size                  = var.vMaxNoofInstances
  min_size                  = var.vMinNoofInstances
  launch_configuration      = aws_launch_configuration.rLaunchConfiguration.name
  vpc_zone_identifier       = [aws_subnet.rSubnetMGMT[count.index].id ]
  metrics_granularity       = 1Minute
  enabled_metrics           = [GroupInServiceInstances]
  load_balancers = aws_elb.rLoadBalancer.name

  tag {
    Name                 = "${var.vInstanceShortNameBAS}-asg"
  }
}

but if I use count while creating the resource(aws_autoscaling_group) it will create multiple resources i.e. 3 different autoscaling groups for each subnet, but here I only want all the subnet ids that I created earlier. How can I achieve this without creating multiple resources?

1

1 Answers

2
votes

You can use the splat operator: aws_subnet.rSubnetMGMT.*.id which will resolve to be the list of IDs of the subnets that were created.