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?