Terraform is an infrastructure as code tool that lets you provision and manage cloud infrastructure. Terraform provides plugins called providers that let you interact with cloud providers and other APIs. You can use the Terraform 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.
How Terraform Works with Google Cloud Compute Engine
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:
- You describe the infrastructure you want to provision in a Terraform configuration file. You don't need to write code describing how to provision the infrastructure. Terraform provisions the infrastructure for you.
- You run the terraform plan command, which evaluates your configuration and generates an execution plan. You can review the plan and make changes as needed
Hashicorp Terraform is an Infrastructure as code tool that lets you provision and manage cloud infrastructure. Terraform provider for Google Cloud lets you provision and manage Google Cloud infrastructure.
The declarative model means the configuration expresses desired state rather than procedural steps. The provider for Google Cloud translates that state into Compute Engine API calls. The workflow is consistent across resources: author configuration, initialize the working directory, plan changes, apply changes, and optionally destroy.
Prerequisites and Project Setup
Before you begin, ensure that you have the following prerequisites:
- Google Cloud Platform Account: You will need an active Google Cloud account.
- Terraform Installed: Install Terraform on your local machine. If not already installed, follow this guide to set up Terraform.
- gcloud CLI Installed: The gcloud command-line interface is required for authentication. You can install it following this guide.
- A Google Cloud Project: Create a GCP project if you don't have one
This project provides a structured approach for setting up a GCP project, configuring Terraform, and deploying infrastructure resources efficiently.
Roles required to select or create a project:
- Select a project: Selecting a project doesn't require a specific IAM role — you can select any project that you've been granted a role on.
- Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
Create a Google Cloud project:
bash
gcloud projects create PROJECT_ID
Replace PROJECT_ID with a name for the Google Cloud project you are creating.
To use an online terminal with the gcloud CLI and Terraform already set up, activate Cloud Shell. At the bottom of this page, a Cloud Shell session starts and displays a command-line prompt. It can take a few seconds for the session to initialize.
Terraform Configuration for Compute Engine VM Instances
This file defines the Google Cloud resources that you want to create.
bash
cat main.tf
The output is similar to the following
This file describes the googlecomputeinstance resource, which is the Terraform resource for the Compute Engine VM instance. googlecomputeinstance is configured to have the following properties:
- name is set to my-vm
- machine_type is set to n1-standard-1
- zone is set to us-central1-a
- boot_disk sets the boot disk for the instance
- network_interface is set to use the default network in your Google Cloud project
Create the Compute Engine VM instance
In Cloud Shell, run the following command to verify that Terraform is available:
bash
terraform
The output should be similar to the following:
Usage: terraform [global options] <subcommand> [args] The available commands for execution are listed below. The primary workflow commands are given first, followed by less common or more advanced commands. Main commands: init Prepare your working directory for other commands validate Check whether the configuration is valid plan Show changes required by the current configuration apply Create or update infrastructure destroy Destroy previously-created infrastructure
Initialize Terraform by running the following command. This command prepares your workspace so Terraform can apply your configuration.
bash
terraform init
The output should be similar to the following:
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/google..
After initialization, the plan step evaluates the configuration against the current state in Google Cloud and produces an execution plan. The apply step creates or updates infrastructure to match the desired state.
A minimal googlecomputeinstance configuration expresses name, machine type, zone, boot disk, and network interface. The resource name my-vm, machine type n1-standard-1, zone us-central1-a, boot disk definition, and default network interface are typical starting points for a quickstart VM.
Modules and Blueprints for Scale
Modules and blueprints help you automate provisioning and managing of Google Cloud resources at scale. A module is a reusable set of Terraform configuration files that creates a logical abstraction of Terraform resources. A blueprint 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 blueprint | Details |
|---|---|
| terraform-google-vm | Collection of opinionated submodules that you can use as building blocks to provision VMs in Google Cloud. |
| terraform-google-startup-scripts | Provides a library of useful startup scripts to embed in VMs. |
| terraform-google-container-vm | Deploys containers on Compute Engine instances |
This project provides an efficient way to manage GCP Compute Engine instances using Terraform, following best practices for modular configuration and automation.
Modules allow reuse without copying configuration. The terraform-google-vm collection offers building blocks for VM provisioning. The terraform-google-startup-scripts module provides a library of useful startup scripts to embed in VMs. The terraform-google-container-vm module deploys containers on Compute Engine instances.
Deployment Workflow and Verification
Running Terraform follows a predictable workflow.
- Setting Up GCP Project
- Setting Up Terraform
- Terraform Configuration
- Running Terraform
- Cleaning Up
The repository demonstrates how to manage Google Cloud Platform Compute Engine instances using Terraform. It provides a structured approach for setting up a GCP project, configuring Terraform, and deploying infrastructure resources efficiently.
Verify the Deployment:
- Navigate to Compute Engine > VM instances in the GCP Console.
Once apply completes, the VM instance appears in the Compute Engine console with the name, machine type, zone, and network defined in the configuration. Connection can be established using the external IP and SSH.
Network and subnet deployment can be combined with VM provisioning. A comprehensive guide on deploying compute engine, network and Subnet on GCP using terraform shows how to define VPC networks, subnets, and VM instances in a single configuration.
Cleanup and Destruction
To remove all resources:
bash
terraform destroy
Confirm by typing yes
Destruction removes previously created infrastructure defined in the configuration. The destroy command generates a plan of resources to be removed and applies it after confirmation.
Best practice is to keep state separate from configuration, use version control for Terraform files, and apply changes through plan review before apply. The project structure typically includes prerequisites, project setup, Terraform setup, configuration, running Terraform, cleaning up, and additional resources.
Conclusion
Terraform with Google Cloud Compute Engine enables declarative, repeatable provisioning of virtual machines, networks, and supporting resources. The provider for Google Cloud translates Terraform configuration into Compute Engine API operations through a clear workflow of init, plan, apply, and destroy.
The quickstart for creating a VM instance using Terraform shows how to author a googlecomputeinstance resource with name, machine type, zone, boot disk, and network interface, initialize the working directory, and apply the configuration. The same pattern scales to networks and subnets, allowing infrastructure to be defined alongside compute.
Modules and blueprints extend the base workflow for production use. Reusable modules like terraform-google-vm, terraform-google-startup-scripts, and terraform-google-container-vm provide opinionated building blocks and automation at scale. Verification in the GCP Console confirms that Terraform has materialized the desired state, and terraform destroy provides a controlled path to clean up resources.
The combination of declarative configuration, provider abstraction, and modular reuse makes Terraform a durable approach for managing GCP Compute Engine workloads across development, staging, and production environments.
Sources
- https://docs.cloud.google.com/compute/docs/terraform
- https://github.com/moshclouds/Managing-GCP-Compute-Engine-With-Terraform
- https://docs.cloud.google.com/docs/terraform/create-vm-instance
- https://dev.to/christiana_orji/terraform-how-to-deploy-compute-engine-network-and-subnet-with-terraform-on-gcp-1f4h