【发布时间】:2019-08-07 23:28:32
【问题描述】:
所以在从 Terraform v0.11.4 升级到 v0.12.5 之后,我遇到了一个已知的“错误”,如 Hashicorps 票 #21411 中详述的关于引用列表变量和使用 flatten 函数来管理它。
我已经使用 Hashicorp 升级说明中描述的 flatten 函数转换了一些属性值,但到目前为止这个比我更胜一筹:
resource "aws_nat_gateway" "nat_gw" {
count = local.nat_gw_count
allocation_id = element(aws_eip.nat_gw.*.id, count.index)
subnet_id = element(data.terraform_remote_state.remote_state.outputs.subnet_ids_frontend, count.index)
tags = merge(
local.default_tags,
{
"Name" = "${var.project}_${var.env}_natgw_${count.index}"
},
)
}
返回的错误信息是:
Error: Incorrect attribute value type
on nat_gw.tf line 56, in resource "aws_route_table_association" "public":
56: subnet_id = element(data.terraform_remote_state.remote_state.outputs.subnet_ids_proxy, count.index)
Inappropriate value for attribute "subnet_id": string required.
远程状态中存储的值如下:
subnet_ids_frontend = [
[
"subnet-03b44ca6123456789",
"subnet-02e6bf55123456789",
],
]
subnet_id 格式已由 terraform 升级脚本更新,但正如文档中提到的那样,它在该领域有些偶然。
您知道这里使用的正确格式/功能是什么吗?
【问题讨论】:
-
您的
subnet_ids_frontend是一个嵌套列表而不是一个列表,因此您必须访问包含第一个元素的列表,然后访问包含第二个元素的字符串,即data.terraform_remote_state.remote_state.outputs.subnet_ids_frontend[0][count.index]。或者,将值存储为列表而不是嵌套列表,这也可以。 -
@MattSchuchard,好电话。我注意到这个远程状态输出似乎到处都是列表,但这是第一次出现问题。我一直在想我必须使用元素来取出元素。我会将此标记为答案,但它是一个评论,你想将其添加为答案吗?
标签: terraform