简体   繁体   中英

How to get multiple values in a list without using count in terraform?

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 ie 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 ie 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?

您可以使用 splat 运算符: aws_subnet.rSubnetMGMT.*.id ,它将解析为所创建子网的 ID 列表。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM