我试图在我创建的云运行模块上设置多个环境变量。我在Terraform中使用的例子是静态的。是否有可能动态地创建这些?
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范例用法——cloud-run-service-multiple-environment-variables
我试过:
dynamic "env" {
for_each = var.envs
content {
name = each.key
value = each.value
}
}
但是我得到以下错误:
编辑:完整的代码示例对"each.value"已在不可用的上下文中使用,例如当配置不再包含中的值时│its "for_each"表达式。删除对每一个的引用。值来解决此错误。
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
}
当您使用动态块时,不能使用each
. 应该是:
dynamic "env" {
for_each = var.envs
content {
name = env.key
value = env.value
}
}