Skip to main content

Command Palette

Search for a command to run...

Terraform Basic.

Updated
5 min read

1. What is Terraform? (In simple words)

Terraform is a tool to create and manage cloud infrastructure using code.

Instead of clicking in AWS console to create:

  • EC2

  • VPC

  • Load Balancer

  • S3

  • RDS

You write code once, and Terraform creates everything automatically.

This approach is called Infrastructure as Code (IaC).


2. Why Terraform is used in DevOps

In DevOps, we want:

Problem

Without Terraform

With Terraform

Create servers

Manual clicks

One command

Same setup in Dev & Prod

Different configs

Same code

Rollback infra

Hard

Easy

Infra tracking

No history

Git versioning

Team collaboration

Messy

Clean

Terraform makes infra:

  • Repeatable

  • Version-controlled

  • Automated

  • Fast

  • Less error-prone


3. When to use Terraform

Use Terraform when:

  • You are working with cloud (AWS, Azure, GCP)

  • You want same infra in Dev, QA, Prod

  • You are doing CI/CD pipelines

  • You want auto infra creation

  • You want to avoid manual cloud setup

  • You manage Kubernetes, servers, networking, storage

Do NOT use Terraform for:

  • Application code deployment (use CI/CD tools)

  • Small one-time manual setup (overkill)


4. Where Terraform is used (Industry use cases)

Terraform is used in:

  • Cloud infrastructure (AWS, Azure, GCP)

  • Kubernetes cluster setup (EKS, AKS, GKE)

  • Networking (VPC, Subnets, Security Groups)

  • Load balancers

  • Databases

  • Auto scaling infra

  • Multi-cloud setup

  • Disaster recovery infra

  • CI/CD pipelines

Used by companies like:
Netflix, Uber, Spotify, Swiggy, Zomato, startups, enterprises


5. How Terraform works (Flow)

Terraform works in 5 simple steps:

  1. Write code

  2. Initialize Terraform

  3. Preview changes

  4. Apply infra

  5. Destroy infra when needed

Command flow:

terraform init
terraform plan
terraform apply
terraform destroy

6. Important Terraform Concepts (DevOps perspective)

a) Provider

Tells Terraform which cloud to use

Example: AWS

provider "aws" {
  region = "ap-south-1"
}

b) Resource

Actual infra like EC2, S3, VPC

resource "aws_instance" "web" {
  ami           = "ami-0f58b397bc5c1f2e8"
  instance_type = "t2.micro"
}

c) Variables

To avoid hardcoding values

variable "instance_type" {
  default = "t2.micro"
}
instance_type = var.instance_type

d) Output

Show values after creation

output "public_ip" {
  value = aws_instance.web.public_ip
}

e) State file

Terraform keeps infra info in terraform.tfstate

Used for:

  • Tracking resources

  • Updating infra

  • Destroying infra

In industry, this is stored in S3 backend.


f) Backend (Remote State)

terraform {
  backend "s3" {
    bucket = "my-terraform-state"
    key    = "prod/terraform.tfstate"
    region = "ap-south-1"
  }
}

g) Modules

Reusable Terraform code

Used in companies to:

  • Avoid duplicate code

  • Follow standard infra templates


7. Industry Standard Terraform Setup (Step by Step)

This is how real companies use Terraform:

Step 1: Create Git repo

infra-terraform/
  ├── modules/
  ├── envs/
  │   ├── dev/
  │   ├── qa/
  │   └── prod/

Step 2: Setup Remote Backend (S3 + DynamoDB)

  • S3 for state file

  • DynamoDB for state locking

Why?

  • Team collaboration

  • Avoid state corruption


Step 3: Create IAM User for Terraform

Permissions:

  • EC2

  • VPC

  • S3

  • RDS

  • IAM (limited)

Never use root account.


Step 4: Write Terraform Code

provider "aws" {
  region = "ap-south-1"
}

resource "aws_instance" "app" {
  ami           = "ami-0f58b397bc5c1f2e8"
  instance_type = "t2.micro"
  tags = {
    Name = "dev-app-server"
  }
}

Step 5: Connect Terraform with CI/CD (GitHub Actions / GitLab CI)

Example flow:

  1. Developer pushes Terraform code

  2. Pipeline runs:

    • terraform init

    • terraform plan

  3. On approval:

    • terraform apply

This is how infra changes go to production safely.


8. Real Example (Create EC2 in AWS)

provider "aws" {
  region = "ap-south-1"
}

resource "aws_instance" "myserver" {
  ami           = "ami-0f58b397bc5c1f2e8"
  instance_type = "t2.micro"

  tags = {
    Name = "terraform-server"
  }
}

Run:

terraform init
terraform plan
terraform apply

Boom. EC2 created.


9. Advantages of Terraform

  • Works with all clouds

  • Infra as Code

  • Version control

  • Fast infra setup

  • Same infra everywhere

  • Easy rollback

  • Team collaboration

  • Automation friendly

  • Less manual errors

  • Supports CI/CD


10. Terraform vs Cloud Console (Interview Point)

Feature

Cloud Console

Terraform

Automation

Repeatability

Version control

Team collaboration

Rollback

Multi-cloud


11. Terraform Interview Questions (Very Important)

You should be ready to answer:

  • What is Terraform and IaC?

  • What is terraform state file?

  • Difference between terraform plan and apply

  • What is provider?

  • What is module?

  • How to manage secrets in Terraform?

  • How to use Terraform in CI/CD?

  • How to manage multiple environments?

  • How to avoid infra deletion by mistake?


12. How to learn Terraform fast (DevOps roadmap)

Week 1:

  • Basics: provider, resource, init, plan, apply

Week 2:

  • Variables, outputs, state, backend

Week 3:

  • Modules, remote state, CI/CD integration

Week 4:

  • Project: Create VPC + EC2 + Load balancer