You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform多行私钥base64编解码含Heredoc标记的解决方法

解决Terraform中私钥变量编码解码后包含Heredoc标记的问题

问题原因

你的变量定义中,Heredoc的结束标记EOT可能未顶格书写,导致Terraform将其识别为变量内容的一部分;或者变量值首尾存在多余空白字符,编码后解码时带出不必要的内容。

修正方案

1. 规范Heredoc格式

确保Heredoc的结束标记EOT完全顶格,无任何缩进或空格。正确的变量定义如下:

variable "oci_prv_key" {
  type = string
  default = <<EOT
-----BEGIN PRIVATE KEY-----
xxxxxxxxx
xxxxxxxx
xxxxxxxxx
xxxxxxxxx
-----END PRIVATE KEY-----
EOT
}

2. 编码前去除多余空白

如果变量值首尾存在换行或空格,可使用trimspace()函数清理后再编码,避免多余内容被带入:

locals {
  encoded_private_key = base64encode(trimspace(var.oci_prv_key))
}

验证解码结果

使用base64decode()解码上述编码后的变量,即可得到不含Heredoc标记的纯净私钥内容:

output "decoded_private_key" {
  value = base64decode(local.encoded_private_key)
}

内容的提问来源于stack exchange,提问作者Ahmed Al-Haffar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 05:26:00