创建实例模板时 Terraform GCP,获取源图像的​​相对路径时出错

Terraform GCP when creating instance template, Error getting relative path for source image

我在设置 GCP 实例模板时遇到了新问题。我假设 terraform gcp 提供程序有更新。

resource "google_compute_instance_template" "backend-template" {
  name                    = "${var.platform_name}-backend-instance-template"
  description             = "Template used for backend instances"
  instance_description    = "backend Instance"
  machine_type            = "n1-standard-1"
  metadata_startup_script = "${lookup(var.startup_scripts,"backend-server")}"

disk {
  boot         = "true"
  source_image = "backend-packer-image"
}

metadata {
  APP_SETTINGS        = "${var.app_settings}"
  URL_STAGING         = "${var.url_staging}"
  API_URL_STAGING     = "${var.api_url_staging}"
  URL_PRODUCTION      = "${var.url_production}"
  API_URL_PRODUCTION  = "${var.api_url_production}"
  LOGIN_URL           = "${var.login_url}"
  API_URL             = "${var.api_url}"
  vault_server_IP     = "${lookup(var.static_ips, "vault-server")}"
  environment         = "${var.environment}"
}

network_interface {
  subnetwork = "${google_compute_subnetwork.private-fe-be.self_link}"
}

lifecycle {
  create_before_destroy = true
}

tags = ["no-ip", "backend-server"]

service_account {
  scopes = ["cloud-platform"]
}
}

这是 运行 脚本后的当前错误。但是,映像 backend-packer-image 已创建并存在于 GCP

* google_compute_instance_template.backend-template: 1 error(s) occurred:

* google_compute_instance_template.backend-template: error flattening disks: Error getting relative path for source image: String was not a self link: global/images/backend-packer-image

我今天遇到了完全相同的问题,我不得不直接查看拉取请求以找到正确使用它的方法。

所以,我带来的是: 在输入此命令之前,您必须首先确保在您的项目中,否则如果它是自定义图像,您将找不到您要查找的图像:

gcloud compute images list --uri | grep "your image name"

像这样,您将拥有图片的 uri,然后您可以将其完全放入图片中,它会起作用。

将图像名称替换为 source_image

上的 URI
resource "google_compute_instance_template" "backend-template" {
  name                    = "${var.platform_name}-backend-instance- 
  template"
  description             = "Template used for backend instances"
  instance_description    = "backend Instance"
  machine_type            = "n1-standard-1"
  metadata_startup_script = "${lookup(var.startup_scripts,"backend-server")}"

  disk {
  boot         = "true"
  source_image = "https://www.googleapis.com/compute/v1/projects/<project-name>/global/images/backend-packer-image"
}

metadata {
  APP_SETTINGS        = "${var.app_settings}"
  URL_STAGING         = "${var.url_staging}"
  API_URL_STAGING     = "${var.api_url_staging}"
  URL_PRODUCTION      = "${var.url_production}"
  API_URL_PRODUCTION  = "${var.api_url_production}"
  LOGIN_URL           = "${var.login_url}"
  API_URL             = "${var.api_url}"
  vault_server_IP     = "${lookup(var.static_ips, "vault-server")}"
  environment         = "${var.environment}"
}

network_interface {
  subnetwork = "${google_compute_subnetwork.private-fe-be.self_link}"
}

lifecycle {
  create_before_destroy = true
}

tags = ["no-ip", "backend-server"]

service_account {
  scopes = ["cloud-platform"]
}
}

也可以将 terraform 脚本绑定到 运行 以前的版本

provider "google"{
  version     = "<= 1.17"
  credentials = "${var.service_account_path}"
  project     = "${var.gcloud_project}"
  region      = "${var.gcloud_region}"
}