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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.