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?