title: Attach multiple private subnet to route table for each terraform
body: <p>I have public and private subnets established in a VPC created with for each. I am now trying to create route tables for the subnets and nat gateways specifically for access for private instances. My subnets, route tables, and public subnet associations are working properly. I am having trouble getting my private subnets to attach to the route table connecting it to the NAT gateway. I believe my logic correct. My NAT gateways are sitting in my public subnets. The only issue is private subnets being attached to the route table that connects to the NAT gateway. Below is my code, any advice is appreciated.</p>
  <pre><code>resource &quot;aws_route_table&quot; &quot;public&quot; {
    for_each = var.pub_subnet
    vpc_id   = aws_vpc.main.id
  
    route {
      cidr_block = &quot;0.0.0.0/0&quot;
      gateway_id = aws_internet_gateway.main.id
    }
  
    tags = {
      Name = var.rt_tags
    }
  }
  
  resource &quot;aws_route_table_association&quot; &quot;public&quot; {
    for_each       = aws_subnet.public
    route_table_id = aws_route_table.public[each.key].id
    subnet_id      = each.value.id
  }
  
  resource &quot;aws_route_table_association&quot; &quot;nat&quot; {
    for_each       = aws_subnet.private
    route_table_id = aws_route_table.nat[each.key].id
    subnet_id      = each.value.id
  }
  
  resource &quot;aws_route_table&quot; &quot;nat&quot; {
    for_each = var.pub_subnet
    vpc_id   = aws_vpc.main.id
  
    route {
      cidr_block = &quot;0.0.0.0/0&quot;
      gateway_id = aws_nat_gateway.main[each.key].id
    }
  
    tags = {
      Name = var.rt_tags_private
    }
  }
  
  resource &quot;aws_subnet&quot; &quot;public&quot; {
    for_each                = var.pub_subnet
    vpc_id                  = aws_vpc.main.id
    cidr_block              = each.value.cidr_block
    availability_zone       = each.value.availability_zone
    map_public_ip_on_launch = true
    tags = {
      Name = each.key
    }
  }
  
  resource &quot;aws_subnet&quot; &quot;private&quot; {
    for_each                = var.priv_subnet
    vpc_id                  = aws_vpc.main.id
    cidr_block              = each.value.cidr_block
    availability_zone       = each.value.availability_zone
    map_public_ip_on_launch = false
    tags = {
      Name = each.key
    }
  }
  </code></pre>
  <p><strong>Variables</strong></p>
  <pre><code>variable &quot;pub_subnet&quot; {
    type = map(object({
      cidr_block        = string
      availability_zone = string
    }))
    default = {
      &quot;PubSub1&quot; = {
        cidr_block        = &quot;10.0.1.0/24&quot;
        availability_zone = &quot;us-west-1a&quot;
      }
    }
  }
  
  variable &quot;priv_subnet&quot; {
    type = map(object({
      cidr_block        = string
      availability_zone = string
    }))
    default = {
      &quot;PrivSub1&quot; = {
        cidr_block        = &quot;10.0.2.0/24&quot;
        availability_zone = &quot;us-west-1c&quot;
      }
    }
  }
  </code></pre>
  <p><strong>Error</strong></p>
  <pre><code>Error: Invalid index
  
    on vpc.tf line 61, in resource &quot;aws_route_table_association&quot; &quot;nat&quot;:
    61:   route_table_id = aws_route_table.nat[each.key].id
      |----------------
      | aws_route_table.nat is object with 1 attribute &quot;PubSub1&quot;
      | each.key is &quot;PrivSub1&quot;
  
  The given key does not identify an element in this collection value.
  </code></pre>
  <p><strong>NAT Gateway</strong></p>
  <pre><code>resource &quot;aws_nat_gateway&quot; &quot;main&quot; {
    for_each      = aws_subnet.public
    subnet_id     = each.value.id
    allocation_id = aws_eip.main[each.key].id
  }
  </code></pre>
  <p><strong>EIP</strong></p>
  <pre><code>resource &quot;aws_eip&quot; &quot;main&quot; {
    for_each = aws_subnet.public
    vpc      = true
  
    lifecycle {
      create_before_destroy = true
    }
  }
  </code></pre>