2

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) ?

Pouya Ataei
  • 123
  • 4

2 Answers2

1

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 }

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) }