1

I have a new issue with setting up GCP instance template. I am presuming there was an update on the terraform gcp provider.

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

This is the current error after running the script. However, the image backend-packer-image was already created and exists on 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
3
  • Looks like it's a known issue and is being tracked at github.com/terraform-providers/terraform-provider-google/issues/… Commented Sep 19, 2018 at 10:08
  • @ydaetskcoR Thanks, it is indeed as old as one day so far. It had worked a few hours before and I also had nothing changed. Commented Sep 19, 2018 at 10:16
  • 1
    Does it work okay in 1.17.1? There's also a note in the changelog about this but doesn't look like it should apply here unless there's a mistake. I'm also a little bit surprised that wasn't held back for a v2 release as it looks to be a breaking change to me (even if only supposedly for some configurations) but I don't know the GCP provider that well. Commented Sep 19, 2018 at 10:17

2 Answers 2

1

I had the exact same problem today, I had to go look directly into the pull request to find a way to use this correctly.

So, what I came with is this: you must first be sure to be in your project before typing this command or you won't find the image you are looking for if it's a custom one:

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

Like this you will have the uri of your image, you can then put it fully for the image and it will work.

Replace the image name with the URI on source_image

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"]
}
}
Sign up to request clarification or add additional context in comments.

Comments

1

It is also possible to tie the terraform scripts to run previous versions

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.