Overview
Infrastructure as Code (IaC) defines servers, networks, databases, and permissions in version-controlled files instead of clicking in consoles. Tools read the code, compare it with the real infrastructure, and create, update, or delete resources to match. Declarative tools (Terraform, CloudFormation, Pulumi) describe the desired end state.
IaC makes infrastructure reproducible (identical staging and production), reviewable (pull requests), auditable (git history), and recoverable (rebuild after disaster). It is usually applied through CI/CD pipelines with plan previews, policy checks, and remote state with locking.
Instead of telling builders what to do on site each day, you hand them blueprints. Anyone can build the same house again from the blueprints, and changes are reviewed on paper before construction.
When to use it
- Any cloud infrastructure beyond experiments.
- Multiple environments that must stay consistent.
- Compliance and audit requirements.
- Disaster recovery and region replication.
Where it shows up in interviews
Recognize it when: staging and production drift.
- Design a platform for 50 microservices
- Set up multi-region DR
Recognize it when: infrastructure changes need review and audit.
- Design a compliant cloud landing zone
Where it is used in real software
The most widely used multi-cloud IaC tool, with providers for AWS, Azure, GCP, Kubernetes, and SaaS.
AWS-native templates; CDK lets you write them in TypeScript, Python, or Java.
IaC in general-purpose languages with the same desired-state model.
Key terms
- Declarative
- Describe what you want; the tool figures out how.
- State
- Record of managed resources and their IDs (Terraform state).
- Plan / apply
- Preview changes / execute them.
- Drift
- Real infrastructure differing from code.
- Module
- Reusable group of resources.
How it works, step by step
- 1Write resources as code
VPC, databases, IAM, in modules.
- 2Store state remotely with locking
S3 + DynamoDB lock, or Terraform Cloud.
- 3Open a pull request
CI runs fmt, validate, plan, and policy checks.
- 4Review the plan
Watch for destroys and replacements.
- 5Apply from CI
After approval; detect drift regularly.
Manual vs IaC
Managing infrastructure for three environments
| Aspect | Console clicks | Infrastructure as Code |
|---|---|---|
| Consistency | Environments drift | Same modules, different variables |
| Review | None | Pull requests with plans |
| History | Hard to audit | Git history |
| Disaster recovery | Rebuild from memory | Re-apply code |
| Speed at scale | Slow and error prone | Fast, repeatable |
NOWAspect: Consistency | Console clicks: Environments drift | Infrastructure as Code: Same modules, different variables
Treat infrastructure like application code: versioned, reviewed, tested, and deployed through pipelines.
Implementation
terraform { backend "s3" { bucket = "acme-tf-state" key = "prod/network.tfstate" region = "us-east-1" dynamodb_table = "tf-locks" # prevents concurrent applies encrypt = true }} module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "~> 5.0" name = "prod" cidr = "10.0.0.0/16" azs = ["us-east-1a", "us-east-1b", "us-east-1c"] private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] public_subnets = ["10.0.101.0/24", "10.0.102.0/24", "10.0.103.0/24"] enable_nat_gateway = true} resource "aws_db_instance" "orders" { identifier = "orders-prod" engine = "postgres" engine_version = "16" instance_class = "db.r7g.large" multi_az = true storage_encrypted = true deletion_protection = true manage_master_user_password = true # password in Secrets Manager, not in code db_subnet_group_name = aws_db_subnet_group.private.name allocated_storage = 100 username = "app"}Complexity and performance
Grows with resources per state.
Split states by layer.
Trade-offs
Declarative tools handle ordering and diffs; imperative scripts are flexible but brittle.
Large states are slow and risky; many small states need cross-references.
Variants and related techniques
Argo CD applies manifests from Git continuously.
OPA, Sentinel, or Checkov block insecure changes.
Common mistakes
- Secrets in IaC files or state.
Fix: Use secret managers; encrypt and restrict state access.
- Manual console changes.
Fix: Cause drift; enforce changes through code and detect drift.
- Applying without reviewing the plan.
Fix: Plans reveal destructive replacements; review and protect critical resources.
Interview questions
Why use Infrastructure as Code?
It makes infrastructure reproducible, reviewable, auditable, and recoverable; enables consistent environments; and lets infrastructure changes flow through the same CI/CD and review practices as code.
How do you manage Terraform state safely?
Store it remotely with encryption and locking (S3 + DynamoDB or Terraform Cloud), restrict access, split state by environment and layer, and never edit it manually except through state commands.
Practice problems
| Problem | Difficulty | What it trains |
|---|---|---|
| Write Terraform for an S3 bucket with lifecycle rules | Easy | Basics. |
| Design IaC for dev, staging, prod across accounts | Medium | Modules and state. |