Ravindra BagaleCourses & study guides Track your progress

Labs · DevOps

Lab: Learn the Terraform Workflow (init, fmt, validate, plan, apply, destroy) with the Local Provider, No Cloud Bill

Beginner30 minTerraform 1.6+ · Terminal · A text editor

Course: DevOps · Chapter 12: Terraform Introduction

Chapter 12 explains why Terraform and the basic workflow; this lab practises that workflow with zero cloud cost.

Chala mitrano! AWS account nasla tari Terraform shikta yeto. Aaj laptop varach init, plan, apply, destroy.

Suppose we are…

Suppose we have just joined a Netflix-style platform team, and the first rule we hear is: "Nobody clicks in the console; everything is Terraform." Before touching the company AWS account, we want to practise the workflow somewhere it cannot cost money or break anything.

In this lab Terraform creates a plain text file on your laptop. The resource is tiny, but the loop is exactly the one you will use for VPCs and databases: write, init, plan, apply, change, destroy.

Goal of this lab

By the end you will have:

  • Terraform installed and terraform version working.
  • One local_file resource with a variable and an output.
  • A full loop including a change, a manual delete that Terraform heals, and terraform destroy.

What you need

  • Terraform 1.6 or newer. On Windows: winget install Hashicorp.Terraform; on Mac: brew tap hashicorp/tap && brew install hashicorp/tap/terraform; on Linux, the HashiCorp package repository.
  • 30 minutes. No cloud account needed.

Part 1: Write the code

  1. Check the install and create a folder:

    terraform version
    mkdir tf-first && cd tf-first
    

    What you should see: Terraform v1.x.x.

  2. Create main.tf:

    main.tf

    terraform {
      required_version = ">= 1.6"
      required_providers {
        local = {
          source  = "hashicorp/local"
          version = "~> 2.5"
        }
      }
    }
    
    variable "team" {
      type    = string
      default = "platform"
    }
    
    resource "local_file" "welcome" {
      filename = "${path.module}/welcome.txt"
      content  = "Hello ${var.team} team, this file was created by Terraform.\n"
    }
    
    output "file_path" {
      value = local_file.welcome.filename
    }
    

Part 2: init, fmt, validate, plan, apply

  1. Download the provider:

    terraform init
    

    What you should see: Installing hashicorp/local v2.5.x and Terraform has been successfully initialized!

  2. Format and check the code:

    terraform fmt
    terraform validate
    

    What you should see: Success! The configuration is valid.

  3. See what would happen:

    terraform plan
    

    What you should see: + resource "local_file" "welcome" and Plan: 1 to add, 0 to change, 0 to destroy.

  4. Apply it and type yes:

    terraform apply
    cat welcome.txt
    

    What you should see: Apply complete! Resources: 1 added, the output file_path, and the file text with platform.

  5. Look at what Terraform remembers:

    terraform state list
    terraform output
    

    What you should see: local_file.welcome and file_path = "./welcome.txt".

Part 3: Change and drift

  1. Change the variable from the command line and plan:

    terraform plan -var team=devops
    

    What you should see: must be replaced because the content changed, and Plan: 1 to add, 0 to change, 1 to destroy.

  2. Delete the file by hand, then plan again:

    rm welcome.txt
    terraform plan
    

    What you should see: Terraform notices the file is missing and plans 1 to add. This is how it heals drift.

  3. Apply to bring it back:

    terraform apply -auto-approve
    

    -auto-approve is fine for a practice file on your laptop; in a team, read the plan and type yes.

Clean up: destroy everything

  1. Delete everything this lab created. Type yes when asked:

    terraform destroy
    

    What you should see: Destroy complete! Resources: 1 destroyed. and welcome.txt is gone.

Ravindra Bagale's Tip

Commit .terraform.lock.hcl to Git, but never .terraform/ or *.tfstate. The lock file pins provider versions for the whole team.

Common mistakes

MistakeWhat happensFix
Running plan before initMissing provider errorRun terraform init first
Editing terraform.tfstate by handState breaks, Terraform loses trackUse terraform state commands
Committing .terraform/ and stateHuge repo, secrets in GitAdd them to .gitignore
Using -auto-approve everywhereNo human reads the planOnly on throwaway practice

Self-check checklist

0 of 4 done

Try-at-home challenge

Add a second variable env with a validation block that only allows dev, stage or prod, and try terraform plan -var env=test to see the error.

Samajla ka? Write, init, plan, apply, destroy: the same loop for a file or a full VPC. Aata pudhe jaauya for_each.