Creating Google Compute Engine (GCE) Virtual Machine (VM) Using Terraform

Table of contents

No heading

No headings in the article.

In this article, we'll dive into the process of crafting a Google Cloud virtual machine instance with Terraform. Terraform, an esteemed infrastructure as code (IaC) tool, empowers you to fashion resources from the ground up, whether it's on-premises, Google Cloud, Microsoft Azure, or AWS. Let's walk through the prerequisites and the steps to set up our files, acquaint ourselves with initialization, validation, planning, and verification of the deployment.

Structure

  • Prerequisites

  • Creating GCE instance using Terraform

Prerequisites

  • Google Cloud Account (you can open a Google Cloud Account through the console)

  • Create a Google Cloud Project

  • Enable IAM API and Compute Engine API

  • Install, configure, and authenticate the gCloud CLI.

  • Install Terraform

  • Create and destroy resources using Terraform Commands

  1. Google Cloud Account

    If you haven’t got a Google Cloud account yet, head over to create one. Upon account creation, you'll receive $300 free credits for a limited time and enjoy free-tier eligibility for 12 months. Keep in mind that charges may apply for services beyond the free tier.

  2. Create Google Cloud Project

Google Cloud organizes resources via projects. After creating your account, the next step is to create a project. While project names can be altered, the project ID remains unique and immutable. To initiate a project, navigate to the upper-left corner, click, and select "create new project." Choose a fitting name for your project.

  1. Install gCloud CLI

With your account set up, it's time to install, configure, and authenticate the gCloud CLI for terminal commands. Authorization and configuration are essential for Terraform usage. Follow the steps outlined in the documentation to install, configure, and employ the CLI. Execute gcloud init to authorize the project, utilize gcloud config set to establish credentials like email and projects, select the project, and optionally define a region/zone. I advise against setting a zone to allow flexibility for hosting multiple zones within a project. Finally, execute gcloud auth application-default login to enable Terraform to access necessary credentials for Google Cloud.

  1. Install Terraform

If Terraform is unfamiliar territory for you, head to the official documentation for installation instructions. Verify the installation by typing terraform. For Windows installation, refer to this article.

  1. Enable IAM API and Compute Engine API

Upon accessing your Google Account, utilize Cloud Console to enable IAM API and Compute Engine API. Alternatively, use gCloud CLI commands gcloud services enable compute.googleapis.com and gcloud services enable iam.googleapis.com.

  1. Creating and Destroying Terraform Resources

With the prerequisites in place, let’s create a GCE instance using Terraform. Start by creating the main.tf file. This serves as the core file containing all configurations necessary for resource creation via Terraform. While additional configuration files are possible, for simplicity, we'll stick with the main.tf file for creating our resources. Firstly, define the provider indicating the use of Google Cloud Platform (GCP). Google Cloud necessitates a valid project ID and region for resource creation, specifying where the resource will be deployed and the region within the project.

provider "google" {

project = "project-id" # write your own Project ID

region = "us-central-1"

}

Next, we'll craft the main body, utilizing the Terraform resource template to instantiate a GCP virtual machine.

resource "google_compute_instance" "terraform-gcp" {

name = "my-instance" #you can change instance name

machine_type = "e2-micro"

zone = "us-central1-a"

boot_disk {

initialize_params {

image = "ubuntu-minimal-2204-lts" #you can change the OS of the GCE instance

}

}

network_interface {

network = "default"

}

}

The snippet above depicts the creation of an instance using google_compute_instance, signifying the creation of a GCE virtual machine instance, with terraform-gcp designated to this specific instance resource. Further down, we specify the instance name as "my instance," e2-micro as our machine type, deploying our resource in US Central 1 with zone "a". Unlike Azure and AWS, Google Cloud mandates configuration of a boot disk and a network interface for the instance. Our code initializes a boot disk parameter (initialize_params) with an Ubuntu 22.04 LTS image and implements a default network for use in our project.

After implementing the block, initialize the project with terraform init. A successful initialization will yield a .terraform folder, .terraform.lock.hcl file, and a confirmation message.

To validate our configurations, employ terraform validate and terraform plan to ensure successful configuration, ready for deployment.

terraform plan provides an outline of resources to be created, serving as a preliminary indication that deployment is imminent after necessary checks.

Now, it's time to apply our changes with terraform apply --auto-approve, inspect the created resources with terraform state list, and finally destroy resources using terraform destroy --auto-approve.

Upon executing terraform apply, a terraform.tfstate file is generated, delineating the resources and their configuration in our project. Employ terraform state list to view the created resources.

Given that Google Cloud resources incur costs, it's prudent to destroy resources if they're no longer needed using terraform destroy --auto-approve.

Upon destroying all resources, a terraform.tfstate.backup is automatically generated, facilitating rollback to previous infrastructure if needed.

Now equipped with the knowledge of creating a simple GCE virtual machine using Terraform, this post has covered setting up a Google Cloud Account, creating a Google Cloud project, installing, authenticating, configuring, logging in, and utilizing the gCloud CLI, as well as installing Terraform. Finally, we've explored how to create GCE instances using Terraform. Thank you for your time and do share your thoughts and feedback!