简体   繁体   English

Terraform - 云运行列表中的多个环境变量

[英]Terraform - cloud run multiple environment variables from list

I'm trying to set multiple environment variables on a cloud run module I've created.我正在尝试在我创建的云运行模块上设置多个环境变量。 The example I'm following from Terraform is static. Is it possible to dynamically create these?我从 Terraform 开始的示例是 static。是否可以动态创建这些?

template {
    spec {
      containers {
        image = "us-docker.pkg.dev/cloudrun/container/hello"
        env {
          name = "SOURCE"
          value = "remote"
        }
        env {
          name = "TARGET"
          value = "home"
        }
      }
    }
  }

https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_service#example-usage---cloud-run-service-multiple-environment-variables https://registry.terraform.io/providers/hashicorp/google/latest/docs/resources/cloud_run_service#example-usage---cloud-run-service-multiple-environment-variables

I've tried:我试过了:

dynamic "env" {
   for_each = var.envs
   content {
     name  = each.key
     value = each.value
   }
}

But I get the following error:但我收到以下错误:

A reference to "each.value" has been used in a context in which it unavailable, such as when the configuration no longer contains the value in │ its "for_each" expression.对“each.value”的引用已在其不可用的上下文中使用,例如当配置不再包含其“for_each”表达式中的值时。 Remove this reference to each.value in your configuration to work around this error.在您的配置中删除对 each.value 的引用以解决此错误。

Edit: Full code example编辑:完整代码示例

resource "google_cloud_run_service" "default" {
  name     = "cloudrun-srv"
  location = "us-central1"

  template {
    spec {
      containers {
        image = "us-docker.pkg.dev/cloudrun/container/hello"
        env {
          name = "SOURCE"
          value = "remote"
        }
        env {
          name = "TARGET"
          value = "home"
        }
      }
    }
  }

  traffic {
    percent         = 100
    latest_revision = true
  }
  autogenerate_revision_name = true
}

When you use dynamic blocks, you can't use each .使用动态块时,不能使用each It should be:它应该是:

dynamic "env" {
   for_each = var.envs
   content {
     name  = env.key
     value = env.value
   }
}

Can you please help me with reading secrets from secret manager using dynamic block?你能帮我使用动态块从秘密管理器中读取秘密吗?

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

相关问题 AWS Codebuild 中的多个环境变量与 Terraform - Multiple Environment Variables in AWS Codebuild with Terraform Cloud Run Build 期间未定义环境变量 - Environment variables are undefined during Cloud Run Build 如何使用 Terraform 将值列表传递到 Lambda 环境变量 - How to pass a list of values into Lambda environment variables with Terraform 有没有办法在 terraform locals 中连接多个列表变量 - is there a way to concatenate multiple list variables in terraform locals 是否可以在 Google Cloud Run 中添加依赖环境变量? - Is it possible to add dependent environment variables in Google Cloud Run? Next.js 部署到 Google Cloud Run 时未定义的环境变量 - Environment variables undefined on Next.js deployment to Google Cloud Run 列表中的 Escaping 字符作为环境变量发送到 GCP Cloud Run - Escaping characters in a list sent as environment variable to GCP Cloud Run Terraform 云运行触发器 Azure - Terraform cloud run triggers with Azure 更新 lambda 环境变量(由 terraform 生成) - Update lambda environment variables (generated by terraform) Terraform - 如何将环境变量传递给 terraform 中的子模块 - Terraform - How to pass environment variables to sub modules in terraform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM