# Terraform Error: Forecah loop fails with error

**URL:** <https://discuss.hashicorp.com/t/terraform-error-forecah-loop-fails-with-error/30373>\
**Category:** Terraform\
**Created:** [October 7, 2021, 8:14am UTC](https://discuss.hashicorp.com/t/terraform-error-forecah-loop-fails-with-error/30373 "2021-10-07T08:14:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![PrateekKhatri](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/prateekkhatri/32/19718_2.png) [@PrateekKhatri](https://discuss.hashicorp.com/u/PrateekKhatri)\
**Post date:** [October 7, 2021, 8:14am UTC](https://discuss.hashicorp.com/t/terraform-error-forecah-loop-fails-with-error/30373/1 "2021-10-07T08:14:45Z")

</div>

Hi Team,

I am trying to fetch the details of VPC which is creating at run time. I have added below data block to fetch the details of the subnet attached to the VPC.

terraform version 0.15.3

\*\*data “aws\_subnets” “example” {  
filter {  
name = “vpc-id”  
values = [aws\_vpc.vpc.id]  
}  
}

data “aws\_subnet” “example” {  
for\_each = data.aws\_subnets.example.ids  
id = each.value  
}

output “subnet\_cidr\_blocks” {  
value = [for s in data.aws\_subnet.example : s.cidr\_block]  
}

resource “aws\_vpc” “vpc” {  
cidr\_block = var.cidr\_block  
instance\_tenancy = “default”  
enable\_dns\_hostnames = true  
enable\_dns\_support = true  
assign\_generated\_ipv6\_cidr\_block = var.enable\_ipv6

tags = {  
Name = “{var.env}-vpc" Environment = "{var.env}”  
}  
}\*\*

While running **terraform plan** I am getting below error:

**on modules\network\vpc\vpc.tf line 19, in data “aws\_subnet” “example”:  
│ 19: for\_each = data.aws\_subnets.example.ids  
│ ├────────────────  
│ │ data.aws\_subnets.example.ids is a list of string, known only after apply  
│  
│ The “for\_each” 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  
│ for\_each depends on.**

Can you please help me with this error?

---

<div class="post-metadata">

**Author:** ![jbardin](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/jbardin/32/3325_2.png) [@jbardin](https://discuss.hashicorp.com/u/jbardin)\
**Post date:** [October 7, 2021, 12:46pm UTC](https://discuss.hashicorp.com/t/terraform-error-forecah-loop-fails-with-error/30373/2 "2021-10-07T12:46:15Z")

</div>

Hi @PrateekKhatri,

The error text:

> The “for\_each” value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created.

Means that the value `data.aws_subnets.example.ids` is not known until the resources it references are applied, therefor terraform cannot plan how to create individual instances with `for_each`. While Terraform will always attempt to read a data source during planning when possible, your `aws_subnets` data source cannot read data about the `aws_vpc` before the `aws_vpc` has been created.

Seeing how you are creating the vpc in this configuration, I assume you are also creating the subnets within that vpc and have access to those managed resources as well. I cannot tell what you need the `aws_subnet` data source for in this example, but in general if you have a managed resource in a configuration you should not be using a data source representing the same logical resource.

If the resources are not in the configuration, you will need some static source of values for the `for_each` statement. Since at some level the subnets must be created from a static collection of values, that same collection of values would be fed into this configuration to populate the `for_each` expressions.
