Home > database >  aws_route_table_association with multiple subnets at runtime (AWS)
aws_route_table_association with multiple subnets at runtime (AWS)

Time:04-19

I am trying to create a aws_route_table_association resource for public subnets. The number of public subnets will be determined at runtime and hence the number of associations to be created.

While doing a terraform plan my code fails. Below is the source code and error i am getting. Anybody able to advise on a way to accomplish this.

//  required subnets and their configurations
variable "required_subnets" {
  description = "list of subnets required"
  default     = ["public-1a", "private-1a", "public-1b", "private-1b"]
}

#create public and provate subnets
resource "aws_subnet" "subnets" {
  count             = length(var.required_subnets)
  vpc_id            = aws_vpc.my_vpc.id
  cidr_block        = lookup(var.subnet_conf[var.required_subnets[count.index]], "cidr")
  availability_zone = lookup(var.subnet_conf[var.required_subnets[count.index]], "availability_zone")

  # enable public ip addresses in public subnet
  map_public_ip_on_launch = false

  tags = {
    Name = var.required_subnets[count.index]
  }
}

//fetch reference to public subnets
data "aws_subnets" "public_subnets" {

  filter {
    name   = "vpc-id"
    values = [data.aws_vpc.vpc.id]
  }

  tags = {
    Name = "public-*"
  }
}

#assosiate public route table with public subnet
resource "aws_route_table_association" "public" {
  count          = length(data.aws_subnets.public_subnets.ids)
  subnet_id      = data.aws_subnets.public_subnets.ids[count.index]
  route_table_id = aws_route_table.my_public_route_table.id
}

the error is as below:

│ Error: Invalid count argument
│
│   on vpc.tf line 62, in resource "aws_route_table_association" "public":
│   62:   count          = length(data.aws_subnets.public_subnets.ids)
│
│ The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how    
│ many instances will be created. To work around this, use the -target argument to first apply only the resources that the   
│ count depends on.

I

CodePudding user response:

If required_subnets is all you need, then there is no reason for your data.aws_subnets.public_subnets. Also it would be much better to use for_each, not count, as for_each does not depend on the order of items. Thus, you can simply your code as follows:

//  required subnets and their configurations
variable "required_subnets" {
  description = "list of subnets required"
  default     = ["public-1a", "private-1a", "public-1b", "private-1b"]
}

#create public and provate subnets
resource "aws_subnet" "subnets" {
  for_each          = toset(var.required_subnets)
  vpc_id            = aws_vpc.my_vpc.id
  cidr_block        = lookup(var.subnet_conf[each.key], "cidr")
  availability_zone = lookup(var.subnet_conf[each.key], "availability_zone")

  # enable public ip addresses in public subnet
  map_public_ip_on_launch = false

  tags = {
    Name = each.key
  }
}

#assosiate public route table with public subnet
resource "aws_route_table_association" "public" {
  for_each       = {for name, subnet in aws_subnet.subnets: name => subnet if length(regexall("public-", name)) > 0}   

  subnet_id      = each.value.id
  route_table_id = aws_route_table.my_public_route_table.id
}
  • Related