Skip to main content

Automating Infrastructure on Google Cloud with Terraform: Challenge Lab




As more and more organizations move to the cloud, the need for powerful infrastructure automation tools has never been greater. That's where Terraform comes in, a cutting-edge tool that allows you to automate the deployment, management, and scaling of your infrastructure with ease. With Terraform, you can define your entire infrastructure as code, making it easy to maintain, version, and reproduce. Whether you're deploying infrastructure to AWS, Google Cloud, or another cloud provider, Terraform provides a powerful and flexible platform for automating your workflows. In this blog post, we'll take a look at how to automate infrastructure on google cloud with terraform.


Task A. Create the configuration files 
Task B. Import infrastructure 
Task C.Configure a remote backend 
Task D. Modify and update infrastructure 
Task E.Destroy resources 
Task F. Use a module from the Registry 
Task G. Configure a firewall


................................................................................................................

Task A. Create the configuration files 

1.In Cloud Shell, create your Terraform configuration files and a directory structure that resembles the following:


touch main.tf
touch variables.tf
mkdir modules
cd modules
mkdir instances
cd instances
touch instances.tf
touch outputs.tf
touch variables.tf
cd ..
mkdir storage
cd storage
touch storage.tf
touch outputs.tf
touch variables.tf
cd

  1. Fill out the variables.tf files in the root directory and within the modules. Add three variables to each file: region, zone, and project_id. For their default values, use us-east1, <filled in at lab start>, and your Google Cloud Project ID.

variable "region" {
 default = "us-central1"
}

variable "zone" {
 default = "us-central1-a"
}

variable "project_id" {
 default = "<FILL IN PROJECT ID>"
}


3.Add the Terraform block and the Google Provider to the main.tf file. Verify the zone argument is added along with the project and region arguments in the Google Provider block

terraform {
  required_providers {
    google = {
      source = "hashicorp/google"
      version = "4.47.0"
    }
  }
}

provider "google" {
  project     = var.project_id
  region      = var.region

  zone        = var.zone
}


4.Initialize Terraform.
Run terraform init in Cloud Shell in the root directory to initialize terraform.

.............................................................

Task B. Import infrastructure:

1.
Go to Compute Engine > VM Instances. Click on tf-instance-1. Copy the Instance ID down somewhere to use later.

Go to Compute Engine > VM Instances. Click on tf-instance-2. Copy the Instance ID down somewhere to use later.

First, add the module reference to the end of  main.tf 

module "instances" {

  source     = "./modules/instances"

}

Next, Copy the following configuration into the file modules/instances/instances.tf:


resource "google_compute_instance" "tf-instance-1" {

  name         = "tf-instance-1"

  machine_type = "n1-standard-1"

  zone         = var.zone

  boot_disk {

    initialize_params {

      image = "debian-cloud/debian-10"

    }

  }

  network_interface {

 network = "default"

  }

metadata_startup_script = <<-EOT

        #!/bin/bash

    EOT

allow_stopping_for_update = true

}

resource "google_compute_instance" "tf-instance-2" {

  name         = "tf-instance-2"

  machine_type = "n1-standard-1"

  zone         = var.zone

  boot_disk {

    initialize_params {

      image = "debian-cloud/debian-10"

    }

  }

  network_interface {

 network = "default"

  }

metadata_startup_script = <<-EOT

        #!/bin/bash

    EOT

allow_stopping_for_update = true

}



2.use the terraform import command to import them into your instances module.

To import the first instance, use the following command, using the Instance ID for tf-instance-1 you copied down earlier.
terraform import module.instances.google_compute_instance.tf-instance-1 <Instance ID - 1>

To import the second instance, use the following command, using the Instance ID for tf-instance-2 you copied down earlier.
terraform import module.instances.google_compute_instance.tf-instance-2 <Instance ID - 2>


3.Apply your changes. Note that since you did not fill out all of the arguments in the entire configuration, the apply will update the instances in-place. 
terraform plan
terraform apply

.......................................................................................

Task C:Configure a remote backend 

1.Create a Cloud Storage bucket resource inside the storage module. For the bucket name, use Bucket Name.
Add the following code to the modules/storage/storage.tf file:

resource "google_storage_bucket" "storage-bucket" {
  name          = "Enter bucket_name"
  location      = "US"
  force_destroy = true
  uniform_bucket_level_access = true
}


Next, add the following to end of the main.tf file:
module "storage" {
  source     = "./modules/storage"
}


Run the following 2 commands in cloud shell:
terraform init
terraform apply


  1. Configure this storage bucket as the remote backend inside the main.tf file. Be sure to use the prefix terraform/state so it can be graded successfully. Next, update the main.tf file so that the terraform block looks like the following

terraform { 
  backend "gcs" {
    bucket  = "Enter bucket_name"
 prefix  = "terraform/state"
  }
  required_providers {
    google = {
      source = "hashicorp/google"
      version = "4.47.0"
    }
  }
}


3.Run the following to initialize the remote backend.

terraform init


....................................................................................................................

Task D: Modify and update infrastructure

1.Navigate to modules/instances/instance.tf. Replace the entire contents of the file with the following:
resource "google_compute_instance" "tf-instance-1" {
  name         = "tf-instance-1"
  machine_type = "n1-standard-2"
  zone         = var.zone
  allow_stopping_for_update = true

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-10"
    }
  }

  network_interface {
 network = "default"
  }
}

resource "google_compute_instance" "tf-instance-2" {
  name         = "tf-instance-2"
  machine_type = "n1-standard-2"
  zone         = var.zone
  allow_stopping_for_update = true

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-10"
    }
  }

  network_interface {
 network = "default"
  }
}

