使用Terraform 0.11将列表转换为地图

时间:2019-06-17 18:44:07

标签: terraform

使用Terraform 0.11.x (我不在乎0.12),假设我有一个列表键,并且想要使用以下方法将其转换为地图:

locals {
  account = "287985351234"
  names   = ["alpha", "beta", "gamma"]
  region  = "eu-west-1"
}

data "null_data_source" "kms" {
  count = "${length(local.names)}"

  inputs = {
    key   = "${upper(local.names[count.index])}"
    value = "${format("arn:aws:kms:%s:%s:key/%s",local.region, local.account, local.names[count.index])}"
  }
}

output "debug" {
  value = "${data.null_data_source.kms.*.outputs}"
}

输出是地图列表:

data.null_data_source.kms[2]: Refreshing state...
data.null_data_source.kms[0]: Refreshing state...
data.null_data_source.kms[1]: Refreshing state...

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

debug = [
    {
        key = ALPHA,
        value = arn:aws:kms:eu-west-1:287985351234:key/alpha
    },
    {
        key = BETA,
        value = arn:aws:kms:eu-west-1:287985351234:key/beta
    },
    {
        key = GAMMA,
        value = arn:aws:kms:eu-west-1:287985351234:key/gamma
    }
]

有没有办法用所有这样的键使其成为一个映射?:

{
    ALPHA = "arn:aws:kms:eu-west-1:287985351234:key/alpha",
    BETA = "arn:aws:kms:eu-west-1:287985351234:key/beta",
    GAMMA = "arn:aws:kms:eu-west-1:287985351234:key/gamma"
}

1 个答案:

答案 0 :(得分:1)

SweetOps Slack频道上一个叫Loren的家伙给了我解决方案。我把它留给以后再来的人:

locals {
  its_a_map = "${zipmap(data.null_data_source.kms.*.outputs.key, data.null_data_source.kms.*.outputs.value)}"
}