本文介绍了Terraform:如何阅读地图列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面的例子:

data "aws_kms_secrets" "api_key" {
  count = "${length(keys(var.keys))}"

  secret {
    name    = "secret_name"
    payload = "${element(values(var.keys), count.index)}"
  }
}

resource "aws_api_gateway_api_key" "access_key" {
  count = "${length(keys(var.keys))}"

  name  = "${var.environment}-${element(keys(var.keys), count.index)}"
  value = "${lookup(element(data.aws_kms_secrets.api_key.*.plaintext, count.index), "secret_name")}"
}

似乎不可能从数据资源中查找明文值.

It appears to be impossible to look up the plaintext values from the data resource.

value = "${lookup(element(data.aws_kms_secrets.api_key.*.plaintext, count.index), "secret_name")}"

lookup 中的结果:参数 1 应该是类型映射,得到类型字符串:

我尝试了很多 elementlookup* 的组合,但字典语法都不起作用.

I have tried many combinations of element,lookup,*, and dictionary syntax nothing works.

我的 var.keys 看起来像:

keys = {
  key-name-one = "sssss"
  key-name-two = "sss"
}

推荐答案

这里的技巧是使用字典语法来替换元素调用,它在映射列表中表现得更好.

The trick here is to use the dictionary syntax to replace the element call, it behaves better with lists of maps.

value = "${lookup(data.aws_kms_secrets.api_key.*.plaintext[count.index], "secret_name")}"

data.aws_kms_secrets.api_key[count.index].plaintext 是无效的 HCL

这篇关于Terraform:如何阅读地图列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:52