简体   繁体   English

如何使用 terraform 获取默认的 vpc id

[英]How to get the default vpc id with terraform

I am trying to get the vpc_id of default vpc in my aws account using terraform我正在尝试使用terraform在我的aws帐户中获取default vpc 的vpc_id

This is what I tried but it gives an error这是我尝试过的,但它给出了一个错误

Error: Invalid data source错误:数据源无效

this is what I tried:这是我尝试过的:

data "aws_default_vpc" "default" {

}


# vpc
resource "aws_vpc" "kubernetes-vpc" {
  cidr_block = "${var.vpc_cidr_block}"
  enable_dns_hostnames = true

  tags = {
    Name = "kubernetes-vpc"
  }
}

The aws_default_vpc is indeed not a valid data source. aws_default_vpc确实不是有效的数据源。 But the aws_vpc data source does have a boolean default you can use to choose the default vpc:但是aws_vpc数据源确实有一个布尔default ,您可以使用它来选择默认 vpc:

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

For completeness, I'll add that an aws_default_vpc resource exists that also manages the default VPC and implements the resource life-cycle without really creating the VPC* but would make changes in the resource like changing tags (and that includes its name).为了完整起见,我将添加一个aws_default_vpc资源,该资源还管理默认 VPC 并实施资源生命周期,而无需真正创建 VPC*,但会更改资源,例如更改标签(包括其名称)。

* Unless you forcefully destroy the default VPC * 除非你强行销毁默认 VPC

From the docs:从文档:

This is an advanced resource and has special caveats to be aware of when using it.这是一种高级资源,在使用它时需要注意一些特殊的注意事项。 Please read this document in its entirety before using this resource.在使用此资源之前,请完整阅读本文档。

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_vpc https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/default_vpc

This这个

resource "aws_default_vpc" "default" {
}

will do.会做。

I think this is convenient for terraform projects managing a whole AWS account, but I would advise against using it whenever multiple terraform projects are deployed in a single organization account.我认为这对于管理整个 AWS 账户的 terraform 项目很方便,但我建议不要在单个组织账户中部署多个 terraform 项目时使用它。 You should better stay with @blokje5's answer in that case.在这种情况下,您最好继续使用@blokje5 的回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM