Codementor Events

Terraform in GCloud Shell: Learning Infrastructure Automation Made Simple

Published Nov 06, 2018Last updated May 04, 2019


Terraform in GCloud (image source: https://www.hashicorp.com/blog/kickstart-terraform-on-gcp-with-google-cloud-shell)

In the world of DevOps, automation is inevitable. We can automate anything and everything from the point of creating the application to the point of deploying it. These have made application development and deployment not just easy but fun. Automation equally makes processes faster and less prone to error.

For infrastructure automation, Terraform is just the right tool for the job. You can create or destroy an infrastructure within seconds using this tool. Its beauty also is that it can be used in many different cloud platforms.

Terraform in Google Cloud Shell

Recently, the Google Cloud Platform (GCP) launched a feature that enables using terraform in Google Cloud Shell; right there on your browser. I got so excited about this that I decided to write about it to help others know and use this awesome feature.

From my point of view, here are the advantages that this feature brings to the community:

  1. Makes learning Terraform with GCloud simple.
  2. Afford anyone the platform to quickly test out a concept without the hassle of setting it up.
  3. Save new DevOps engineers from the mistake of pushing there GCloud secret credentials to GitHub while learning Terraform. Pushing your credentials to the source control is a very dangerous practice.

Hands On

Prerequisite

To use the Terraform in Cloud Shell is very easy. The official blog of HashiCorp has the video and written document on how to use this feature. However, we will use this platform to create a simple subnet and a VM instance in the Compute Engine.

Steps:

  1. Click on the OPEN IN GOOGLE SHELL and Click on proceed when a prompt shows.
  2. By the top right-hand side of the page, create or select the project you want to create the resource on.
  3. Confirm the Project by the bottom right-hand.


Google Cloud Shell

  1. And we are good to write out terraform scripts on the Cloud Shell to create our resources.

  2. Go to File > New > File and create a new terraform file and name it network.tf

  3. Paste the below terraform script inside the newly created network.tf file.

resource "google_compute_network" "terraform-cloudshell" {
  name = "terraform-cloudshell-network"
  auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "terraform-cloudshell-subnet" {
  name = "terraform-cloudshell-subnetwork"
  ip_cidr_range = "10.0.0.0/16"
  region = "us-central1"
  network = "${google_compute_network.terraform-cloudshell.self_link}"
  
}
  1. Go to main.tf file and replace existing script with this updated terraform script.
resource "google_compute_instance" "terraform-cloudshell-instance" {
 name = "vm-instance-${local.name_suffix}"
 machine_type = "f1-micro"
 zone = "us-central1-a"
 allow_stopping_for_update = true

boot_disk {
  initialize_params{
   image = "${data.google_compute_image.debian_image.self_link}"
  }
 }

network_interface {
  subnetwork = "${google_compute_subnetwork.terraform-cloudshell-subnet.name}"
  access_config {
  }
 }
}

data "google_compute_image" "debian_image" {
 family = "debian-9"
 project = "debian-cloud"
}

The above script will create a Compute Engine VM instance in the subnet we created. In order to create the resources, we will run the following commands in the cloud shell terminal:

export GOOGLE_CLOUD_PROJECT=project_id
  1. Initialise Terraform
terraform init
  1. Create the resource with
terraform apply

Assuming there are no errors, you will be asked if you want to go ahead with the resources creation. To affirm, type “YES”

After a while, the instance will be created in the subnet we specified. Nice Job! You have just used Terraform in Cloud Shell to provision an infrastructure.

We have to destroy the resources we created in order for us not to incur charges.

  1. Destroy the resources we provisioned using Terraform
terraform destroy

That’s it. Thank you for reading.

Thank you Terraform and Google Cloud Team for this awesome feature.


Clap and Share

Discover and read more posts from Kene Nnamani
get started
post commentsBe the first to share your opinion
Show more replies