How to create VM in GCP using Devops Tool – Terraform
0
/
474
Overview
If you are interested in creating cloud infrastructure and automating it, this article is for you. In this blog we will learn how to create a Virtual Machine (Compute Engine) in Google Cloud using Terraform.
Terraform with GCP
Let’s get familiar with few terms :
Terraform – is infrastructure as code solution to configure infrastructure and deploy resources in cloud like GCP.
Google Cloud Platform – GCP is a cloud-based infrastructure environment.
Google Compute Engine – is a resource that provides virtual systems to Google Cloud Platform customers.
Pre-requisites for creating VM in GCP :
- VM or server with terraform installed.
If you want to learn how to install terraform follow this post -> INSTALL DEVOPS IAC TOOL “TERRAFORM” ON CENTOS 7
- GCP Account
- GCP project with service account.
Steps :
1. Create GCP project
2. Create Service Account in GCP and Download credentials json file
3. Create VM (Compute Engine) with Terraform in GCP
Let’s start implementation :
1. Create GCP project in GCP
a. Access cloud console
https://console.cloud.google.com/
Create Project
b. Select Home > Google Cloud Platform
Create Project Step 2
c. Click Create Project from Dashboard
Create Project Step 3
d. Enter Project Name : “HostBread” and click on create
Note down the project & project id : hostbread
You can see below screen when the project is created :
Create Project Step 4
2. Create Service Account in GCP
a. Select API & Service-> Service Accounts
Create Service Account 1
b. On Service account page click on service account
Create Service Account 2
c. Select Home > Google Cloud Platform
Provide account details and click on CREATE
Service account name : terraform-gcp
Service account ID : terraform-gcp
Service account description : Access for terrafrom
Create Service Account 3
d. On Next screen for Service account permissions select Role as “Owner” and Click CONTINUE
Create Service Account 4
e. On next screen Click Create. A JSON file that contains your key downloads to your computer.
Create Service Account 5
3. Create VM (Compute Engine) with Terraform in GCP
a. As you can see we have copied above json file at below location :
[root@devops ~]# hostname
devops.hostbread.com
[root@devops ~]# cd /root/secret/
[root@devops secret]# ls
hostbread-d44243ebddf5.json
[root@devops secret]#
We will provide the key to Terraform in main.tf
b. Now lets create main.tf config file
[root@devops gcp-vm]# cat main.tf
provider “google” {
credentials = file(“/root/secret/hostbread-d44243ebddf5.json”)
project = “hostbread”
region = “us-central1”
zone = “us-central1-c”
}
resource “google_compute_instance” “vm_instance” {
name = “hostbread-tf-vm”
machine_type = “f1-micro”
boot_disk {
initialize_params {
image = “debian-cloud/debian-9”
}
}
network_interface {
# A default network is created for all GCP projects
#network = google_compute_network.vpc_network.self_link
network = “default”
access_config {
}
}
}
[root@devops gcp-vm]#
c. Now we initialize the project using “terraform init”
[root@devops gcp-vm]# terraform init
d. Next we provision the GCP VM using “terraform apply”. When prompted to confirm, type yes & press ENTER.
[root@devops gcp-vm]# terraform apply
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# google_compute_instance.vm_instance will be created
+ resource “google_compute_instance” “vm_instance” {
+ can_ip_forward = false
+ cpu_platform = (known after apply)
+ current_status = (known after apply)
+ deletion_protection = false
+ guest_accelerator = (known after apply)
+ id = (known after apply)
+ instance_id = (known after apply)
+ label_fingerprint = (known after apply)
+ machine_type = “f1-micro”
+ metadata_fingerprint = (known after apply)
+ min_cpu_platform = (known after apply)
+ name = “hostbread-tf-vm”
+ project = (known after apply)
+ self_link = (known after apply)
+ tags_fingerprint = (known after apply)
+ zone = (known after apply)
+ boot_disk {
+ auto_delete = true
+ device_name = (known after apply)
+ disk_encryption_key_sha256 = (known after apply)
+ kms_key_self_link = (known after apply)
+ mode = “READ_WRITE”
+ source = (known after apply)
+ initialize_params {
+ image = “debian-cloud/debian-9”
+ labels = (known after apply)
+ size = (known after apply)
+ type = (known after apply)
}
}
+ network_interface {
+ name = (known after apply)
+ network = “default”
+ network_ip = (known after apply)
+ subnetwork = (known after apply)
+ subnetwork_project = (known after apply)
+ access_config {
+ nat_ip = (known after apply)
+ network_tier = (known after apply)
}
}
+ scheduling {
+ automatic_restart = (known after apply)
+ on_host_maintenance = (known after apply)
+ preemptible = (known after apply)
+ node_affinities {
+ key = (known after apply)
+ operator = (known after apply)
+ values = (known after apply)
}
}
}
Plan: 1 to add, 0 to change, 0 to destroy.
Do you want to perform these actions?
Terraform will perform the actions described above.
Only ‘yes’ will be accepted to approve.
Enter a value: yes
google_compute_instance.vm_instance: Creating…
google_compute_instance.vm_instance: Still creating… [10s elapsed]
google_compute_instance.vm_instance: Creation complete after 16s [id=projects/hostbread/zones/us-central1-c/instances/hostbread-tf-vm]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
[root@devops gcp-vm]#
e. When we check google cloud console we see that we have provisioned a virtual machine on Google Cloud Platform :
GCP VM provisioned
f. At last we can destroy GCP VM using “terraform destroy”
[root@devops gcp-vm]# terraform destroy
When VM is destroyed we can see message :
Destroy complete! Resources: 1 destroyed.
Hope you have enjoyed reading this article. We have learned how to provision one VM in GCP.
Guess how much time you can save by deploying 100 such VMs automatically and without any human errors, using DevOps IaC (Infrastructure as code) tool – “Terraform “
Subscribe and follow Golibrary on Facebook and Linkedin to get all the updates.
Comments
comments
Prayag Sangode
***Linux, Cloud & Devops Architect & Technical Content Writer***
I am a Linux Enthusiast and Supporter/Promoter of Open Source Technology with over 12+ years of experience in Linux, Cloud and Devops.
I am A Technical Content writer for various sites like :
Hostbread
&
Golibrary
Tags :clouddevopsGCPterraformVM
Related Posts