user5844653
user5844653

Reputation: 45

How to use google_compute_instance_from_template?

This is my day 1 trying out terraform and needed some help.

I have a gcp instance template which I want to specify in my .tf and create an instance

resource "google_compute_instance_from_template" "tpl" {
  
  name = "k8-node-4-from-tf"
  source_instance_template = "k8-node"
  project = "myproject"
  zone = "us-west4-b"

}

I encountered this error when applying this

googleapi: Error 404: The resource 'projects/myproject/regions/us-west4/instanceTemplates/k8-node' was not found, notFound

The self link I see for the instance template is this,

 "selfLink": "projects/myproject/global/instanceTemplates/k8-node"

How can I resolve this error ? Specifying a zone on .tf is mandatory. When I try "global" for zone, this errors out : Zone global doesn't exist

Upvotes: 0

Views: 290

Answers (1)

Puteri
Puteri

Reputation: 3789

The zone refers to where the VM will be created, not where the template is.

So you can refer to the template as:

resource "google_compute_instance_from_template" "tpl" {
  
  name = "k8-node-4-from-tf"
  source_instance_template = "projects/myproject/global/instanceTemplates/k8-node"
  project = "myproject"
  zone = "us-west4-b"

}

or if the template is defined in your TF files:

resource "google_compute_instance_from_template" "tpl" {
  
  name = "k8-node-4-from-tf"
  source_instance_template = google_compute_instance_template.<your_instance_template_name>.self_link_unique
  project = "myproject"
  zone = "us-west4-b"

}

Everything is in the documentation so when you start with something new, it's highly recommended that you read that first since most of the time you'll find samples there.

Upvotes: 3

Related Questions