How to create an "instance template" from an existing GCE instance with Terraform

Hi,

Is it possible to create an “instance template” from a currently running GCE’s boot disk ?

I am creating an instance with terraform… with “provisioner” i am copying a script to the instance and with “ssh connection” i am connecting to the instance and executing this script to make some package installations . What I want to do is, to create an instance template by using this newly created instance’s bootdisk and finally i will create a managed instance group… Is there a way of creating an instance template by using this newly created instance ?

Here is the code i tried, but getting the following error, so it did not work :

Error: Error creating Image: googleapi: Error 400: The disk resource ‘projects/my-sandbox/zones/us-west1-a/disks/etcd-1’ is already being used by ‘projects/my-sandbox/zones/us-west1-a/instances/etcd-1’, resourceInUseByAnotherResource

on main.tf line 295, in resource “google_compute_image” “example”:

295: resource “google_compute_image” “example” {

provider “google” {
region = “{var.region}" project = "{var.project_name}”
credentials = “{file("{var.credentials_file_path}”)}"
}

resource “google_compute_instance” “etcdinstance1” {

name = “etcd-1”
machine_type = “{var.machine_type}" zone = "{var.region}-a”
tags = [“etcd-node”,“int-lb”,“allow-ssh”]

boot_disk {
initialize_params {
image = “${var.os_image}”
}
}

network_interface {
network = “{var.network_name}" subnetwork = "{var.subnetwork_name}”

access_config {
  # Ephemeral
}

}

service_account {
scopes = [“https://www.googleapis.com/auth/compute.readonly”]

Sounds like a job for Packer (https://www.packer.io/).

1 Like

yes, you are right I think