将环境变量列表传递给资源

Pass list of environment variables to resource

我正在尝试从我的 terragrunt.hcl 输入中传递一个列表,其中包含容器的环境变量。但是我无法让它工作。

terragrunt.hcl:

inputs = {
    containers_env = {
        "ENV_TEST" = "testing"
        "ENV_TEST_2" = "testing2"
    }
}

main.tf:

resource "kubernetes_cron_job" "cronjob" {
    metadata {
        name      = var.metadata_name
        namespace = var.metadata_namespace
    }
    spec {
        schedule                      = var.spec_schedule
        concurrency_policy            = var.spec_concurrencyPolicy
        failed_jobs_history_limit     = var.spec_failedJobsHistoryLimit
        successful_jobs_history_limit = var.spec_successfulJobsHistoryLimit
        job_template {
            metadata {}
            spec {
                template {
                    metadata {}
                    spec {
                        restart_policy                      = var.template_spec_restartPolicy
                        termination_grace_period_seconds    = var.template_spec_terminationGracePeriodSeconds
                        container {
                            name                = var.containers_name
                            image               = var.containers_image
                            image_pull_policy   = var.containers_imagePullPolicy
                            command             = var.containers_command
                            env                 = var.containers_env
                        }
                    }
                }
            }
        }
    }
}

variables.tf:

variable "containers_env" {
    description = ""
    type        = map(string)
}

我遇到错误:

Error: Unsupported argument

  on main.tf line 41, in resource "kubernetes_cron_job" "cronjob":
  41:                                                   env                             = var.containers_env

An argument named "env" is not expected here. Did you mean to define a block
of type "env"?

地形:v0.12.13 Terragrunt:v0.21.5

container 块类型似乎需要将环境变量定义为一系列嵌套的 env 块。手动写出来,它们可能看起来像这样:

env {
  name  = "ENV_TEST"
  value = "testing"
}
env {
  name  = "ENV_TEST_2"
  value = "testing2"
}

要根据var.containers_env动态生成这些块,我们可以使用dynamic block,以var.containers_env作为其重复表达式:

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