Trailing new line in key vault after using heredoc syntax

we are trying to create keyvault secret using Terraform at runtime and we after building the code, it looks something like this

resource “azurerm_key_vault_secret” “AggregatorsKeySpaceName” {
name = “AggregatorsKeySpaceName”
value = <<EOT
KV_SECRET_VALUE
EOT
key_vault_id = azurerm_key_vault.keyvault.id
}

the issue is that, heredoc syntax adds a trailing new line at the end of the “value”, hence the value is stored something like this

image

As you can in the image, the secret value 5 is trailed with new line and it kind of changes the meaning of the secret value in case of passwords and other KV secret stored.

  • Use the trimspace function on the heredoc result to remove the trailing newline (which will also remove any leading whitespace):

value = trimspace(
<<-EOT
KV_SECRET_VALUE
EOT
)

  • Use the chomp function, which is a more specialized alternative to trimspace that only removes newline characters from the end of the string, leaving other leading and trailing whitespace intact.

value = chomp(
<<-EOT
KV_SECRET_VALUE
EOT
)

3 Likes