<p>You'll have to use the output of the list in a for loop to get the subnet ids for_each, but it would look something like this:</p>
<pre><code>data &quot;aws_vpcs&quot; &quot;foo&quot; {}

output &quot;vpcs&quot; {
  value = data.aws_vpcs.foo.ids
}
</code></pre>

CC BY-SA 4.0
2021-08-13T18:36:26.060Z
2021-08-13T18:36:26.060Z
Lorenzo Couto
<p>You can use Terraform data sources.</p>
<p>Terraform official link :
<a href="https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc" rel="nofollow noreferrer">https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc</a></p>
<h5>Example:</h5>
<pre><code>variable &quot;vpc_id&quot; {}

data &quot;aws_vpc&quot; &quot;selected&quot; {
  id = var.vpc_id
}

resource &quot;aws_subnet&quot; &quot;example&quot; {
  vpc_id            = data.aws_vpc.selected.id
  availability_zone = &quot;us-west-2a&quot;
  cidr_block        = cidrsubnet(data.aws_vpc.selected.cidr_block, 4, 1)
}
</code></pre>

CC BY-SA 4.0
2021-08-16T05:32:31.983Z
2021-08-16T05:32:31.983Z
Raghu Reddy
<p>I'd like to get a list of all remote VPCs from AWS with terraform. I've read the documentation and found two data sources:</p>
<ol>
<li><strong>AWS_DEFAULT_VPC</strong></li>
<li><strong>AWS_VPC</strong></li>
</ol>
<p>The first one will give you the default VPC and the second one accepts a VPC id and toss out other information such as subnets...</p>
<p>How can I get a list of all VPCs existing in ceratin AWS region (US-West-2 for instance) ?</p>

<amazon-web-services><terraform><aws-vpc>
Get a list of remote VPCs on AWS using Terraform?
Pouya Ataei
# Get a list of remote VPCs on AWS using Terraform?

(Asked by: username_0 on 2021-08-12)

I'd like to get a list of all remote VPCs from AWS with terraform. I've read the documentation and found two data sources:

1. **AWS\_DEFAULT\_VPC**
2. **AWS\_VPC**

The first one will give you the default VPC and the second one accepts a VPC id and toss out other information such as subnets...

How can I get a list of all VPCs existing in ceratin AWS region (US-West-2 for instance) ?

**Tags:** amazon-web-services, terraform, aws-vpc


---

## 2 answers

### Answer by username_1 on 2021-08-13. Score: 1 | Accepted answer

You'll have to use the output of the list in a for loop to get the subnet ids for_each, but it would look something like this:
data "aws_vpcs" "foo" {}

output "vpcs" {
  value = data.aws_vpcs.foo.ids
}


---

### Answer by username_2 on 2021-08-16. Score: 0

You can use Terraform data sources.
Terraform official link :
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/vpc
Example:
variable "vpc_id" {}

data "aws_vpc" "selected" {
  id = var.vpc_id
}

resource "aws_subnet" "example" {
  vpc_id            = data.aws_vpc.selected.id
  availability_zone = "us-west-2a"
  cidr_block        = cidrsubnet(data.aws_vpc.selected.cidr_block, 4, 1)
}


---

