简体   繁体   English

如何使用从一个 Terraform 计划创建到另一个 Terraform 计划的 VPC id 和子网 id 值

[英]How to use the VPC id and subnets id values which were created from one Terraform plan to in another Terraform plan

I have created the VPC, subnets and security groups in one Terraform plan (let's call this Plan A ).我在一个 Terraform 计划(我们称之为Plan A )中创建了 VPC、子网和安全组。 It executed well and the state is stored in the remote S3 backend as well.它执行得很好,状态也存储在远程 S3 后端中。

Now I have a new Terraform plan (let's call this Plan B ) where I need to launch an EC2 instance.现在我有了一个新的 Terraform 计划(我们称之为Plan B ),我需要在其中启动一个 EC2 实例。 For this I need to fetch the VPC, subnet ids from the Plan A Terraform output.为此,我需要从Plan A Terraform 输出中获取 VPC、子网 ID。

Is there a recommended way to do this?有没有推荐的方法来做到这一点?

There's 2 main ways of passing outputs of things around in Terraform.在 Terraform 中有两种主要的方式来传递事物的输出。

The first, and oldest, way is to use the remote state feature to fetch outputs in a different state file.第一种也是最古老的方法是使用远程状态功能在不同的状态文件中获取输出。

The second, newer, approach is to use your provider's data sources that expose a read only request to your provider to fetch information about a resource.第二种更新的方法是使用提供者的 数据源,这些 数据源向提供者公开只读请求以获取有关资源的信息。

With this you would use the aws_vpc and aws_subnet_ids data sources to retrieve information about the relevant subnet IDs.有了这个,您将使用aws_vpcaws_subnet_ids数据源来检索有关相关子网 ID 的信息。

An example might look something like that given in the aws_subnet_ids docs:一个示例可能类似于aws_subnet_ids文档中给出的aws_subnet_ids

variable "vpc" {}
variable "ami" {}

data "aws_vpc" "selected" {
  tags {
    Name = "${var.vpc}"
  }
}

data "aws_subnet_ids" "private" {
  vpc_id = "${data.aws_vpc.selected.id}"
  tags {
    Tier = "Private"
  }
}

resource "aws_instance" "app" {
  count         = "3"
  ami           = "${var.ami}"
  instance_type = "t2.micro"
  subnet_id     = "${element(data.aws_subnet_ids.private.ids, count.index)}"
}

This would place a single EC2 instance in each of your 3 subnets that are tagged with Tier = Private in the VPC with a Name tag value provided by the vpc variable.这将在 VPC 中标记为Tier = Private 3 个子网中的每个子网中放置一个 EC2 实例,并使用vpc变量提供的Name标记值。

Obviously you can push this further by using the aws_ami data source to also fetch the AMI ID you want to use based on some filtering criteria.显然,您可以通过使用aws_ami数据源进一步推动这一点,以根据某些过滤条件获取您想要使用的 AMI ID。 This also helps to remove some of the more magic variables you might have in your Terraform code otherwise.这也有助于删除一些您可能在 Terraform 代码中拥有的更神奇的变量。

If you created your Plan A vpc and subnet with a unique tag (eg: Name) you can fetch them easily using the following example:如果您使用唯一标签(例如:名称)创建了您的Plan A vpcsubnet ,您可以使用以下示例轻松获取它们:

data "aws_vpc" "selected" {
  filter {
    name = "tag:Name"
    values = ["my_vpc_name"]
  }
}

data "aws_subnet" "selected" {
  filter {
    name = "tag:Name"
    values = ["my_subnet_name"]
  }
}

resource "aws_security_group" "sg" {
  vpc_id = data.aws_vpc.selected.id
  ...
}

resource "aws_instance" "instance" {
  vpc_security_group_ids = [ aws_security_group.sg.id ]
  subnet_id              = data.aws_subnet.selected.id
  ...
}

Note: It's easy to modify your old resources to include the Name tag (or any tag)注意:修改旧资源以包含名称标签(或任何标签)很容易

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

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