简体   繁体   English

terraform 从地图列表中获取索引值

[英]terraform get index value from the list of maps

I want to feed each value of the vault_field for aws secret manager secret_string with this code.我想为 aws secret manager secret_string 的vault_field的每个值提供此代码。

variables.tf

variable "aws_secrets" {
  type = list(
    object({
      aws_secret_id = string,
      vault_path    = string,
      vault_field   = string,
    })
  )
  default = []
}

main.tf

data "vault_generic_secret" "aws_secrets" {
  for_each = { for idx, val in var.aws_secrets : idx => val }

  path = each.value.vault_path
}

resource "aws_secretsmanager_secret" "aws_secrets" {
  for_each                = { for idx, val in var.aws_secrets : idx => val }
  name                    = "my-secrets"
}

resource "aws_secretsmanager_secret_version" "aws_secrets" {
  for_each      = { for idx, val in var.aws_secrets : idx => val }
  secret_id     = aws_secretsmanager_secret.aws_secrets[each.key].id
  secret_string = jsonencode(data.vault_generic_secret.aws_secrets[2])
}

however I get this error但是我得到这个错误

Error: Invalid index

76:   secret_string = jsonencode(data.vault_generic_secret.aws_secrets[2])
├────────────────
│ data.vault_generic_secret.aws_secrets is object with 1 attribute "0"

The given key does not identify an element in this collection value.}

There is no need to hardcode an index of the secret.无需对秘密索引进行硬编码。 You also need to actually refer to secret_string attribute:您还需要实际参考secret_string属性:

secret_string = jsonencode(data.vault_generic_secret.aws_secrets[each.key].   data_json)

or或者

secret_string = jsonencode(data.vault_generic_secret.aws_secrets[each.key].   data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM