Terraform - Error: [DEBUG] Create SSH Key illegal base64 data at input byte 1

Terraform - Error: [DEBUG] Create SSH Key illegal base64 data at input byte 1

我正在尝试 运行 IBM 云上的以下代码来配置不同的资源,所有资源都已创建,但 VSI/VM 实例的 public 键有问题但不确定发生了什么,这是代码 test.tf:

# Configure the IBM Cloud Provider
provider "ibm" {
  ibmcloud_api_key      = "${var.ibmcloud_api_key}"
  generation            = 2
  region                = "us-south"

}



###################Reources###################

#VPC
resource "ibm_is_vpc" "vpc1" {
  name                  = "vpc1"
}


#Subnet for the VPC
resource "ibm_is_subnet" "subnet1" {
  name            = "subnet1"
  vpc             = ibm_is_vpc.vpc1.id
  zone            = "${var.zone1}"
  ipv4_cidr_block = "10.240.0.0/24"
}


#Second Subnet for bastion VSI
resource "ibm_is_subnet" "subnet2" {
  name            = "subnet2"
  vpc             = ibm_is_vpc.vpc1.id
  zone            = "${var.zone1}"
  ipv4_cidr_block = "10.240.1.0/24"
  public_gateway = "${ibm_is_public_gateway.gateway.id}"
}


#Public Gateway
resource "ibm_is_public_gateway" "gateway" {
  name = "gateway"
  vpc  = ibm_is_vpc.vpc1.id
  zone = "${var.zone1}"
}


#data SSH
resource "ibm_is_ssh_key" "ssh_public_key" {
    name = "testssh"
    public_key = var.ssh_public_key
}


#VSI

resource "ibm_is_instance" "vm1" {
  name              = "vm1"
  image             = "${var.image}"
  profile           = "${var.profile}"
  zone              = "${var.zone1}"
  keys              = [ibm_is_ssh_key.ssh_public_key.id]
  vpc               = ibm_is_vpc.vpc1.id

  primary_network_interface {
    subnet          = ibm_is_subnet.subnet2.id
  }

  network_interfaces {
    name            = "eth1"
    subnet          = ibm_is_subnet.subnet2.id

  }

  }

这里是变量文件 variables.tf:

#variables

#API Key top connect to my IBM Cloud
variable "ibmcloud_api_key" {
    default = "9lsRdBjb70PlwxxxxxxxxxxxxxxxxxxxLdf6"
}


##What zone I want to use
#IBMcloud regions would help to get the regions/zones
variable "zone1" {
    default = "us-south-1"

}


#SVSI image template
#ibmcloud is image command
variable "image" {
    default = "6aec77ca-ab4a-459e-81dc-6e5ec9f99d4a" #centos minimal

}


#SSH key for the VMs/VSIs for provisioning
variable "ssh_public_key" {
    default = "C:/Users/User.Name/ibmkey.pub"

}


#VSI config
#ibmcloud is instance-profiles command
variable "profile" {
    default = "bc1-2x8" #2CPUs and 8GB of RAM

}

这是 运行 应用后的错误输出:

Error: [DEBUG] Create SSH Key illegal base64 data at input byte 1
{
    "StatusCode": 400,
    "Headers": {
        "Cache-Control": [
            "max-age=0, no-cache, no-store, must-revalidate"
        ],
        "Cf-Cache-Status": [
            "DYNAMIC"
        ],
        "Cf-Ray": [
            "5b8ab320e9c4b959-MIA"
        ],
        "Cf-Request-Id": [
            "042a8e48910000b959aa8fe200000001"
        ],
        "Connection": [
            "keep-alive"
        ],
        "Content-Length": [
            "187"
        ],
        "Content-Security-Policy": [
            "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval' https://ssl.google-analytics.com https://assets.zendesk.com https://connect.facebook.net; img-src 'self' https://ssl.google-analytics.com https://s-static.ak.facebook.com https://assets.zendesk.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com https://assets.zendesk.com; font-src 'self' https://themes.googleusercontent.com; frame-src https://assets.zendesk.com https://www.facebook.com https://s-static.ak.facebook.com https://tautt.zendesk.com; object-src 'none'"
        ],
        "Content-Type": [
            "application/json; charset=utf-8"
        ],
        "Date": [
            "Sun, 26 Jul 2020 02:30:37 GMT"
        ],
        "Expect-Ct": [
            "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\""
        ],
        "Expires": [
            "-1"
        ],
        "Pragma": [
            "no-cache"
        ],
        "Server": [
            "cloudflare"
        ],
        "Set-Cookie": [
            "__cfduid=dab8eaaa41dc1d2e24658e3191d0e3d881595730636; expires=Tue, 25-Aug-20 02:30:36 GMT; path=/; domain=.iaas.cloud.ibm.com; HttpOnly; SameSite=Lax; Secure"
        ],
        "Strict-Transport-Security": [
            "max-age=31536000; includeSubDomains"
        ],
        "Vary": [
            "Accept-Encoding"
        ],
        "X-Content-Type-Options": [
            "nosniff"
        ],
        "X-Request-Id": [
            "7ff3ada5-02e8-4fb2-a1f2-5fa9ca4da415"
        ],
        "X-Trace-Id": [
            "7108b437f9d18820"
        ],
        "X-Xss-Protection": [
            "1; mode=block"
        ]
    },
    "Result": {
        "errors": [
            {
                "code": "key_parse_failure",
                "message": "illegal base64 data at input byte 1",
                "target": {
                    "name": "key.public_key",
                    "type": "field"
                }
            }
        ],
        "trace": "7ff3ada5-02e8-4fb2-a1f2-5fa9ca4da415"
    },
    "RawResult": null
}


  on test1.tf line 51, in resource "ibm_is_ssh_key" "ssh_public_key":
  51: resource "ibm_is_ssh_key" "ssh_public_key" {

有什么想法吗???

我认为这里发生的事情是您将 SSH public 密钥指定为文件名而不是实际的 public 密钥定义,因此提供者正在发送文字字符串 C:/Users/User.Name/ibmkey.pub 作为您的密钥,而不是该文件的内容。

我对这个提供商还不够熟悉,无法确定,但我认为它希望您已经阅读过该文件并将其 内容 作为 [= ibm_is_ssh_key.

的参数 12=]

此处如何进行的两个主要选项是在设置 ssh_public_key 变量时传递文件的内容,使调用者有责任首先将文件读入内存,或者更改您的模块以将给定文件读入内存本身:

resource "ibm_is_ssh_key" "ssh_public_key" {
    name       = "testssh"
    public_key = file(var.ssh_public_key)
}

According to the provider source code,此错误消息的大部分是直接从远程 API 返回的,因此如果读取文件也不起作用,您可能需要查阅相关文档 POST /keys API 操作以了解其 key.public_key 参数期望的格式,然后在您的 Terraform 配置中匹配该格式。