Provision Compute Engine resources with Terraform

HashiCorp Terraform is an infrastructure-as-code (IaC) tool that lets you provision and manage cloud infrastructure. Terraform provides plugins calledproviders that let you interact with cloud providers and other APIs. You can use theTerraform provider for Google Cloud to provision and manage Google Cloud resources, including Compute Engine.

This page introduces you to using Terraform with Compute Engine, including an introduction to how Terraform works and some resources to help you get started using Terraform with Google Cloud. You'll also find links to Terraform reference docs for Compute Engine, code examples, and guides for using Terraform to provision Compute Engine resources.

For instructions on how to get started with Terraform for Google Cloud, seeInstall and configure Terraform or theTerraform for Google Cloud quickstart.

How Terraform works

Terraform has a declarative and configuration-oriented syntax, which you can use to describe the infrastructure that you want to provision in your Google Cloud project. After you author this configuration in one or more Terraform configuration files, you can use the Terraform CLI to apply this configuration to your Compute Engine resources.

The following steps explain how Terraform works:

  1. You describe the infrastructure you want to provision in aTerraform configuration file. You don't need to write code describing how to provision the infrastructure. Terraform provisions the infrastructure for you.
  2. You run theterraform plan command, which evaluates your configuration and generates an execution plan. You can review the plan and make changes as needed.
  3. You run theterraform apply command, which performs the following actions:

    1. It provisions your infrastructure based on your execution plan by invoking the corresponding Compute Engine APIs in the background.
    2. It creates aTerraform state file, which is a JSON file that maps the resources in your configuration file to the resources in the real-world infrastructure. Terraform uses this file to keep a record of the most recent state of your infrastructure, and to determine when to create, update, and destroy resources.
    3. When you runterraform apply, Terraform uses the mapping in the state file to compare the existing infrastructure to the code, and make updates as necessary:

      • If a resource object is defined in the configuration file, but doesn't exist in the state file, Terraform creates it.
      • If a resource object exists in the state file, but has a different configuration from your configuration file, Terraform updates the resource to match your configuration file.
      • If a resource object in the state file matches your configuration file, Terraform leaves the resource unchanged.

Terraform resources for Compute Engine

Resources are the fundamental elements in the Terraform language. Eachresource block describes one or more infrastructure objects, such as virtualnetworks or compute instances.

The following table lists the Terraform resources available forCompute Engine:

Terraform resourcesTerraform data sources

Terraform-based guides for Compute Engine

The following table lists Terraform-based how-to guides and tutorials for Compute Engine:

