In this article, we are going to dive into Infrastructure as Code (IaC) with Terraform and Google Cloud. We’ll use Terraform to provision a Virtual Machine (VM) on GCP, automating the process and ensuring consistency across deployments.
The code for this article can be found in this GitHub repo in the Day_5 folder.
What is Terraform?
This is an open-source tool that allows you to define and manage infrastructure resources using a declarative language. You get to declare your infrastructure setup in a configuration file, and Terraform figures out how to create or update the resources to match.
Prerequisites
Terraform to be installed locally in your computer.
Google Cloud Project with the
Compute Admin
role granted to a Service Account.
Setting up
Create a directory in your computer and name it Provision-a-VM-with-Terraform
or whatever you want.
Inside it, create a file called main.tf
and paste the following:
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "6.8.0"
}
}
}
provider "google" {
# Configure authentication using a service account key file
credentials = file("your-credentials-file-here.json") # Point this to where your credentials are locally
region = "your-region-here" # Replace with desired region
project = "your-project-id-here" # Replace with your project id
}
resource "google_compute_instance" "my_vm" {
name = "your-desired-instance-name" # Replace with desired instance name
machine_type = "your-machine-type" # Replace with desired machine type
zone = "your-desired-zone" # Replace with desired zone
boot_disk {
initialize_params {
image = "debian-cloud/debian-11" # Replace with desired image
}
}
network_interface {
subnetwork = "default" # Replace with desired subnetwork
}
}
Script explanation:
Terraform Configuration
terraform { … }
: This block defines the required providers for your infrastructurerequired_providers
: This block defines the providers you’ll be using in your script.google: This indicates that you’ll be using
hashicorp/google
provider to interact with Google Cloud Platform.source
: Specifies the source of the provider.version
: specifies the version of the provider to use.Provider Configuration
provider “google” { … }
: This block configures how Terraform will interact with your Google Cloud environment.credentials
: This option specifies the path to your service account key file. This file contains the necessary credentials for Terraform to authenticate to your GCP project.Remember to replace it with the actual file path & name. Replace the
region
andproject
values with the correct ones as well before proceedingResource Definition
resource “google_compute_instance” “my_vm” { … }
: This block defines a single Compute Engine instance resource.Remember to replace
name
,machine_type
,zone
with the values of your choosing.boot_disk
: Configures the boot disk for the instance. Theinitialize_params
key contains theimage
key where it specifies the source image for the boot disk. You can replace the existingdebian-cloud/debian-11
with the desired imagenetwork_interface
: Configures the network interface for the instance.subnetwork
: Specifies the subnetwork where the instance will be attached. You should replacedefault
with your desired subnetwork.
Execution
Initialize Terraform:
In your terminal, navigate to the same location as your main.tf
file and run the following command:
$ terraform init
This command will initialize the working directory and download the necessary plugins including the Google Cloud provider.
Plan the changes:
Run terraform plan
to preview the changes that Terraform will make to your GCP project. This will help you review and validate the configuration before applying it.
$ terrafor plan
Apply the changes:
Run terraform apply
to create the VM in GCP. Review the changes carefully and confirm the application.
Confirm provisioned resources
Go into your GCP Project and confirm that a Virtual Machine has been created with the configurations you supplied in the Terraform script:
Conclusion
Terraform empowers you to manage Infrastructure as Code (IaC), making it easier to deploy and manage resources consistently. Using Terraform with Google Cloud provides a powerful combination for automating the provisioning of Virtual Machines and other infrastructure components.