【问题标题】:Terraform GCP when creating instance template, Error getting relative path for source image创建实例模板时 Terraform GCP,获取源图像的​​相对路径时出错
【发布时间】:2019-02-23 11:23:39
【问题描述】:

我在设置 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

【问题讨论】:

  • 看起来这是一个已知问题,正在github.com/terraform-providers/terraform-provider-google/issues/…进行跟踪
  • @ydaetskcoR 谢谢,到目前为止,它确实有一天的历史。几个小时前它已经工作了,我也没有任何改变。
  • 1.17.1 中可以正常工作吗? changelog 中也有关于此的注释,但除非有错误,否则它似乎不应该适用于此。我也有点惊讶,因为 v2 版本没有被阻止,因为它看起来对我来说是一个重大变化(即使只是为了某些配置),但我不太了解 GCP 提供程序。

标签: google-cloud-platform terraform devops terraform-provider-gcp


【解决方案1】:

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

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

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"]
}
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-09-26
      • 2022-08-02
      • 1970-01-01
      • 2021-04-10
      • 2015-03-25
      • 2020-12-17
      • 2018-09-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多