title: Automatically create a subnet for each AWS availability zone in Terraform
body: <p>Is there a better way to optime the code below so I don't have to ask for availability zone again and again instead can do it in once. as the region is variable so I cant define hardcoded availability zone. can you guys please I want my public subnets to be /24</p>
  <pre><code>provider &quot;aws&quot; {
      region = var.region
  }
  
  resource &quot;aws_vpc&quot; &quot;app_vpc&quot; {
    cidr_block           = var.vpc_cidr
    enable_dns_support   = true
    enable_dns_hostnames = true
    tags = {
      Name = var.vpc_name
    }
  }
  
  
  data &quot;aws_availability_zones&quot; &quot;available&quot; {
    state = &quot;available&quot;
  }
  
  #provision public subnet
  resource &quot;aws_subnet&quot; &quot;public_subnet_01&quot; {
    vpc_id     = aws_vpc.app_vpc.id
    cidr_block = var.public_subnet_01
    availability_zone = data.aws_availability_zones.available.names[0]
    tags = {
      Name = &quot;public_subnet_01&quot;
    }
    depends_on = [aws_vpc_dhcp_options_association.dns_resolver]
  }
  resource &quot;aws_subnet&quot; &quot;public_subnet_02&quot; {
    vpc_id     = aws_vpc.app_vpc.id
    cidr_block = var.public_subnet_02
    availability_zone = data.aws_availability_zones.available.names[1]
    tags = {
      Name = &quot;public_subnet_02&quot;
    }
    depends_on = [aws_vpc_dhcp_options_association.dns_resolver]
  }
  resource &quot;aws_subnet&quot; &quot;public_subnet_03&quot; {
    vpc_id     = aws_vpc.app_vpc.id
    cidr_block = var.public_subnet_03
    availability_zone = data.aws_availability_zones.available.names[2]
    tags = {
      Name = &quot;public_subnet_03&quot;
    }
    depends_on = [aws_vpc_dhcp_options_association.dns_resolver]
  }
  </code></pre>