Getting the default vpc id

7,462 views
Skip to first unread message

Barry Skalrud

unread,
Feb 17, 2016, 6:03:26 PM2/17/16
to Terraform
Hi, 

So I'm very new to terraform and wondering how the following can be done.

If I want to create an ec2 instance in the default vpc how do I then refer to the details of the vpc? For example, if you create a new vpc using the resource,

resource "aws_vpc" "default" {
   cidr_block = "10.0.0.0/16"
}

you can then refer back to the created vpc when specifying the subnet, for example,

resource "aws_subnet" "default" {
  vpc_id                  = "${aws_vpc.default.id}"
  cidr_block              = "10.0.1.0/24"
  map_public_ip_on_launch = true
}

However, if you don't create a vpc, how can you reference the default one so that you can obtain properties like, vpc.id and vpc.main_route_table_id?

Obviously, you could go in to your AWS console and get the values manually, but I'm looking for a more automated solution.

Thanks! 


Andrew Langhorn

unread,
Feb 18, 2016, 4:48:33 AM2/18/16
to terrafo...@googlegroups.com
Terraform can only use computed variables when it knows about the state of existing infrastructure, which is to say that if a VPC or other infrastructure was not created through Terraform, trying to access it isn't possible as far as I know.

Is there a reason why you're using the default VPC? Creating a new VPC would allow you to get running with a VPC with computed variables.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/14c54955-c29b-41fc-9e6f-2a71b547d74d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Andrew Langhorn
Senior Infrastructure Engineer
Emailandrew....@thoughtworks.com
Telephone+44 7733 339809
ThoughtWorks

Barry Skalrud

unread,
Feb 18, 2016, 5:44:04 PM2/18/16
to terrafo...@googlegroups.com
Ok cool, thanks for that. 

There's no real reason for trying to access the default VPC, was just playing around with the variables and was wondering how it was possible. Although, just thinking about it now, there's no reason to access the default VPC, but what if you had already created a VPC for another deployment and wanted to use that one? You could enter in all the relevant information manually from the AWS console, but it would be nice to just know the id VPC and then use that to get additional information. 

Anyway, not critical, just a nice to have. 

Barry

You received this message because you are subscribed to a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/bXZc1KNAcgU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to terraform-too...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/CAEpa1DL%2Bjg8%2BvQCVCOteM5gP_j0weCW1swzNojw-O8OZKseP8g%40mail.gmail.com.

Andrew Langhorn

unread,
Feb 21, 2016, 1:41:30 PM2/21/16
to terrafo...@googlegroups.com
If you had already created infrastructure outside of Terraform, you could use something like Terraforming in the absence of first-class support for building a state with Terraform.

So, I suppose in this instance, you could run Terraforming against your existing AWS account, and then work with Terraform from that point on. That's something I hadn't considered earlier in the week. Terraforming might not always produce perfect state files, so be wary of that. Probably best to run a 'terraform plan' after you have the state file from it just to make sure it's at least syntactically valid.

Personally, I leave my default VPC empty and spin up new ones so that I have complete control over their creation from the ground up rather than relying on AWS behaviour.


For more options, visit https://groups.google.com/d/optout.

Aureli Gomez

unread,
Aug 24, 2017, 11:35:50 AM8/24/17
to Terraform
Did you try the format:


as value into a variable, and then use it. This should get the default vpc id, 


On Thursday, February 18, 2016 at 11:44:04 PM UTC+1, Barry Skalrud wrote:
Ok cool, thanks for that. 

There's no real reason for trying to access the default VPC, was just playing around with the variables and was wondering how it was possible. Although, just thinking about it now, there's no reason to access the default VPC, but what if you had already created a VPC for another deployment and wanted to use that one? You could enter in all the relevant information manually from the AWS console, but it would be nice to just know the id VPC and then use that to get additional information. 

Anyway, not critical, just a nice to have. 

Barry

Trung Nguyen Kien

unread,
Aug 25, 2017, 1:29:19 PM8/25/17
to Terraform
This is what I do to get the default VPC and subnets

data "aws_vpc" "default" {
  default = true
}

data "aws_subnet" "default" {
  vpc_id            = "${data.aws_vpc.default.id}"
  default_for_az    = true
  availability_zone = "${var.availability_zone}"
}

data "aws_subnet_ids" "all" {
  vpc_id = "${data.aws_vpc.vpc.id}"
}

Of course you can enhance it further to put it in a module which takes `use_default` boolean and manipulate the logic between creating a new VPC or reuse a default one.
Something like:

resource "aws_vpc" "new_vpc" {
  count      = "${var.use_default == "true" ? 0 : 1}"
  cidr_block = "${var.vpc_cidr_block}"
}

data "aws_vpc" "vpc" {
  id = "${var.use_default == "true" ? data.aws_vpc.default.id : join(" ", aws_vpc.new_vpc.*.id)}"
}

resource "aws_subnet" "new_subnet" {
  count             = "${var.use_default == "true" ? 0 : 1}"
  cidr_block        = "${cidrsubnet(aws_vpc.new_vpc.cidr_block, 4, count.index + 1)}"
  vpc_id            = "${aws_vpc.new_vpc.id}"
  availability_zone = "${var.availability_zone}"
}

data "aws_subnet" "subnet" {
  id = "${var.use_default == "true" ? data.aws_subnet.default.id : join(" ", aws_subnet.new_subnet.*.id)}"
}

output "vpc_id" {
value = "${data.aws_vpc.vpc.id}"
}

output "subnet_id" {
value = "${data.aws_subnet.subnet.id}"
}

output "subnet_cidr_block" {
value = "${data.aws_subnet.subnet.cidr_block}"
}

output "vpc_cidr_block" {
value = "${data.aws_vpc.vpc.cidr_block}"
}

output "subnet_ids" {
value = ["${data.aws_subnet_ids.all.ids}"]
}

Message has been deleted

Clint Shryock

unread,
May 16, 2018, 1:46:53 PM5/16/18
to terrafo...@googlegroups.com
Hello –

As mentioned earlier, you can use the aws_vpc data source to reference the default aws vpc:


There is also a resource that you can use which allows you to not just read it, but set values


Regarding existing infrastructure, Terraform does support importing existing infrastructure into state:


It's done resource-by-resource, not all at once. 

Cheers,
Clint

On Sun, May 13, 2018 at 8:11 PM, Roger Brandt <rogerbr...@gmail.com> wrote:
When I attempted to use this technique, it appears that the existing default VPC for my account and region was adopted into my application.  The default VPC for my account and region was set up by our network and security group, so I do not have permissions to delete it (nor would I want to since it is shared).  When I attempted to destroy my application, I received this error 

  module.linux_builder.data.aws_vpc.default: 1 error(s) occurred:

* module.linux_builder.data.aws_vpc.default: data.aws_vpc.default: UnauthorizedOperation: You are not authorized to perform this operation.
status code: 403, request id: 46316bc5-67d6-4b07-8654-a150a426dcf5
* data.aws_subnet_ids.all: 1 error(s) occurred:

* data.aws_subnet_ids.all: data.aws_subnet_ids.all: UnauthorizedOperation: You are not authorized to perform this operation.
status code: 403, request id: 0d745f88-aa90-4dfb-bf98-fab371f446d1

Is there a way to get default VPC information without adopting it into my application's configuration?
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/7a026c08-2deb-4c4f-9b5c-320a625e0234%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages