首页 > 解决方案 > Terraform GCP Secret Manager:对于每个多个秘密错误

问题描述

尝试在 Secret Manager、子网名称、cidr 范围、vpc 名称等中存储一些值。以下是我与 For Each 一起使用的代码。给我一个错误,我不确定我做错了什么

resource "google_secret_manager_secret" "Network" {
  provider = google-beta
  for_each = local.exports
  secret_id = each.key

  replication {
    automatic = false
  }

}

resource "google_secret_manager_secret_version" "Network-Secrets" {
  provider = google-beta
  for_each = local.exports
  secret = each.value.name
  secret_data = each.value.object
}

locals {
  exports = {
    "rd-vpc-vpc-id" = { object = module.holly.network_id, name = "rd-vpc-vpc-id" }
    "rd-subnets-subnet1-id" = { object = module.kryten_subnet.subnet_id, name = "rd-subnets-subnet1-id"  }
    "rd-subnets-subnet1-cidr" = { object = module.kryten_subnet.subnet_cidr, name = "rd-subnets-subnet1-cidr"  }
  }
}

当我做一个 terraform plan 时,我没有收到任何错误,但是在 Apply 上输入 yes 后,会出现以下错误。

Error: Error creating SecretVersion: googleapi: got HTTP response code 404 with body: <!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 404 (Not Found)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px
;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins
{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background
:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{bac
kground:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/bran
ding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/g
ooglelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>404.</b> <ins>That’s an error.</ins>
  <p>The requested URL <code>/v1beta1/rd-subnets-subnet1-cidr:addVersion?alt=json</code> was not found on this server.  <ins>That’s all we know.</ins>


  on ..\..\..\..\red-dwarf-terraform-modules\network\exports.tf line 12, in resource "google_secret_manager_secret_version" "Network-Secrets":
  12: resource "google_secret_manager_secret_version" "Network-Secrets" {

非常感谢任何帮助。

编辑:如果我使用

secret = google_secret_manager_secret.Network.id

我收到以下错误

Error: Missing resource instance key

  on ..\..\..\..\red-dwarf-terraform-modules\network\exports.tf
line 16, in resource "google_secret_manager_secret_version" "Network-Secrets":
  16:   secret = google_secret_manager_secret.Network.id

Because google_secret_manager_secret.Network has "for_each" set, its
attributes must be accessed on specific instances.

For example, to correlate with indices of a referring resource, use:
    google_secret_manager_secret.Network[each.key]

[terragrunt] 2020/07/16 22:46:43 Hit multiple errors:
exit status 1

标签: foreachterraform-provider-gcpgoogle-secret-manager

解决方案


secret参数必须是(父密钥的google_secret_manager_secret.Network.id完整资源 url)。


推荐阅读