Get Terraform to ignore changes to an environment variable in a k8s deployment

  Kiến thức lập trình

I have a TF resource defined as follows (example is simplified):

resource "kubernetes_deployment" "this" {
  # [... some stuff removed for simplification]
  spec {
    replicas = 3

    # [... more stuff removed]

    template {
      metadata {
        # [... removed]
      }

      spec {
        container {
          name  = local.name
          image = "some_image:some_tag"

          env {
            name  = "SOME_ENV_VARIABLE_1"
            value = "SOME_VALUE"
          }

          env {
            name  = "SOME_ENV_VARIABLE_2"
            value = "SOME_OTHER_VALUE"
          }
[...]
}

When I run the plan for this I get something like the following:

Terraform will perform the following actions:

  # module.my_module.kubernetes_deployment.this will be updated in-place
  ~ resource "kubernetes_deployment" "this" {
        id               = "some/id"
        # (1 unchanged attribute hidden)

      ~ spec {
            # (5 unchanged attributes hidden)

          ~ template {
              ~ spec {
                    # (13 unchanged attributes hidden)

                  ~ container {

                        # [some stuff removed here that isn't important]

                        # (8 unchanged attributes hidden)

                      - env {
                          - name  = "SOME_OTHER_DYNAMICALLY_CREATED_ENV_VARIABLE" -> null
                          - value = "some_value" -> null
                        }

                        # (15 unchanged blocks hidden)
                    }

I’ve been trying to figure out how I can instruct TF to ignore changes to an environment variable named SOME_OTHER_DYNAMICALLY_CREATED_ENV_VARIABLE but not other variables.

The only solution I’ve thought of would be to actually define the SOME_OTHER_DYNAMICALLY_CREATED_ENV_VARIABLE environment variable in my resource as the first env block and then add spec[0].template[0].spec[0].container[0].env[0] to my lifecycle.ignore_changes list.

But that is a very brittle solution to this if somebody accidentally adds an env block before that one.

Is there any way to specify that this environment variable specifically should be ignored?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT