# Query on dynamic Reiteration cidr Subnets

**URL:** <https://discuss.hashicorp.com/t/query-on-dynamic-reiteration-cidr-subnets/22365>\
**Category:** Terraform\
**Created:** [March 23, 2021, 10:24am UTC](https://discuss.hashicorp.com/t/query-on-dynamic-reiteration-cidr-subnets/22365 "2021-03-23T10:24:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fozzy77](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/fozzy77/32/16580_2.png) [@fozzy77](https://discuss.hashicorp.com/u/fozzy77)\
**Post date:** [March 23, 2021, 10:24am UTC](https://discuss.hashicorp.com/t/query-on-dynamic-reiteration-cidr-subnets/22365/1 "2021-03-23T10:24:37Z")

</div>

I need to create dynamic subnet space and it was following an example, so in a give scenario creating 15 subnets across 3 az’s with a 27 suffix. I started here eg \>\> base\_cidr\_block = “10.178.0.0/16”  
networks = [  
{  
name = “eu-west-2a”  
new\_bits = var.Subnet\_Bits  
},  
{  
name = “eu-west-2b”  
new\_bits = var.Subnet\_Bits  
},  
{  
name = “eu-west-2c”  
new\_bits = var.Subnet\_Bits  
},  
{  
]  
}

resource “aws\_vpc” “example” {  
cidr\_block = module.subnet\_addrs.base\_cidr\_block  
}

resource “aws\_subnet” “example” {  
for\_each = module.subnet\_addrs.network\_cidr\_blocks

vpc\_id = aws\_vpc.example.id  
availability\_zone = each.key  
cidr\_block = each.value

But this fails once I add the next in sequence as the key is repeated , is there a better way or am I doing something wrong . I was using the following source \>\>module “subnet\_addrs” {  
source = “hashicorp/subnets/cidr”

---

<div class="post-metadata">

**Author:** ![fozzy77](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/fozzy77/32/16580_2.png) [@fozzy77](https://discuss.hashicorp.com/u/fozzy77)\
**Post date:** [March 23, 2021, 3:52pm UTC](https://discuss.hashicorp.com/t/query-on-dynamic-reiteration-cidr-subnets/22365/2 "2021-03-23T15:52:02Z")

</div>

I tried this , and it seems to work , any feedback or advice would be greatesource “aws\_subnet” “private\_infrastructure” {  
for\_each = var.subnet\_map  
vpc\_id = aws\_vpc.main.id  
cidr\_block = cidrsubnet(var.vpc\_cidr,11, each.key)  
availability\_zone = length(regexall("^[a-z]{2}-[a-z]±[0-9][a-z]", element(var.availability\_zone\_list, each.key))) \> 0 ? element(var.availability\_zone\_list, each.key) : null  
tags = merge(var.tags, map(“Name”, format("%s-subnet-private\_infrastructure-%s", aws\_vpc.main.id, element(var.availability\_zone\_list, each.key))))  
}  
Using this variable “subnet\_map” {  
description = “subnet”  
type = map(list(string))  
default = {  
“0” = [“eu-west-2a”]  
“1” = [“eu-west-2b”]  
…
