Getting the vpc_id of the default, or the only one VPC

Dear Colleagues,

Sorry for a probable FAQ, but the solutions I’ve been able to find on the Internet don’t work for me.

I want to avoid setting the VPC ID in my Terraform files and just want Terraform to use the only existing VPC in an AWS region. However some resources require the vpc_id, so I’ve tried variants of the following:

data "aws_vpc" "default" {                                                                                                                            
  default = true                                                                                                                                      
}                                                                                                                                                     
                                                                                                                                                      
resource "aws_security_group" "test1" {                                                                                                               
  name        = "test1"                                                                                                                               
  vpc_id      = data.aws_vpc.default.id                                                                                                               
...                                                                                                                                                   
}

This however produces the following error on “terraform apply”:

data.aws_vpc.default: Reading…
Error: no matching EC2 VPC found
with data.aws_vpc.default,
on main.tf line 26, in data “aws_vpc” “default”:
26: data “aws_vpc” “default” {

Do you think I can somehow get the vpc_id of the “current” (default, the only one) vpc?

There is a difference between the “default” VPC (which was created by AWS itself) and the case where only a single VPC exists (that wasn’t created by AWS).

Are you sure the VPC that exists is actually the “default” VPC?

Are you sure the VPC that exists is actually the “default” VPC?

No, I’m not sure. I am not even sure how to check if it is, as I have inherited this configuration.

Actually speaking, I don’t even care it it’s a defalt VPC. I just want to get the vpc_id of the VPC we are currently working in.

In which case you just need to adjust your data source to find the VPC. As you’ve written it only the default VPC will be found. Instead take a look at other methods for fetching the correct VPC, such as using tags.

Instead take a look at other methods for fetching the correct VPC, such as using tags.

If I have to tag or select the VPC in some way, I may as well use the VPC ID. The idea was to work with “whatever VPC there is.”

As you can see from the data source documentation (Terraform Registry) the different filters are all optional. So you could just have no filters. However it also states “The given filters must match exactly one VPC whose data will be exported as attributes.” so using the data source without any filters would only work if there is exactly one VPC in the account within the region being used. If there were more than one it would fail.

Thank you Stuart, I’ll try this approach with an empty filter.