Use Terraform to create a Compute Engine instance with a persistent disk

## Code sample

### Terraform


To learn how to apply or remove a Terraform configuration, see
[Basic Terraform commands](https://docs.cloud.google.com/docs/terraform/basic-commands).


For more information, see the
[Terraform provider reference documentation](https://registry.terraform.io/providers/hashicorp/google/latest/docs).

    resource "google_project_service" "compute_api" {
      service            = "compute.googleapis.com"
      disable_on_destroy = false
    }

    # Using pd-standard because it's the default for Compute Engine

    resource "google_compute_disk" "default" {
      name = "disk-data"
      type = "pd-standard"
      zone = "us-west1-a"
      size = "5"
    }

    resource "google_compute_instance" "test_node" {
      name         = "test-node"
      machine_type = "f1-micro"
      zone         = "us-west1-a"

      boot_disk {
        initialize_params {
          image = "debian-cloud/debian-13"
        }
      }
      attached_disk {
        source      = google_compute_disk.default.id
        device_name = google_compute_disk.default.name
      }

      network_interface {
        network = "default"
        access_config {
          # Ephemeral IP
        }
      }

      # Ignore changes for persistent disk attachments
      lifecycle {
        ignore_changes = [attached_disk]
      }


    }

## What's next


To search and filter code samples for other Google Cloud products, see the
[Google Cloud sample browser](https://docs.cloud.google.com/docs/samples?product=computeengine).