Search code examples
google-cloud-platformterraformdevopsterraform-provider-gcp

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


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

Solution

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