因此,在从Terraformv0.11.4升级到v0.12.5之后,我碰到了一个已知的"bug“,详见Hashicorps票证#21411,它引用了列表变量,并使用flatten函数来管理它。
正如Hashicorp升级说明中所描述的那样,我使用平面函数转换了几个属性值,但到目前为止,这个属性值已经超过了我:
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升级脚本更新,但是正如文档所提到的,它在这方面有点失败。
有什么想法吗?这里正确的格式/功能是什么?
发布于 2019-08-07 15:28:32
存储在远程状态中的subnet_ids_frontend变量是两个维度的嵌套列表。因此,您需要使用第一个元素访问嵌套列表,然后使用第二个元素访问字符串。这将通过data.terraform_remote_state.remote_state.outputs.subnet_ids_frontend[0][<subnet_id_element>]完成(注意,这利用了Terraform0.12的第一类变量表达式)。您的资源的代码更新将是:
resource "aws_nat_gateway" "nat_gw" {
count = local.nat_gw_count
allocation_id = element(aws_eip.nat_gw.*.id, count.index)
subnet_id = data.terraform_remote_state.remote_state.outputs.subnet_ids_frontend[0][count.index]
...
}或者,可以将存储在远程状态中的subnet_ids_frontend值构造为列表而不是嵌套列表,这样就不需要进行代码更新了。
https://stackoverflow.com/questions/57394872
复制相似问题