Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Infrastructure as Code with Terraform

https://www.cloudskillsboost.google

Select - DevOps Engineer, SRE Learning Path

Select - Getting Started with Terraform for Google Cloud

High Level Objectives

  • Verify Terraform installation
  • Define Google Cloud as the provider
  • Create, change, and destroy Google Cloud resources by using Terraform

Skills

  • terraform version
  • Provider
  • create,change,destroy
  • terraform

Version Stack

Stack Version
Terraform 1.3.6

Check Terraform Installation

terraform --version

touch main.tf
  • main.tf
terraform {
  required_providers {
    google = {
      source = "hashicorp/google"
    }
  }
}
provider "google" {
  region  = "us-central1"
  zone    = "us-central1-c"
}
terraform init
  • Add the following to main.tf
resource "google_compute_instance" "terraform" {
  name         = "terraform"
  machine_type = "n1-standard-2"
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }
}
terraform plan
## The configuration fails with the following error. This is because you cannot configure a compute engine without a network.
  • Now add the network by including the following code segment to the google_compute_instance block.
network_interface {
    network = "default"
    access_config {
    }
}
terraform plan

terraform apply
  • Validate on console

Change the infrastructure

  • Add a tags argument to the instance we just created so that it looks like this:
resource "google_compute_instance" "terraform" {
  name         = "terraform"
  machine_type = "n1-standard-2"
  tags         = ["web", "dev"]
  # ...
}
terraform plan

terraform apply
  • Edit machine type
resource "google_compute_instance" "terraform" {
  name         = "terraform"
  machine_type = "n1-standard-1"
  tags         = ["web", "dev"]
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }
  network_interface {
    network = "default"
    access_config {
    }
  }
  allow_stopping_for_update = true
}
terraform plan

terraform apply
  • Validate

Destroy the Infrastructure

terraform destroy