<p>This should be fairly easy, or I might doing something wrong, but after a while digging into it I couldn't find a solution.</p>
<p>I have a Terraform configuration that contains a <a href="https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/secret" rel="nofollow noreferrer">Kubernetes Secret</a> resource which data comes from Vault. The resource configuration looks like this:</p>
<pre><code>resource &quot;kubernetes_secret&quot; &quot;external-api-token&quot; {
  metadata {
    name      = &quot;external-api-token&quot;
    namespace = local.platform_namespace
    annotations = { 
      &quot;vault.security.banzaicloud.io/vault-addr&quot; = var.vault_addr
      &quot;vault.security.banzaicloud.io/vault-path&quot; = &quot;kubernetes/${var.name}&quot;
      &quot;vault.security.banzaicloud.io/vault-role&quot; = &quot;reader&quot;
    }   
  }

  data = { 
    &quot;EXTERNAL_API_TOKEN&quot; = &quot;vault:secret/gcp/${var.env}/micro-service#EXTERNAL_API_TOKEN&quot;
  }
}
</code></pre>
<p>Everything is working fine so far, but every time I do <code>terraform plan</code> or <code>terraform apply</code>, it marks that resource as &quot;changed&quot; and updates it, even when I didn't touch the resource or other resources related to it. E.g.:</p>
<pre><code>... (other actions to be applied, unrelated to the offending resource) ...


  # kubernetes_secret.external-api-token will be updated in-place
  ~ resource &quot;kubernetes_secret&quot; &quot;external-api-token&quot; {
      ~ data = (sensitive value)
        id   = &quot;platform/external-api-token&quot;
        type = &quot;Opaque&quot;

        metadata {
            annotations      = {
                &quot;vault.security.banzaicloud.io/vault-addr&quot; = &quot;https://vault.infra.megacorp.io:8200&quot;
                &quot;vault.security.banzaicloud.io/vault-path&quot; = &quot;kubernetes/gke-pipe-stg-2&quot;
                &quot;vault.security.banzaicloud.io/vault-role&quot; = &quot;reader&quot;
            }
            generation       = 0
            labels           = {}
            name             = &quot;external-api-token&quot;
            namespace        = &quot;platform&quot;
            resource_version = &quot;160541784&quot;
            self_link        = &quot;/api/v1/namespaces/platform/secrets/external-api-token&quot;
            uid              = &quot;40e93d16-e8ef-47f5-92ac-6d859dfee123&quot;
        }
    }

Plan: 3 to add, 1 to change, 0 to destroy.
</code></pre>
<p>It is saying that the data for this resource has been changed. However the data in Vault remains the same, nothing has been modified there. This update happens every single time now.</p>
<p>I was thinking on to use the <a href="https://www.terraform.io/language/meta-arguments/lifecycle#ignore_changes" rel="nofollow noreferrer"><code>ignore_changes</code></a> lifecycle feature, but I assume this will make any changes done in Vault secret to be ignored by Terraform, which I also don't want. <strong>I would like the resource to be updated only when the secret in Vault was changed.</strong></p>
<p>Is there a way to do this? What am I missing or doing wrong?</p>

<p>You need to add in the Terraform Lifecycle ignore changes meta argument to your code. For data with API token values but also annotations for some reason Terraform seems to assume that, that data changes every time a plan or apply or even destroy has been run. I had a similar issue with Azure KeyVault.</p>
<p>Here is the code with the lifecycle ignore changes meta argument included:</p>
<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-html lang-html prettyprint-override"><code>resource "kubernetes_secret" "external-api-token" {
  metadata {
    name      = "external-api-token"
    namespace = local.platform_namespace
    annotations = { 
      "vault.security.banzaicloud.io/vault-addr" = var.vault_addr
      "vault.security.banzaicloud.io/vault-path" = "kubernetes/${var.name}"
      "vault.security.banzaicloud.io/vault-role" = "reader"
    }   
  }

  data = { 
    "EXTERNAL_API_TOKEN" = "vault:secret/gcp/${var.env}/micro-service#EXTERNAL_API_TOKEN"
  }

lifecycle {
    ignore_changes = [
      # Ignore changes to data, and annotations e.g. because a management agent
      # updates these based on some ruleset managed elsewhere.
      data,annotations,
    ]
  }
}</code></pre>
</div>
</div>
</p>
<p>link to meta arguments with lifecycle:</p>
<p><a href="https://www.terraform.io/language/meta-arguments/lifecycle" rel="nofollow noreferrer">https://www.terraform.io/language/meta-arguments/lifecycle</a></p>
