简体   繁体   English

从 2 个字符串到列表的 Terraform 输出

[英]Terraform output from 2 strings to list

In terraform I have 2 data outputs:在 terraform 中,我有 2 个数据输出:

data "aws_instances" "daas_resolver_ip_1" {
  instance_tags = {
    Name = "${var.env_type}.${var.environment}.ns1.${var.aws_region}.a."
  }
}

data "aws_instances" "daas_resolver_ip_2" {
  instance_tags = {
    Name = "${var.env_type}.${var.environment}.ns2.${var.aws_region}.b."
  }
}

I want to get the private_ip from each of those combine those into a list and be used as follows:我想从每一个中获取 private_ip 将它们组合成一个列表并按如下方式使用:

  dhcp_options_domain_name_servers  = ["${data.aws_instances.daas_resolver_ip_1.private_ip}", "${data.aws_instances.daas_resolver_ip_1.private_ip}"]

How can I achieve this?我怎样才能做到这一点? At the moment this is the error I get:目前这是我得到的错误:

Error: module.pmc_environment.module.pmc_vpc.aws_vpc_dhcp_options.vpc: domain_name_servers: should be a list

I believe what you've encountered here is a common limitation of Terraform 0.11.我相信您在这里遇到的是 Terraform 0.11 的一个常见限制。 If this is a new configuration then starting with Terraform 0.12 should avoid the problem entirely, as this limitation was addressed in the Terraform 0.12 major release.如果这是一个新配置,那么从 Terraform 0.12 开始应该可以完全避免这个问题,因为这个限制在 Terraform 0.12 主要版本中得到了解决。

The underlying problem here is that the private_ip values of at least one of these resources is unknown during planning (it will be selected by the remote system during apply) but then Terraform 0.11's type checker is failing because it cannot prove that these unknown values will eventually produce a list of strings as the dhcp_options_domain_name_servers requires.这里的潜在问题是这些资源中至少一个的private_ip值在规划期间是未知的(它将在应用期间由远程系统选择)但是 Terraform 0.11 的类型检查器失败了,因为它无法证明这些未知值最终将生成dhcp_options_domain_name_servers需要的字符串列表。


Terraform 0.12 addresses this by tracking type information for unknown values and propagating types through expressions so that eg in this case it could know that the result is a list of two strings but the strings themselves are not known yet. Terraform 0.12 通过跟踪未知值的类型信息并通过表达式传播类型来解决这个问题,例如,在这种情况下,它可以知道结果是两个字符串的列表,但字符串本身还不知道。 From Terraform 0.11's perspective, this is just an unknown value with no type information at all, and is therefore not considered to be a list, causing this error message.从 Terraform 0.11 的角度来看,这只是一个完全没有类型信息的未知值,因此不被视为列表,导致此错误消息。


A workaround for Terraform 0.11 is to use the -target argument to ask Terraform to deal with the operations it needs to learn the private_ip values first, and then run Terraform again as normal once those values are known: Terraform 0.11 的解决方法是使用-target参数让 Terraform 处理它需要的操作以首先学习private_ip值,然后在知道这些值后再次正常运行 Terraform:

terraform apply -target=module.pmc_environment.module.pmc_vpc.data.aws_instances.daas_resolver_ip_1 -target=module.pmc_environment.module.pmc_vpc.data.aws_instances.daas_resolver_ip_2
terraform apply

The first terraform apply with -target set should deal with the two data resources, and then the subsequent terraform apply with no arguments should then be able to see what the two IP addresses are.第一个带有-target set 的terraform apply应该处理这两个数据资源,然后接下来的不带参数的terraform apply应该可以看到这两个 IP 地址是什么。

This will work only if all of the values contributing to the data resource configurations remain stable after the initial creation step.只有在初始创建步骤之后所有对数据资源配置有贡献的值都保持稳定时,这才会起作用。 You'd need to repeat this two-step process on subsequent changes if any of var.env_type , var.environment , or var.aws_region become unknown as a result of other planned actions.如果var.env_typevar.environmentvar.aws_region任何一个因其他计划的操作而变得未知,您需要对后续更改重复此两步过程。

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

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