resource "google_compute_instance" "tf-instance-3" {
  name         = "tf-instance-3"
  machine_type = "n1-standard-2"
  zone         = var.zone
  allow_stopping_for_update = true

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-10"
    }
  }

  network_interface {
 network = "default"
  }
}

2.Run the following commands
terraform init
terraform apply




........................................................................................................
Task E.Destroy resources 

1.Taint the tf-instance-3 resource by running the following command:
terraform taint module.instances.google_compute_instance.tf-instance-3

2.Run the following commands to apply the changes:
terraform init
terraform apply


3.Remove the following chunk of code from instances.tf file:

resource "google_compute_instance" "tf-instance-3" {
  name         = "tf-instance-3"
  machine_type = "n1-standard-2"
  zone         = var.zone
  allow_stopping_for_update = true

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-10"
    }
  }

  network_interface {
 network = "default"
  }
}

4.run the following command:
terraform apply

......................................................................................................


Task F. Use a module from the Registry :


1.Copy and paste the following code at the end of the main.tf file:

module "vpc" {
    source  = "terraform-google-modules/network/google"
    version = "6.0.0"

    project_id   = "Enter your project id here"
    network_name = "Enter VPC Name"
    routing_mode = "GLOBAL"

    subnets = [
        {
            subnet_name           = "subnet-01"
            subnet_ip             = "10.10.10.0/24"
            subnet_region         = "us-east1"
        },
        {
            subnet_name           = "subnet-02"
            subnet_ip             = "10.10.20.0/24"
            subnet_region         = "us-east1"
            
        }
    ]
}

2.Run the following commands:
terraform init
terraform apply


3.Next, navigate to the instances.tf file and update the configuration resources to connect tf-instance-1 to subnet-01 and tf-instance-2 to subnet-02.
Navigate to modules/instances/instances.tf. Replace the entire contents of the file with the following:

resource "google_compute_instance" "tf-instance-1" {
  name         = "tf-instance-1"
  machine_type = "n1-standard-2"
  zone         = var.zone
  allow_stopping_for_update = true

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-10"
    }
  }

  network_interface {
 network = "Enter vpc_name"
    subnetwork = "subnet-01"
  }
}

resource "google_compute_instance" "tf-instance-2" {
  name         = "tf-instance-2"
  machine_type = "n1-standard-2"
  zone         = var.zone
  allow_stopping_for_update = true

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-10"
    }
  }

  network_interface {
 network = "Enter vpc_name"
    subnetwork = "subnet-02"
  }
}


4.Run the following commands:
terraform init
terraform apply

........................................................................................................


Task G. Configure a firewall:

1.Create a firewall rule resource in the main.tf file, and name it tf-firewall.
Add the following resource to the main.tf file and fill in the GCP Project ID:

resource "google_compute_firewall" "tf-firewall" {
  name    = "tf-firewall"
 network = "projects/<Enter your PROJECT_ID>/global/networks/Enter VPC_Name"

  allow {
    protocol = "tcp"
    ports    = ["80"]
  }

  source_tags = ["web"]
  source_ranges = ["0.0.0.0/0"]
}


2.Run the following commands:
terraform init
terraform apply

..........................................................................................................

Congratulations , You have completed the lab😀

Comments

Popular posts from this blog

Travel_Agencies

  Problem Statement Create a class TravelAgencies with below attributes: regNo – int agencyName – String pakageType – String price – int flightFacility – boolean Write getters, setters for the above attributes . Create constructor which takes parameter in the above sequence. Create class Solution with main method. Implement two static methods – findAgencyWithHighestPackagePrice and agencyDetailsforGivenIdAndType in Solution class. findAgencyWithHighestPackagePrice method: This method will take array of TravelAgencies objects as an input parameter and return the highest package price from the given array of objects. agencyDetailsForGivenldAndType method: This method will take three input parameters -array of TravelAgencies objects, int parameter regNo and String parameter packageType. The method will return the TravelAgencies object based on below conditions. FlightFacility should be available. The input parameters(regNo and packageType) should matched with the regNo

December 23 java coding question PRA solution

23 dec pra java coding question solution. Question: Create the class Student with below attributes. id-int name-String marks-int age-int write getters and setters and parametrized constructor in Student class. Create class Solution with main method. implement 2 static methods-findStudentWithMaximumAge and searchStudentById in Solution class. findStudentWithMaximumAge method:     This method will take the Array of the Students objects as input and  returns the Student object having maximum Age.   For this method,the main method should print the student object details with maximum age as it is.    searchStudentById method:     This method will take 2 input parameters.Array of the Students objects and an int value  id.and returns the Student object having the mentioned id  if found, else return null if not found.   For this method ,main method should print the details of Student objects  as it is,if the returned value is not null. or it should print  "No Student found with mentioned

TCS XPLORE CAMERA PROCTORED ASSESSMENT EXAM JAVA CODING SOLUTION 10 MAY,2021. FOOTWEAR PROBLEM.

 create a class Footwear which consists of the below attributes.   footwearId=int   footwearName=String   footwearType=String   price =int the above attributes should be private. write getter and setter and parametrised constructor as required. create the class footwearProgrammm with the main method. implement the 2 static methods.getCountByType and getSecondHighestPriceByBrand in the Solution class. getCountByType method:    this method will take two input parameters. array of the Footwear objects and string parameter footwear type. this method will return the count of the footwears from array of the footwear objects for the given type of footwear. if no footwear with the given footwear type is found in the array of footwear abjects,then the method should return 0. getSecondHighestPriceByBrand method:   this method will take 2 input parameters-array of footwear objects and string parameter inputFootwearName.the method will return the second highest footwear objects based on the price