🏗️ Terraform & Infrastructure as Code (IaC)
1️⃣ What is IaC & Declarative vs Imperative
Definition: Infrastructure as Code (IaC) cloud servers, networks, load balancers aur databases ko cloud console par manually click karne ke bajay machine-readable configuration code files ke zariye automatically build, update aur version-control karne ki process hai.
| Criteria | Declarative (Terraform, CloudFormation) | Imperative (Ansible, Bash, Python SDK) |
|---|---|---|
| Philosophy | "What" – Desired final state define karo. | "How" – Step-by-step commands specify karo. |
| State Tracking | Current state track karke delta (diff) calculate karta hai. | Scripts bina existing state dekhe execute hoti hain. |
| Idempotency | 100% strictly idempotent by design. | Custom scripting ke bina repeat run par duplicate ban sakta hai. |
2️⃣ HashiCorp Configuration Language (HCL) Primitives
HCL human-readable aur machine-friendly declarative language hai:
# 1. Provider Declaration
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.aws_region
}
# 2. Resource Block
resource "aws_instance" "web_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = var.instance_type
tags = {
Name = "Production-Web-Node"
Environment = "Prod"
}
}
# 3. Output Block
output "public_ip" {
description = "Public IP address of the newly provisioned EC2 instance"
value = aws_instance.web_server.public_ip
}
3️⃣ The 4-Step Core Workflow
| Command | Internal Operation |
|---|---|
terraform init |
Working directory initialize karta hai, required cloud provider plugins download karta hai. |
terraform plan |
Desired state vs Real state compare karke execution graph (Add, Change, Destroy) preview karta hai. |
terraform apply |
Cloud APIs call karke actual infrastructure create ya modify karta hai aur state file update karta hai. |
terraform destroy |
Configuration file me defined sabhi cloud resources ko securely delete karta hai. |
4️⃣ Terraform State Architecture & Drift Detection
State File (terraform.tfstate): Ek JSON file jo aapke written HCL code aur cloud me actually bane physical resources ke beech direct mapping record karti hai.
terraform plan run karte hi state refresh karke is drift ko detect kar leta hai aur original state restore karta hai.
5️⃣ Remote Backends & State Locking
Production environments me state file ko local disk par rakhna dangerous hota hai (Accidental loss ya merge conflict risk):
# Backend configuration using AWS S3 & DynamoDB
terraform {
backend "s3" {
bucket = "company-tf-state-prod"
key = "global/s3/terraform.tfstate"
region = "ap-south-1"
dynamodb_table = "terraform-state-locks" # Enforces State Locking!
encrypt = true
}
}
terraform apply chalayein, toh DynamoDB table state ko lock kar deta hai taaki file corrupt na ho.
6️⃣ Reusable Terraform Modules
DRY (Don't Repeat Yourself) principle follow karne ke liye standardized components build karna:
# Calling a custom VPC module
module "vpc_networking" {
source = "./modules/aws-vpc"
vpc_cidr = "10.0.0.0/16"
public_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
environment_tag = "Staging"
}
7️⃣ Multi-Environment Management with Workspaces
# 1. List all available workspaces
terraform workspace list
# 2. Create and switch to staging
terraform workspace new staging
terraform workspace select staging
# 3. Dynamic resource sizing based on current workspace
resource "aws_instance" "app" {
instance_type = terraform.workspace == "prod" ? "t3.xlarge" : "t3.micro"
}