【问题标题】:Getting length of list from Terraform Map从 Terraform Map 获取列表的长度
【发布时间】:2019-02-02 09:00:44
【问题描述】:

我目前在 test.tfvars 文件中有这张地图:

ssm = {
    names = ["Terraform-1","Terraform-2","Terraform-3"]
    values = ["tf-1","tf-2","tf-3"]
}

而我想做的是以下几点:

resource "aws_ssm_parameter" "parameter_store" {
  count = 3
  name = "$${element(var.ssm[names],count.index)}"
  type = "String"
  value = "$${element(var.ssm[values],count.index)}"
}

但我希望计数基于我的 ssm 地图中名称列表的长度,而不是 count=3。我试过这个:

"${length(var.ssm[names])}"

但我得到了错误:

Error: aws_ssm_parameter.parameter_store: resource count can't reference variable: names

谁能指出我解决此错误的正确方向?我不太确定自己做错了什么。

【问题讨论】:

  • 你为什么把你的美元符号加倍?这些应该是单身。例如。 name = "${element(var.ssm[names],count.index)}"

标签: terraform terraform-provider-aws


【解决方案1】:

当前的 terraform 版本 (0.11.x) 在需要处理嵌套在地图中的列表时,有时会表现得有些奇怪。这可能会在新版本 0.12.x 中得到解决,但也许有更好的解决方案...

你为什么不这样重组你的地图:

ssm = {
    "Terraform-1" = "tf-1"
    "Terraform-2" = "tf-2"
    "Terraform-3" = "tf-3"
}

您的资源现在看起来像这样:

resource "aws_ssm_parameter" "parameter_store" {
  count = "${length(var.ssm)}"
  name  = "${keys(var.ssm)[count.index]}"
  type  = "String"
  value = "${values(var.ssm)[count.index]}"
}

【讨论】:

  • 不适合我,得到parse error at 1:20: expected "}" but found "["
  • 对我来说,这听起来更像是括号的问题......你能给我们看看你的代码吗?
猜你喜欢
  • 1970-01-01
  • 2022-11-14
  • 2021-02-23
  • 1970-01-01
  • 2023-04-06
  • 2015-11-20
  • 2017-07-28
  • 2021-07-25
  • 2021-10-06
相关资源
最近更新 更多