GuideDetails
Create and start a Compute Engine instanceExplains how to create a virtual machine (VM) instance.
Create a VM with Local SSD disksExplains how you can create a VM with Local SSD disk storage.
Create disk snapshotsExplains how to create standard snapshots to periodically back up data from disks.
Duplicate a disk with clonesExplains how to create zonal disk clones of an existing disk.
Create and manage regional disksExplains how to create and manage regional disks.
Create and use Spot VMsExplains how to create and manage Spot VMs. Spot VMs are VM instances that are excess Compute Engine capacity. Spot VMs are available at much lower prices compared to the on-demand price for standard VMs
Add SSH keys to VMsDescribes how to add SSH keys to VM instances that use OS Login and VMs that use metadata-based SSH keys.
Running shutdown scriptsExplains how to create and run shutdown scripts that execute commands right before a VM instance is stopped or restarted.
Create a reservation for a single projectExplains how to create a single-project reservation, which can be consumed only by VM instances in the same project.
Create instance templatesDescribes how to create and manage instance templates.
Scaling based on schedulesDescribes how you can create scaling schedules for an existing MIG. Schedule-based autoscaling lets you improve the availability of your workloads by scheduling capacity ahead of anticipated load.
Add Persistent Disk storage to your VMExplains how to create a blank, non-boot zonal Persistent Disk volume and attach it to your VM.
Set up OS LoginDescribes how to set up OS Login.
Attach a non-boot disk to a VMExplains how to attach a non-boot zonal disk to your VM.
Create a VM that uses a user-managed service accountExplains how to create a VM instance that is configured to use a user-managed service account. A service account is a special kind of account typically used by an application or compute workload to make authorized API calls.
Increase the size of a persistent diskExplains how to increase the size of a persistent disk.
Enable virtual displays on a VMDescribes how to enable virtual displays on a VM instance.
Create a MIG in a single zoneDescribes how to create a managed instance group (MIG) in a single zone. Putting all your MIG's VMs in a single zone helps to minimize latency, which is useful for certain workloads—for example, batch workloads.
Create a VM instance with a custom hostnameExplains how to create a VM instance with a custom hostname.
Set up an application-based health check and autohealingDescribes how to set up an application-based health check to autoheal VMs in a MIG.
Create a MIG that uses preemptible VMsDescribes how to create a MIG that uses preemptible VM instances. Preemptible VMs are useful if your workload can tolerate disruptions and you want to take advantage of the cost-savings associated with preemptible VMs.
Add and remove VMs from a MIGDescribes how to add and remove VMs from a MIG.
Create a MIG with autoscaling enabledDescribes how to create an autoscaled MIG that automatically adds and removes VMs based on average CPU utilization across the group.
Create a MIG with stateful disksDescribes how to create a MIG that uses preemptible VM instances. Preemptible VMs are useful if your workload can tolerate disruptions and you want to take advantage of the cost-savings associated with preemptible VMs.
Creating SQL Server VM instancesExplains how to create Microsoft SQL Server VM instances. SQL Server is a database system that runs on Windows Server and some Linux distributions. You can use SQL Server on Compute Engine as part of the backend for your applications, as a flexible development and test environment, or in addition to your on-premises systems for backup and disaster recovery.
Create an OS policy assignmentExplains how to create an OS policy assignment. You can use OS policies to maintain consistent software configurations across Linux and Windows VM instances.
Configuring stateful metadata in MIGsDescribes how to configure stateful metadata in MIGs. Instance metadata is useful for setting properties for and communicating with your applications through the metadata server.
Configuring stateful persistent disks in MIGsExplains how to configure stateful persistent disks in MIGs. Configuring persistent disks to be stateful lets you benefit from VM instance autohealing and automated updates while preserving the state of the disks.
Configuring stateful IP addresses in MIGsDescribes how to configure stateful IP addresses in MIGs. By configuring stateful IP addresses in a MIG, you ensure that IP addresses are preserved when VM instances in the group are autohealed, updated, and recreated.
Set a target distribution shape for VMs in a regional MIGExplains how to set a target distribution shape for VMs.
Create a MIG with VMs in multiple zones in a regionDescribes how to create a MIG that has its VMs spread across multiple zones in a region. Spreading your application load across multiple zones protects your workload against zonal failures.
Disable and re-enable proactive VM redistribution in a regional MIGDescribes how to disable and re-enable proactive VM redistribution in a regional MIG. In a regional MIG, to maintain an even number of VM instances across the selected zones in the region, use proactive instance redistribution. This configuration option maximizes the availability of your application in the event of a zone-level failure.

Terraform modules and blueprints for Compute Engine

Modules and blueprints help you automate provisioning and managing of Google Cloud resources at scale. Amodule is a reusable set of Terraform configuration files that creates a logical abstraction of Terraform resources. Ablueprint is a package of deployable and reusable modules, and a policy that implements and documents a specific solution.

The following table lists modules and blueprints related to Compute Engine:

Module or blueprintDetails
terraform-google-vmCollection of opinionated submodules that you can use as building blocks to provision VMs in Google Cloud.
terraform-google-startup-scriptsProvides a library of useful startup scripts to embed in VMs.
terraform-google-container-vmDeploys containers on Compute Engine instances

What's next

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-15 UTC.