google_compute_instance

Manages a VM instance resource within GCE. For more information see the official documentation and API.

Example Usage

resource "google_service_account" "default" {
  account_id   = "my-custom-sa"
  display_name = "Custom SA for VM Instance"
}

resource "google_compute_instance" "default" {
  name         = "my-instance"
  machine_type = "n2-standard-2"
  zone         = "us-central1-a"

  tags = ["foo", "bar"]

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
      labels = {
        my_label = "value"
      }
    }
  }

  // Local SSD disk
  scratch_disk {
    interface = "NVME"
  }

  network_interface {
    network = "default"

    access_config {
      // Ephemeral public IP
    }
  }

  metadata = {
    foo = "bar"
  }

  metadata_startup_script = "echo hi > /test.txt"

  service_account {
    # Google recommends custom service accounts that have cloud-platform scope and permissions granted via IAM Roles.
    email  = google_service_account.default.email
    scopes = ["cloud-platform"]
  }
}

Argument Reference

The following arguments are supported:



The boot_disk block supports:

The initialize_params block supports:

The scratch_disk block supports:

The attached_disk block supports:

The network_performance_config block supports:

The network_interface block supports:

The access_config block supports:

The ipv6_access_config block supports:

The alias_ip_range block supports:

The service_account block supports:

The scheduling block supports:

The guest_accelerator block supports:

The node_affinities block supports:

The params block supports:

The shielded_instance_config block supports:

The confidential_instance_config block supports:

The advanced_machine_features block supports:

The reservation_affinity block supports:

The specific_reservation block supports:

Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:

Timeouts

This resource provides the following Timeouts configuration options: configuration options:

Import

Instances can be imported using any of these accepted formats:

In Terraform v1.5.0 and later, use an import block to import instances using one of the formats above. For example:

import {
  id = "projects/{{project}}/zones/{{zone}}/instances/{{name}}"
  to = google_compute_instance.default
}

When using the terraform import command, instances can be imported using one of the formats above. For example:

$ terraform import google_compute_instance.default projects/{{project}}/zones/{{zone}}/instances/{{name}}
$ terraform import google_compute_instance.default {{project}}/{{zone}}/{{name}}
$ terraform import google_compute_instance.default {{name}}