Search code examples
gitterraformheredocline-endingsterraform-template-file

Terraform keeps changing line endings of the multi-line heredoc depending on the runtime environment


I have this terraform resource (a bit simplified for clarity):

resource "azurerm_key_vault_secret" "env_secrets" {
  name         = "my-secret"
  key_vault_id = var.key_vault_id

  value = <<-EOT
  {
    "ADMIN_USER": "admin",
    "ADMIN_PASSWORD": "some_secret",
  }
  EOT

  content_type = "application/x-json"
}

What happens is that depending on where the terraform is run (on WSL2 on Windows or on Ubuntu in the deploy pipeline) the line ending change back and forth from \n to \r\n meaning there is all the time a "change" that should be applied which is not ideal.

Is there any good way to fix it? I assume perhaps a hard conversion to \n, or removal of \r or something like that. Maybe there are some standard ways of fixing this?

P.S. I assume that different line-endings happen because of git, but seems like the correct way on how git behaves so it should probably be fixed in terraform.


Solution

  • That's what I ended up doing:

    locals {
      value_raw = <<-EOT
      {
        "ADMIN_USER": "admin",
        "ADMIN_PASSWORD": "some_secret",
      }
      EOT
      value = chomp(replace(local.value_raw, "\r\n", "\n"))
    }
    
    resource "azurerm_key_vault_secret" "env_secrets" {
    
      value = local.value
    }