☁️ Cloud-Native Infrastructure & Terraform (IaC)
1️⃣ Declarative vs Imperative IaC & Foundations
Definition: Infrastructure as Code (IaC) ek DevOps practice hai jisme cloud computing resources (VPCs, Subnets, Compute Instances, Kubernetes clusters) ko manual console clicks ke bajay version-controlled machine-readable definition files ke zariye provision aur manage kiya jata hai.
| Criteria | Imperative Approach (Bash / AWS CLI / Python SDK) | Declarative Approach (Terraform / OpenTofu) |
|---|---|---|
| Execution Logic | Aapko batana padta hai "HOW to do it" (step-by-step procedure likhna padta hai). | Aap sirf batate hain "WHAT the end state should be" (Desired state declare karna). |
| Idempotency | Script ko 2 baar run karne par duplicate resources banne ka khatra rehta hai. | Strictly Idempotent: Desired state match hone par engine zero modifications karta hai. |
| State Tracking | No native state ledger; cloud provider par manually inspect karna padta hai. | Managed State File maintains mapping between real-world infrastructure and code. |
2️⃣ HashiCorp Configuration Language (HCL) Primitives
Terraform configuration blocks provider ecosystem ke sath communicate karte hain:
terraform {
required_version = ">= 1.8.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.aws_region
}
# Resource Block: Creates AWS VPC
resource "aws_vpc" "production_vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "fresherjobs-production-vpc"
Environment = var.environment
}
}
# Variable Block
variable "aws_region" {
type = string
default = "ap-south-1" # Mumbai Region
description = "Primary deployment AWS region"
}
3️⃣ The Terraform Core Engine Lifecycle
| Lifecycle Command | Internal Engine Mechanism |
|---|---|
terraform init |
Working directory initialize karta hai, required providers pull karta hai (.terraform/) aur remote backend configure karta hai. |
terraform plan |
Live infrastructure ko state file se reconcile karta hai, dependency graph construct karta hai aur execution diff calculate karta hai. |
terraform apply |
Plan par approval milne ke baad targeted Cloud Provider APIs par actual CRUD HTTP requests execute karta hai. |
terraform destroy |
State file me tracked sabhi managed resources ko dependency order ke reverse sequence me tear down karta hai. |
4️⃣ State Architecture, Remote Backends & Locking
Terraform ka terraform.tfstate file infrastructure ka single source of truth hota hai. Production me local state commit karna team corruption ka karan banta hai:
• S3 Bucket: State file ko encrypted format (AES-256 / KMS) me version-controlled store karta hai.
• DynamoDB Table:
LockID string primary key ke zariye state locking provide karta hai taaki do developers ek sath apply execute na kar sakein.• Sensitive Data Leakage: State file me database passwords aur certificates plaintext me hote hain; S3 bucket IAM permissions ko tightly restrict karna mandatory hai.
terraform {
backend "s3" {
bucket = "fresherjobs-terraform-state-prod"
key = "vpc/terraform.tfstate"
region = "ap-south-1"
encrypt = true
dynamodb_table = "terraform-state-locking-table"
}
}
5️⃣ Terraform Modules: Composition & Reusability
Duplicate code likhne ke bajay parameterized, reusable building blocks construct karna:
# Standard Module Directory Structure:
# modules/microservice_cluster/
# ├── main.tf → Resource implementations
# ├── variables.tf → Input parameters with validation rules
# └── outputs.tf → Exported attributes (e.g. Cluster endpoint, Security Group ID)
module "production_ecs_cluster" {
source = "./modules/microservice_cluster"
cluster_name = "fresherjobs-api-cluster"
node_count = 5
instance_type = "c6g.xlarge"
environment = "production"
}
output "cluster_api_endpoint" {
value = module.production_ecs_cluster.endpoint
description = "Public Load Balancer FQDN"
}
6️⃣ Drift Detection & Resource Import Workflows
| Operational Challenge | Detection & Remediation Workflow |
|---|---|
| Configuration Drift | Kisi engineer ne AWS web console me jakar manual security group port open kar diya. terraform plan -refresh-only se drift capture karein aur apply karke desired state par revert karein. |
| Brownfield Adoption | Pehle se bane cloud resources ko Terraform ke control me laane ke liye native import block use hota hai:import { to = aws_s3_bucket.legacy_assets id = "fresherjobs-legacy-assets" } |
7️⃣ IaC Security, Policy-as-Code & OpenTofu
• License Divergence: HashiCorp dwara BSL 1.1 license adopt karne ke baad Linux Foundation ke under OpenTofu fork banaya gaya jo 100% open-source (MPL 2.0) hai.
• Client-Side State Encryption: OpenTofu native state encryption provide karta hai taaki cloud storage par state files directly client-side encrypted rahein.
• Policy-as-Code: CI/CD pipeline me infrastructure deploy hone se pehle security scans run karna via Checkov, Tfsec ya Open Policy Agent (OPA) (e.g. Reject plan if any S3 bucket has public read access).