Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to define and provision your cloud infrastructure using a declarative configuration language. This guide will walk you through the process of launching Virtual Machines (VMs) on Google Cloud Platform (GCP) using Terraform, making your infrastructure setup reproducible, scalable, and easy to manage.
Prerequisites
Before you start, ensure that you have the following:
- Google Cloud Account: You need an active Google Cloud account with a project set up. If you don’t have one, sign up at Google Cloud.
- Terraform Installed: Terraform should be installed on your local machine. You can download it from the Terraform website.
- GCP Service Account Key: You’ll need a service account key with appropriate permissions (e.g., Compute Admin) to manage resources in your GCP project. Download the JSON key file for this service account.
Step 1: Set Up Your Terraform Directory
Start by creating a new directory for your Terraform configuration files. This is where you’ll define your infrastructure.
mkdir gcp-terraform-vm
cd gcp-terraform-vm
Step 2: Create the Terraform Configuration File
In your directory, create a new file called main.tf
. This file will contain the configuration for your VM.
touch main.tf
Open main.tf
in your preferred text editor and define the necessary Terraform settings.
# main.tf
provider "google" {
project = "<YOUR_GCP_PROJECT_ID>"
region = "us-central1"
credentials = file("<PATH_TO_YOUR_SERVICE_ACCOUNT_KEY>.json")
}
resource "google_compute_instance" "vm_instance" {
name = "terraform-vm"
machine_type = "e2-medium"
zone = "us-central1-a"
boot_disk {
initialize_params {
image = "debian-cloud/debian-11"
}
}
network_interface {
network = "default"
access_config {
# Ephemeral IP
}
}
tags = ["web", "dev"]
metadata_startup_script = <<-EOT
#! /bin/bash
sudo apt-get update
sudo apt-get install -y nginx
EOT
}
Explanation of the Configuration
- Provider Block: Specifies the GCP provider, including the project ID, region, and credentials.
- google_compute_instance Resource: Defines the VM instance, including its name, machine type, and zone. The
boot_disk
block specifies the disk image, and thenetwork_interface
block defines the network settings. - metadata_startup_script: A startup script that installs Nginx on the VM after it boots up.
Step 3: Initialize Terraform
Before you can apply the configuration, you need to initialize Terraform. This command downloads the necessary provider plugins.
terraform init
Step 4: Plan Your Infrastructure
The terraform plan
command lets you preview the changes Terraform will make to your infrastructure. This step is useful for validating your configuration before applying it.
terraform plan
If everything is configured correctly, Terraform will show you a plan to create the VM instance.
Step 5: Apply the Configuration
Now that you’ve reviewed the plan, you can apply the configuration to create the VM instance on GCP.
terraform apply
Terraform will prompt you to confirm the action. Type yes
to proceed.
Terraform will then create the VM instance on GCP, and you’ll see output confirming the creation.
Step 6: Verify the VM on GCP
Once Terraform has finished, you can verify the VM’s creation by logging into the GCP Console:
- Navigate to the Compute Engine section.
- You should see your
terraform-vm
instance running in the list of VM instances.
Step 7: Clean Up Resources
If you want to delete the VM and clean up resources, you can do so with the following command:
terraform destroy
This will remove all the resources defined in your Terraform configuration.
Conclusion
Using Terraform to launch VMs on Google Cloud Platform provides a robust and repeatable way to manage your cloud infrastructure. With just a few lines of configuration code, you can automate the creation, management, and destruction of VMs, ensuring consistency and reducing the potential for human error. Terraform’s ability to integrate with various cloud providers makes it a versatile tool for infrastructure management in multi-cloud environments.