Infrastructure as Code
Introduction
Nee AWS console la login panni, EC2 instance create pannuva, security group add pannuva, RDS setup pannuva — manually ellam click click click. Next day same setup another region la venum — again click click click. 😩
Infrastructure as Code (IaC) vandha — oru file ezhudhunga, terraform apply run pannunga — DONE! 10 servers, 5 databases, networking ellam 2 minutes la ready! 🚀
Indha article la IaC fundamentals, Terraform hands-on, AI infrastructure setup, best practices — ellam detail ah paapom! ⚙️
Why Infrastructure as Code?
Manual infrastructure management oda problems:
Without IaC 😰:
| Problem | Impact |
|---|---|
| Manual setup | Hours of clicking |
| No version history | "Yaar change pannadhu?" |
| Inconsistency | Dev ≠ Staging ≠ Prod |
| No rollback | Mistake fix panna impossible |
| Documentation | Always outdated |
| Scaling | Each server manual setup |
With IaC 😊:
| Benefit | How |
|---|---|
| **Automation** | One command — full setup |
| **Version control** | Git la track every change |
| **Consistency** | Same code = Same infra everywhere |
| **Rollback** | Git revert = Infra rollback |
| **Documentation** | Code IS the documentation |
| **Scaling** | count = 10 → 10 servers instant |
Real example: Netflix — thousands of servers manage panradhu IaC la. Manual ah possible eh illa! 🎬
IaC Tools Landscape
Popular IaC tools compare pannalam:
| Tool | Language | Cloud Support | Learning Curve | Best For |
|---|---|---|---|---|
| **Terraform** | HCL | Multi-cloud | Medium | Industry standard |
| **Pulumi** | Python/TS/Go | Multi-cloud | Easy (if dev) | Developers |
| **CloudFormation** | YAML/JSON | AWS only | Medium | AWS-only shops |
| **Bicep** | Bicep | Azure only | Easy | Azure users |
| **CDK** | TypeScript/Python | AWS | Medium | AWS + developers |
| **Ansible** | YAML | Multi-cloud | Easy | Config management |
Categories:
- 🏗️ Provisioning: Terraform, Pulumi, CloudFormation — create infrastructure
- ⚙️ Configuration: Ansible, Chef, Puppet — configure servers
- 📦 Containers: Docker Compose, Kubernetes YAML — app deployment
My recommendation: Terraform learn pannunga first — 80% companies use panradhu. Then Pulumi or CDK try pannunga. 🎯
Terraform — Core Concepts
Terraform oda building blocks:
1. Providers 🌐 — Cloud platform connection
2. Resources 🏗️ — What you want to create
3. Variables 📝 — Reusable values
4. Outputs 📤 — Display created resource info
5. State 💾 — Terraform tracks what it created
terraform.tfstatefile la current infra state store aagum- Remote state (S3 bucket) use pannunga team ku
Workflow: Write → Plan → Apply → Destroy 🔄
Terraform Workflow — Step by Step
Complete Terraform workflow:
Critical rule: ALWAYS run terraform plan before apply! Plan output paathuttu dhaan apply pannunga. Illana accidental delete aagum! ⚠️
Pro tip: CI/CD la terraform plan PR la run pannunga, terraform apply merge la run pannunga. Review + automation! 🛡️
AI Infrastructure with Terraform
Real-world AI project infrastructure setup:
Oru file la complete AI platform define aagiduthu! Training servers, model storage, database, API — ellam! 🤖
Terraform Modules — Reusable Components
Modules = Reusable infrastructure packages. Functions maari — oru thadava write pannunga, everywhere use pannunga!
Module structure:
Using a module:
Same module, different configs — staging ku 1 GPU, production ku 4 GPU. Code duplicate illa! 🧩
Public modules: Terraform Registry la 10,000+ community modules irukku. VPC, EKS, RDS — ready-made modules use pannalam!
State Management — Critical Topic
Terraform state file = Most important & dangerous file! ⚠️
What is state?
- terraform.tfstate — JSON file tracking your infrastructure
- Terraform idha use panni "what exists" vs "what should exist" compare pannum
NEVER do these:
❌ State file Git la commit pannaadheenga — secrets irukku!
❌ State file manually edit pannaadheenga — corrupt aagum!
❌ Two people same time apply pannaadheenga — state conflict!
❌ State file delete pannaadheenga — Terraform "forgets" everything!
Remote state setup (MUST for teams):
DynamoDB lock — two people same time apply try pannina, one wait pannum. State corruption prevent! 🔒
Multi-Environment Setup
Dev, Staging, Production — separate environments manage pannradhu:
Approach 1: Workspaces (Simple)
Approach 2: Directory Structure (Recommended)
Approach 3: Terragrunt (Advanced)
Recommendation: Start with Directory Structure. Team grow aana Terragrunt move pannunga! 📂
IaC Pipeline Architecture
┌──────────────────────────────────────────────────────┐ │ INFRASTRUCTURE AS CODE PIPELINE │ ├──────────────────────────────────────────────────────┤ │ │ │ 👨💻 Developer │ │ │ git push (*.tf files) │ │ ▼ │ │ ┌──────────┐ ┌───────────────┐ │ │ │ GitHub │───▶│ GitHub Actions │ │ │ │ PR │ │ CI/CD │ │ │ └──────────┘ └───────┬───────┘ │ │ ┌────▼────┐ │ │ │ tf init │ │ │ │ tf fmt │ │ │ │ tf plan │ ◀── PR Comment │ │ └────┬────┘ (plan output) │ │ │ │ │ ┌──────▼──────┐ │ │ │ Review │ ◀── Team approves │ │ │ & Merge │ │ │ └──────┬──────┘ │ │ │ │ │ ┌──────────▼──────────┐ │ │ │ terraform apply │ │ │ └──────────┬──────────┘ │ │ ┌───────────────┼───────────────┐ │ │ ▼ ▼ ▼ │ │ ┌────────────┐ ┌───────────┐ ┌────────────┐ │ │ │ Dev Infra │ │ Staging │ │ Production │ │ │ │ (auto) │ │ (auto) │ │ (approval) │ │ │ └────────────┘ └───────────┘ └────────────┘ │ │ │ │ 📦 State: S3 + DynamoDB Lock │ │ 🔒 Secrets: HashiCorp Vault / AWS Secrets Manager │ │ │ └──────────────────────────────────────────────────────┘
Pulumi — Developer-Friendly Alternative
Terraform ku HCL learn pannanum. Pulumi la unga favourite language la IaC ezhudhalaam!
Pulumi with Python 🐍:
Terraform vs Pulumi:
| Aspect | Terraform | Pulumi |
|---|---|---|
| Language | HCL (custom) | Python, TS, Go, C# |
| Loops/Logic | Limited | Full programming power |
| Testing | External tools | Native unit tests |
| Community | Massive | Growing |
| State | File/S3 | Pulumi Cloud (free) |
When to use Pulumi: Complex logic, loops, conditions venum na — Pulumi better. Simple infra ku Terraform podhum! 🎯
IaC Security Best Practices
Infrastructure security code level la enforce pannunga:
🔒 1. No hardcoded secrets
🔒 2. Least privilege IAM
🔒 3. Encryption everywhere
🔒 4. Security scanning
- tfsec — Terraform security scanner
- checkov — Policy-as-code scanner
- CI pipeline la add pannunga!
🔒 5. State encryption — Remote state always encrypt pannunga!
Prompt: Design AI Infrastructure
Summary
Key takeaways:
✅ IaC = Infrastructure code la define & manage pannradhu
✅ Terraform = Industry standard, multi-cloud, HCL language
✅ Modules = Reusable infrastructure components
✅ State = Remote backend + locking MUST for teams
✅ Security = No hardcoded secrets, encryption, scanning
✅ Environments = Directory structure for dev/staging/prod
Action item: AWS Free Tier account la Terraform install pannunga, EC2 instance oru main.tf la create pannunga. terraform apply run pannunga — magic feel pannunga! ✨
Next article: Monitoring AI Apps — observability deep dive! 📊
🏁 🎮 Mini Challenge
Challenge: Create EC2 Instance + GPU using Terraform
Infrastructure code la define → one command la deploy pannu! 🏗️
Step 1: Terraform Install 📦
Step 2: AWS Credentials Setup 🔑
Step 3: Terraform Configuration Create 📝
Step 4: Initialize & Deploy 🚀
Step 5: Connect & Run 🔌
Step 6: Destroy (cleanup) 🧹
Completion Time: 2-3 hours
Skills: AWS, Terraform, Infrastructure as Code
Cost: ~$5-10 for usage ⭐
💼 Interview Questions
Q1: Terraform state file — why important? Security concerns?
A: State file = current infrastructure snapshot (what resources exist, IDs, attributes). Terraform read state, compare desired state, plan changes. Important: destroy safe, updates idempotent. Security: sensitive data (passwords, keys) state file la store — encryption needed, version control avoid.
Q2: Terraform modules — reusable code — structure best practice?
A: Module = directory with main.tf, variables.tf, outputs.tf. Input variables: customization. Outputs: other modules consume pannalam. Structure: root module, child modules (networking, compute, database). Example: VPC module reuse multiple environments.
Q3: dev/staging/prod environments — Terraform la manage?
A: Option 1: separate directories (dev/, staging/, prod/) — each own state. Option 2: workspaces (terraform workspace new prod) — same code, separate state. Option 2 simpler but careful (accidental prod delete risk). Recommendation: separate directories (safety), plus variable files (dev.tfvars, prod.tfvars).
Q4: Terraform version control — state file commit pannala?
A: No! State file .gitignore. Remote backend use (AWS S3 + DynamoDB lock). Team: shared state (everyone up-to-date), locking prevents conflicts. State file only locally backup, or remote backend git push.
Q5: Terraform plan output — false positive warnings?
A: Plan shows exact changes. Review carefully! Force new (instance recreate, data loss possible). Sensitive outputs hide (secrets show illa). Targets: specific resource deploy (terraform apply -target=aws_instance.ai_gpu). Destruction dry-run: terraform plan -destroy (safe check before destroy).
Frequently Asked Questions
Terraform state file eppadi manage pannanum?