Terraform: Google Cloud Instance Startup Scripts & Provisioning with Compute Templates

Created on 12 Dec 2016  ยท  3Comments  ยท  Source: hashicorp/terraform

So I'm familiar with Terraform and have built environments on AWS with it. I have a new project on google cloud and I'm having an issue (or misunderstanding) on how to get my provisioning scripts/steps into an autoscaling group.

Terraform Version

Terraform v0.7.13

Affected Resource(s)

  • google_compute_instance_template

Terraform Configuration Files

resource "google_compute_instance_template" "events_service_template" {
  name = "${var.environment}-events-service-template"
  machine_type = "${var.machine_type}"
  can_ip_forward = false

  tags = []

  disk {
    source_image = "ubuntu-os-cloud/ubuntu-1404-lts"
    disk_type = "pd-ssd"
    disk_size_gb = "30"
    auto_delete = true
    boot = true
  }

  network_interface {
    network = "default"
  }

  metadata {
    ssh-keys = "root:${file("${var.public_key_path}")}"
  }

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

  provisioner "file" {
    source = "scripts/startup.sh"
    destination = "/root/startup.sh"

    connection {
      type = "ssh"
      user = "root"
      private_key = "${file("${var.private_key_path}")}"
      agent = false
    }
  }

  provisioner "remote-exec" {
    connection {
      type = "ssh"
      user = "root"
      private_key = "${file("${var.private_key_path}")}"
      agent = false
    }

    inline = [
      "chmod +x /root/startup.sh",
      "/root/startup.sh"
    ]
  }
}

Output

Error applying plan:

1 error(s) occurred:

* ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

References

I used a modified version of the google two-tier example.

bug providegoogle-cloud

Most helpful comment

I think what people were trying to do is:

  1. Define a VM template using google_compute_instance_template.
  2. Use provisioners defined in google_compute_instance_template to provision a VM image as the image for instance group.
  3. Use this template (and image) with google_compute_instance_group_manager to deploy VMs in an instance group. This will allow you to take advantage of the autoscaling feature.

This is simply not possible right now. But, it makes sense to have feature like this.

All 3 comments

google_compute_instance_template is a resource to define a template, not an VM. google_compute_instance is a resource to create a VM.
You're referring to google two-tier example, it uses google_compute_instance resource - https://github.com/hashicorp/terraform/blob/master/examples/google-two-tier/main.tf#L30

I think what people were trying to do is:

  1. Define a VM template using google_compute_instance_template.
  2. Use provisioners defined in google_compute_instance_template to provision a VM image as the image for instance group.
  3. Use this template (and image) with google_compute_instance_group_manager to deploy VMs in an instance group. This will allow you to take advantage of the autoscaling feature.

This is simply not possible right now. But, it makes sense to have feature like this.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

oillio picture oillio  ยท  78Comments

kklipsch picture kklipsch  ยท  95Comments

radeksimko picture radeksimko  ยท  80Comments

ncraike picture ncraike  ยท  77Comments

nevir picture nevir  ยท  82Comments