I have a PEM file that I read using the built-in terraform file() function. When I run ‘terraform plan’ I see this (PEM file contents modified here):
key = <<-EOT
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5P0Z4OUD5PSjri8xexGo
6eQ39NGyQbXamIgWAwvnAs/oDRVqEejE2nwDhnpykaCGLkuDEN0LPd2wF+vC2Cq3
-----END PUBLIC KEY-----
EOT
What I need to see is this:
key = “-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5P0Z4OUD5PSjri8xexGo\n6eQ39NGyQbXamIgWAwvnAs/oDRVqEejE2nwDhnpykaCGLkuDEN0LPd2wF+vC2Cq3\n-----END PUBLIC KEY-----\n”
In other words one long string with ‘\n’ in place of linefeed / newline.
I figured out that I can replace this from my .tf file…
key = file(“myKey.pem”)
…with this…
key = replace(file(“myKey.pem”), “\n”, “\\n”)
…and that ALMOST gets me what I want. Unfortunately what I see now when I run ‘terraform plan’ is this:
key = “-----BEGIN PUBLIC KEY-----\\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA5P0Z4OUD5PSjri8xexGo\\n6eQ39NGyQbXamIgWAwvnAs/oDRVqEejE2nwDhnpykaCGLkuDEN0LPd2wF+vC2Cq3\\n-----END PUBLIC KEY-----\\n”
Meaning there is an unwanted EXTRA backslash preceding the ‘\n’. I have not figured out how to avoid getting this unwanted extra backslash. Does anyone know?
Thanks in advance!