Terraform shows unnecessary changes due to whitespace

Viewed 2243

I have used jsonencode function to pass windows-keys in metadata of gcp compute instance.

  metadata = {
    serial-port-logging-enable = "TRUE"
    // Derived from https://cloud.google.com/compute/docs/instances/windows/automate-pw-generation
    windows-keys = jsonencode(
      {
        email    = "example.user@example.com"
        expireOn = "2020-04-14T01:37:19Z"
        exponent = "AQAB"
        modulus  = "wgsquN4IBNPqIUnu+h/5Za1kujb2YRhX1vCQVQAkBwnWigcCqOBVfRa5JoZfx6KIvEXjWqa77jPvlsxM4WPqnDIM2qiK36up3SKkYwFjff6F2ni/ry8vrwXCX3sGZ1hbIHlK0O012HpA3ISeEswVZmX2X67naOvJXfY5v0hGPWqCADao+xVxrmxsZD4IWnKl1UaZzI5lhAzr8fw6utHwx1EZ/MSgsEki6tujcZfN+GUDRnmJGQSnPTXmsf7Q4DKreTZk49cuyB3prV91S0x3DYjCUpSXrkVy1Ha5XicGD/q+ystuFsJnrrhbNXJbpSjM6sjo/aduAkZJl4FmOt0R7Q=="
        userName = "example-user"
      }
    )
  }

It is showing me unnecessary changes due to whitespace as follows:

~ "windows-keys" = jsonencode( # whitespace changes

Can someone help me with this?

1 Answers

This is most likely fixable by either fixing your line endings, or in the provider you are using (as with https://github.com/hashicorp/terraform/issues/23928). Basically, the state and the to-be-applied re-encoded data are not binary equivalent, and therefore Terraform will think that an update is needed.

Providers can be configured to ignore changes if the JSON is effectively equal, even if there are some whitespace differences.

Related