繁体   English   中英

如何使用 terraform 在 AWS 机密管理器中创建许多机密

[英]How to create many secrets in AWS secrets manager using terraform

我想要做的是将密钥名称列表提供给一个模块,该模块将用于在秘密管理器中生成许多具有不同随机密码的秘密。

我尝试了很多不同的事情,但到目前为止都失败了。

这是我目前拥有的:

module "secrets-manager-1" {

  source = "lgallard/secrets-manager/aws"

  for_each = var.list
  secrets = {
    "${each.value}" = {
      description             = each.value
      recovery_window_in_days = 7
      secret_string           = random_password.special_password.result
    }
  }

  tags = var.standard_tags
}

resource "random_password" "special_password" {
  count = 2
  length = 16
  special = true
}

variable "list" {
  type    = list(string)
  default = [
    "secret_key_1",
    "secret_key_2"

  ]
}

错误:

│ 错误:for_each 参数无效│ │ on....\modules\jitsi\jitsi_secrets.tf 第 54 行,在模块“secrets-manager-1”中:│ 54:for_each = var.list │ ├────── ────────── │ │ var.list 是包含 2 个元素的字符串列表 │ │ 给定的“for_each”参数值不合适:“for_each”参数必须是 map 或字符串集,并且您提供了一个字符串类型列表的值。 ╵ 释放 state 锁。 这可能需要一些时间...

不幸的是,您提供的甚至不是有效的 Terraform 代码。 我相信您希望实现以下目标:

// Create N random password. In this case N = 2
resource "random_password" "special_password" {
  count   = 2
  length  = 16
  special = true
}

// Import a third party module
module "secrets-manager-1" {

  source = "lgallard/secrets-manager/aws"

  // Loop through the random_passowrd resouces and create the secrets
  secrets = {
      for index, pwd in random_password.special_password.*.result : "secret_key_${index}" => {
          secret_string: "${pwd}",
          recovery_window_in_days = 7
      }
  }
}

您可能需要检查splat 表达式,以便能够迭代多个资源。 这用于secrets-manager-1模块中的for表达式。

暂无
暂无

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

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