Use Terraform to create a Compute Engine instance with configured scripts to run at instance shutdown

## 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_compute_instance" "default" {
      name         = "instance-name-shutdown-content-directly"
      machine_type = "f1-micro"
      zone         = "us-central1-c"
      metadata = {
        # Shuts down Apache server
        shutdown-script = "#! /bin/bash /etc/init.d/apache2 stop"
      }
      boot_disk {
        initialize_params {
          image = "debian-cloud/debian-13"
        }
      }
      network_interface {
        # A default network is created for all Google Cloud projects
        network = "default"
        access_config {
        }
      }
    }


    resource "google_compute_instance" "shutdown_content_from_file" {
      name         = "instance-name-shutdown-content-from-file"
      machine_type = "f1-micro"
      zone         = "us-central1-c"
      metadata = {
        # Shuts down Apache server
        shutdown-script = file("${path.module}/shutdown-script.sh")
      }
      boot_disk {
        initialize_params {
          image = "debian-cloud/debian-13"
        }
      }
      network_interface {
        # A default network is created for all Google Cloud projects
        network = "default"
        access_config {
        }
      }
    }

## 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).