How to create a virtual machine instance using an instance template in GCP with Terraform
Create a virtual machine (VM) with Terraform.
Terraform is a leading Infrastructure as Code tool used to provision and manage cloud infrastructure across multiple providers, including Google Cloud Platform (GCP). With Terraform, users can define their infrastructure requirements in configuration files, known as Terraform scripts, which are then executed to create and manage resources in the cloud.
Terraform is a popular Infrastructure as Code (IaC) tool used for building, changing, and versioning infrastructure efficiently.
Before delving into Terraform’s usage with GCP, it’s crucial to understand the basic concepts of Terraform. Terraform scripts describe the desired state of the infrastructure, and Terraform handles the provisioning and management of resources to achieve that state.
In GCP, Terraform interacts with the Google Cloud Provider to create and manage resources such as virtual machines, storage buckets, networking components, and more. Terraform leverages the GCP API to orchestrate the deployment and configuration of these resources.
Key concepts in Terraform include:
- Provider: Terraform providers are plugins that interface with various cloud providers, including GCP. The Google Cloud Provider allows Terraform to manage GCP resources.
- Resources: Resources are the building blocks of infrastructure in Terraform. Users define resources, such as virtual machines, networks, and storage buckets, in their Terraform configuration files.
- Modules: Modules in Terraform enable reusable and shareable configurations. Users can encapsulate infrastructure components into modules for easier management and abstraction.
- State: Terraform maintains a state file that records the current state of deployed resources. This state file is used to track changes and manage infrastructure updates accurately.
For more information about Terraform, please visit this link
Key features of using Terraform with GCP include:
- Infrastructure as Code: Terraform enables infrastructure to be defined and managed as code, providing version control, collaboration, and automation benefits.
- Declarative Syntax: Terraform scripts use declarative syntax to define the desired state of the infrastructure, allowing users to focus on the desired outcome rather than the specific steps to achieve it.
- Plan and Apply: Terraform provides a “plan” phase to preview changes before applying them, ensuring safe and predictable infrastructure updates.
- Resource Graph: Terraform builds a dependency graph of resources, enabling parallel creation and destruction of resources where possible for efficient provisioning and management.
Create a file main.tf
touch main.tf
Terraform configuration defines the creation of a virtual machine (VM) instance using an instance template in Google Cloud Platform (GCP).
provider "google" {
project = "testing"
region = "asia-northeast1"
zone = "asia-northeast1-a"
}
This section specifies the Google Cloud provider configuration, including the project ID, region, and zone where the resources will be deployed.
The following sections instantiate modules from the cypik
namespace, which are used to create a Virtual Private Cloud (VPC), subnet, and firewall rules. These modules help set up the networking environment for the VM instance.
After configuring the networking components, the script defines an instance template using the instance_template
module:
module "instance_template" {
source = "cypik/template-instance/google"
version = "1.0.1"
name = "template"
environment = "test"
region = "asia-northeast1"
source_image = "ubuntu-2204-jammy-v20230908"
source_image_family = "ubuntu-2204-lts"
source_image_project = "ubuntu-os-cloud"
disk_size_gb = "20"
subnetwork = module.subnet.subnet_id
instance_template = true
service_account = null
## public IP if enable_public_ip is true
enable_public_ip = true
metadata = {
ssh-keys = <<EOF
dev:ssh-rsa AAAAB3NzaC1yc2EAA/3mwt2y+PDQMU= suresh@suresh
EOF
}
}
In this block:
source
,name
,name
,environment
, and other parameters define the instance template's characteristics.subnetwork
specifies the subnet in which the VM instance will reside.enable_public_ip
enables a public IP address for the VM.metadata
includes SSH keys for accessing the VM.
Finally, the script creates the VM instance using the defined instance template:
module "compute_instance" {
source = "cypik/template-instance/google"
version = "1.0.1"
name = "instance"
environment = "test"
region = "asia-northeast1"
zone = "asia-northeast1-a"
subnetwork = module.subnet.subnet_id
instance_from_template = true
deletion_protection = false
service_account = null
## public IP if enable_public_ip is true
enable_public_ip = true
source_instance_template = module.instance_template.self_link_unique
}
Here,compute_instance
the module is instantiated, specifying parameters such as name, environment
region
, andsubnetwork
. instance_from_template
is set to true
, indicating that this VM instance will be created from the instance template defined earlier.
terraform init
terraform plan
terraform apply
This Terraform script orchestrates the creation of a VM instance using an instance template, along with necessary networking configurations, in the Google Cloud Platform.
Terraform simplifies the process of managing infrastructure on GCP by providing a consistent and automated approach to provisioning and managing resources.
Summary
Streamlining VM Deployment on GCP with Terraform
Please use the code for more details.
Enjoy it! 🍻 That’s It; we are done...
For seamless Cloud Management incorporating DevOps as the core of the methodology, reach out to us at info@cypik.com
About the author:
My name is Suresh Yadav, and I am an experienced Linux enthusiast and DevOps engineer. I’m passionate about automating and streamlining development processes, and currently, I work as a DevOps Engineer at Cypik. I specialize in cloud technologies, with a focus on the Google Cloud Platform (GCP).