简体   繁体   English

来自另一个Terraform计划的参考变量

[英]Reference variables from another Terraform plan

I have created a set-up with main and disaster recovery website architecture in AWS using Terraform. 我已经使用Terraform在AWS中创建了具有主要和灾难恢复网站架构的设置。

The main website is in region1 and disaster recovery is in region2. 主网站位于region1中,灾难恢复位于region2中。 This script is created as different plans or different directories. 该脚本创建为不同的计划或不同的目录。

For region1, I created one directory which contains only the main website Terraform script to launch the main website infrastructure. 对于region1,我创建了一个目录,该目录仅包含主要网站Terraform脚本以启动主要网站基础结构。

For region2, I created another directory which contains only the disaster recovery website Terraform script to launch the disaster recovery website infrastructure. 对于region2,我创建了另一个目录,该目录仅包含灾难恢复网站Terraform脚本以启动灾难恢复网站基础结构。

In my main website script, I need some values of the disaster recovery website such as VPC peering connection ID, DMS endpoint ARNs etc. 在我的主要网站脚本中,我需要灾难恢复网站的一些值,例如VPC对等连接ID,DMS端点ARN等。

How can I reference these variables from the disaster recovery website directory to the main website directory? 如何从灾难恢复网站目录到主网站目录引用这些变量?

One option is to use the terraform_remote_state data source to fetch outputs from the other state file like this: 一种选择是使用terraform_remote_state数据源从另一个状态文件中获取输出,如下所示:

vpc/main.tf vpc / main.tf

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

output "vpc_id" {
  value = "${aws_vpc.foo.id}"
}

route/main.tf 路线/main.tf

data "terraform_remote_state" "vpc" {
  backend = "s3"
  config {
    bucket = "mybucket"
    key    = "path/to/my/key"
    region = "us-east-1"
  }
}

resource "aws_route_table" "rt" {
  vpc_id = "${data.terraform_remote_state.vpc.vpc_id}"
}

However, it's nearly always better to just use the native data sources of the provider as long as they exist for the resource you need. 但是,只要存在提供者所需的资源的原始数据源,几乎总是更好。

So in your case you will need to use data sources such as the aws_vpc_peering_connection data source to be able to establish cross VPC routing with something like this: 因此,在您的情况下,您将需要使用诸如aws_vpc_peering_connection数据源之aws_vpc_peering_connection数据源,以便能够通过以下方式建立跨VPC路由:

data "aws_vpc_peering_connection" "pc" {
  vpc_id          = "${data.aws_vpc.foo.id}"
  peer_cidr_block = "10.0.0.0/16"
}

resource "aws_route_table" "rt" {
  vpc_id = "${aws_vpc.foo.id}"
}

resource "aws_route" "r" {
  route_table_id            = "${aws_route_table.rt.id}"
  destination_cidr_block    = "${data.aws_vpc_peering_connection.pc.peer_cidr_block}"
  vpc_peering_connection_id = "${data.aws_vpc_peering_connection.pc.id}"
}

You'll need to do similar things for any other IDs or things you need to reference in your DR region. 您需要对DR区域中的其他任何ID或需要引用的内容执行类似的操作。

It's worth noting that there's not any data sources for the DMS resources so you would either need to use the terraform_remote_state data source to fetch any IDs (such as the source and target endpoint ARNs to setup the aws_dms_replication_task or you could structure things so that all of the DMS stuff happens in the DR region and then you only need to refer to the other region's VPC ID , database names and potentially KMS key IDs which can all be done via data sources. 值得注意的是,DMS资源没有任何数据源,因此您要么需要使用terraform_remote_state数据源来获取任何ID(例如源端点ARN和目标端点ARN来设置aws_dms_replication_task要么可以对其进行结构化,以便所有DMS发生在DR区域中,然后您只需引用其他区域的VPC ID数据库名称以及可能的KMS密钥ID ,这些都可以通过数据源完成。

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

相关问题 如何使用从一个 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 无法将 AWS API 网关使用计划引用为 Terraform 中的数据源 - Unable to reference an AWS API Gateway Usage Plan as a data source in Terraform 来自其他 Terraform 资源的 Terraform 模板变量 - Terraform template variables from other Terraform resources 如何在 Kubernetes 供应商计划中引用 AWS 供应商计划的输出? - How to reference the output from AWS provider plan in Kubernetes provider plan? 将 terraform 配置文件移动到单独的目录中,将它们从计划中删除 - Moving terraform configuration files into separate directory removes them from the plan 地形计划命令失败 - Terraform plan command failing Terraform 计划无法完成 - Terraform Plan unable to finish Terraform 将两个变量组合成另一个变量 - Terraform combining two variables into another variable 如何从驻留在 Terraform 中不同模块中的另一个 main.tf 文件中引用 id 值 - How to reference an id value from another main.tf file residing in a different module in Terraform Terraform 计划取 terraform 配置后 - Terraform plan after fetching terraform configuration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM