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

时间:2018-09-19 10:02:52

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

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

2 个答案:

答案 0 :(得分:1)

今天我遇到了完全相同的问题,我不得不直接研究pull请求,以找到正确使用此方法的方法。

所以,我附带的是:  您必须先确定要进入项目,然后再键入此命令,否则,如果它是自定义图片,则找不到所需的图像:

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

答案 1 :(得分:1)

还可以将terraform脚本绑定为运行以前的版本

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