This tutorial shows how to create a single Google Compute Engine VM with Terraform, using the google_compute_instance resource above as the starting point. The snippet above only shows the resource block. It leaves out the provider configuration, project and zone settings, and authentication, and it uses a Debian 9 image that Google has since retired. The sections below give a complete working configuration, the commands to deploy it and remove it, and fixes for the errors people hit most often.
resource "google_compute_instance" "vm_instance" {
name = "terraform-instance"
machine_type = "e2-micro"
boot_disk {
initialize_params {
image = "debian-cloud/debian-9"
}
}
network_interface {
# A default network is created for all GCP projects
network = "default"
access_config {
}
}
}Code language: PHP (php)
What the example above does and what it is missing
The resource block declares one VM named terraform-instance with the e2-micro machine type. Its boot disk is built from a public Debian image, and its network interface is attached to the project's default VPC. The empty access_config {} block matters: it tells Google to give the VM an ephemeral external IP address. If you remove it, the VM gets only an internal IP.
Terraform cannot run that block on its own. To work, it also needs:
- A
terraformblock that declares thehashicorp/googleprovider. - A
provider "google"block that sets the project, region and zone. Without a zone, the instance cannot be placed. - Credentials Terraform can use to call the Google Cloud APIs.
- An image that still exists. The
debian-cloud/debian-9image family has been deprecated and removed from the public image list.
Prerequisites
- A Google Cloud project with billing enabled.
- Terraform 1.5 or later installed (
terraform version). - The Google Cloud CLI installed (
gcloud version). - An account with permission to create Compute Engine instances in the project, for example the Compute Instance Admin role.
Sign in, select the project, enable the Compute Engine API and create Application Default Credentials (ADC). The Terraform provider picks up ADC automatically, so you do not need a key file.
gcloud auth login
gcloud config set project YOUR_PROJECT_ID
gcloud services enable compute.googleapis.com
gcloud auth application-default loginCode language: CSS (css)
Note: Don't download service account JSON keys to a laptop just to run Terraform. Use ADC locally. In CI, use Workload Identity Federation or an attached service account.
Complete working configuration
Create an empty directory and add the following files. Together they wrap the original resource in everything it needs to run.
# versions.tf
terraform {
required_version = ">= 1.5"
required_providers {
google = {
source = "hashicorp/google"
version = ">= 6.0"
}
}
}
provider "google" {
project = var.project_id
region = var.region
zone = var.zone
}Code language: PHP (php)
# variables.tf
variable "project_id" {
description = "Google Cloud project ID"
type = string
}
variable "region" {
type = string
default = "us-central1"
}
variable "zone" {
type = string
default = "us-central1-a"
}Code language: PHP (php)
# main.tf
resource "google_compute_instance" "vm_instance" {
name = "terraform-instance"
machine_type = "e2-micro"
zone = var.zone
labels = {
env = "lab"
managed_by = "terraform"
}
boot_disk {
initialize_params {
image = "debian-cloud/debian-12"
size = 10
}
}
network_interface {
network = "default"
access_config {} # ephemeral public IP
}
shielded_instance_config {
enable_secure_boot = true
}
}Code language: PHP (php)
# outputs.tf
output "instance_name" {
value = google_compute_instance.vm_instance.name
}
output "external_ip" {
value = google_compute_instance.vm_instance.network_interface[0].access_config[0].nat_ip
}
output "internal_ip" {
value = google_compute_instance.vm_instance.network_interface[0].network_ip
}Code language: PHP (php)
# terraform.tfvars
project_id = "YOUR_PROJECT_ID"Code language: PHP (php)
debian-cloud/debian-12 uses the project/family shorthand, which always resolves to the latest image in that family. If you need builds you can repeat exactly, pin a specific image name instead. To list the current families, run gcloud compute images list --filter="family~debian".
Note: Before using this in a real project, check the Terraform Registry for the latest major version of the Google provider. Then pin to it with a pessimistic constraint such as ~> X.0, so that a future major release cannot change behaviour without warning.
Deploy, connect and clean up
- Run
terraform initto download the Google provider and create.terraform.lock.hcl. - Run
terraform fmtandterraform validateto catch syntax mistakes early. - Run
terraform planand review it. It should end withPlan: 1 to add, 0 to change, 0 to destroy. - Run
terraform applyand typeyes. Creating the VM usually takes under a minute, and the outputs print at the end. - Connect with
gcloud compute ssh terraform-instance --zone us-central1-a. The default network includes a firewall rule that allows SSH on port 22. - When you have finished, run
terraform destroyso you are not billed for the VM or its disk.
$ terraform apply
...
google_compute_instance.vm_instance: Creating...
google_compute_instance.vm_instance: Creation complete after 15s [id=projects/YOUR_PROJECT_ID/zones/us-central1-a/instances/terraform-instance]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
external_ip = "34.x.x.x"
instance_name = "terraform-instance"
internal_ip = "10.128.0.2"Code language: JavaScript (javascript)
Corrected version of the original snippet
If you only want to fix the snippet above, here is the minimal corrected resource. You still need the provider block from the previous section.
resource "google_compute_instance" "vm_instance" {
name = "terraform-instance"
machine_type = "e2-micro"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-12"
}
}
network_interface {
network = "default"
access_config {}
}
}Code language: JavaScript (javascript)
debian-cloud/debian-9becamedebian-cloud/debian-12, because the Debian 9 image family is retired and no longer resolves.- Added a
zone. Alternatively, set it in the provider block, as shown in the full configuration. - A provider block with a
projectis required, or you can set theGOOGLE_PROJECTenvironment variable instead.
Common errors and fixes
| Error message (abbreviated) | Cause | Fix |
|---|---|---|
could not find default credentials | ADC is not configured | Run gcloud auth application-default login |
project: required field is not set | No project in the provider block or the environment | Set project in the provider block or export GOOGLE_PROJECT |
Compute Engine API has not been used in project ... or it is disabled | The API is not enabled | Run gcloud services enable compute.googleapis.com and wait a minute |
The resource 'projects/debian-cloud/global/images/family/debian-9' was not found | The image family is retired | Use debian-cloud/debian-12 or another current family |
The resource 'projects/.../networks/default' was not found | The project has no default VPC, which is common when an organization policy skips its creation | Create a VPC with google_compute_network or reference an existing one |
ZONE_RESOURCE_POOL_EXHAUSTED or a quota error | The zone is out of capacity or a project quota has been reached | Try another zone or request a quota increase |
Required 'compute.instances.create' permission | Your identity lacks IAM permissions | Grant a role such as Compute Instance Admin on the project |
Best practices and security notes
- Do not commit state. Add
*.tfstate*,.terraform/and*.tfvarsfiles that contain secrets to.gitignore. Do commit.terraform.lock.hcl. - Use remote state for team work. Configure a
backend "gcs"block pointing at a versioned Cloud Storage bucket, so state is shared and protected by locking. - Limit public exposure. Drop
access_config {}when a VM doesn't need a public IP. Use IAP TCP forwarding (gcloud compute ssh --tunnel-through-iap) or Cloud NAT for outbound traffic. - Attach a dedicated service account to the VM with only the roles it needs, rather than relying on the Compute Engine default service account.
- Turn on deletion protection (
deletion_protection = true) for long-lived VMs. - Scan and lint before applying. Tools such as tfsec flag issues like public IPs and missing Shielded VM settings. See Terraform Tutorials: TFSec for Security Scanning and How to Configure pre-commit for Terraform.
To create several identical VMs from one block, add count or for_each to the resource. Terraform Loops and Iteration covers both patterns.
Frequently asked questions
Why does Terraform say the debian-9 image was not found?
Google has retired the Debian 9 image family, so debian-cloud/debian-9 no longer resolves to an image. Change it to a current family such as debian-cloud/debian-12.
Do I need a service account key file to run Terraform against Google Cloud?
No. Locally, run gcloud auth application-default login and the Google provider uses those credentials automatically. In CI, Workload Identity Federation is preferred over long-lived JSON keys.
What does the empty access_config block do?
It gives the VM an ephemeral external IP address. If you remove it, the instance has only an internal IP and needs IAP, a bastion host or Cloud NAT for access and outbound traffic.
Is an e2-micro VM free?
Google Cloud's Always Free tier includes one e2-micro instance per month in certain US regions, subject to the current free tier terms. VMs outside those regions, or above the free limits, are billed normally, so run terraform destroy when you have finished testing.
How do I get the VM's public IP from Terraform?
Add an output that references google_compute_instance.vm_instance.network_interface[0].access_config[0].nat_ip, then run terraform output external_ip after apply.
Related tutorials
- More Terraform tutorials
- More Google Cloud tutorials
- Terraform Tutorials: TFSec for Security Scanning
- Terraform Loops and Iteration: Complete Guide to count, for_each, for Expressions, and Dynamic Blocks
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals