首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Terraform插值/表达式v0.12

Terraform插值/表达式v0.12
EN

Stack Overflow用户
提问于 2019-08-07 12:47:03
回答 1查看 837关注 0票数 1

因此,在从Terraformv0.11.4升级到v0.12.5之后,我碰到了一个已知的"bug“,详见Hashicorps票证#21411,它引用了列表变量,并使用flatten函数来管理它。

正如Hashicorp升级说明中所描述的那样,我使用平面函数转换了几个属性值,但到目前为止,这个属性值已经超过了我:

代码语言:javascript
复制
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}"
    },
  )
}

返回的错误消息是:

代码语言:javascript
复制
   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.

存储在远程状态中的值如下:

代码语言:javascript
复制
subnet_ids_frontend = [
  [
    "subnet-03b44ca6123456789",
    "subnet-02e6bf55123456789",
  ],
]

subnet_id格式已经由terraform升级脚本更新,但是正如文档所提到的,它在这方面有点失败。

有什么想法吗?这里正确的格式/功能是什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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的第一类变量表达式)。您的资源的代码更新将是:

代码语言:javascript
复制
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值构造为列表而不是嵌套列表,这样就不需要进行代码更新了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57394872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档