V
V
VITYA-XY12020-12-21 18:32:59
Amazon Web Services
VITYA-XY1, 2020-12-21 18:32:59

How to correctly set the id of dynamically created subnets in elb terraform?

How to correctly write the id of the created subnets in elb terraform?

data "aws_availability_zones" "available" {}

locals {
  az_names = data.aws_availability_zones.available.names
}
resource "aws_subnet" "wpl_public_subnet" {
  for_each                = { for index, az_name in local.az_names : index => az_name }
  vpc_id                  = aws_vpc.wpl_vpc.id
  cidr_block              = cidrsubnet(var.vpc_cidr, 8, each.key + 10)
  availability_zone       = local.az_names[each.key]
  map_public_ip_on_launch = true
    tags = {
        Name              = "WPL-PublicSubnet"
        CreatedBy         = var.created_by
    }
}

resource "aws_elb" "wpl_elb" {
  name               = "terraform-elb"
  
  security_groups    = [aws_security_group.wpl_sg.id]
  subnets            = [for_each aws_subnet.wpl_public_subnet : each.value.id]
  

  listener {
    instance_port     = 80
    instance_protocol = "http"
    lb_port           = 80
    lb_protocol       = "http"
  }

  cross_zone_load_balancing   = true
  idle_timeout                = 400
  connection_draining         = true
  connection_draining_timeout = 400

    tags = {
          Name        = "WPL-ELB"
    }
}


I googled and didn't find a good example.

subnets            = [aws_subnet.wpl_public_subnet[*].id]

it doesn't work like that :(

Maybe you need to use aws_subnet.wpl_public_subnet[*].id as a module output and then you can pass it as a list?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mureevms, 2020-12-21
@mureevms

subnets = ["${aws_subnet.wpl_public_subnet.*.id}"]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question