What Is Infrastructure as Code (IaC)?
Are you setting up servers manually? Is every environment configured differently? Infrastructure as Code lets you define your infrastructure in code files, version-control it, and deploy it repeatably with a single command.
What Is IaC?
Infrastructure as Code is the practice of managing servers, networks, and databases through code files rather than manual processes. Instead of clicking through consoles, you write declarative code.
Traditional: Console → Click → Configure → "I forgot how I did it"
IaC: Write code → Git push → CI/CD → Automated setup
Why IaC?
- Repeatability — Deploy identical infrastructure across dev, staging, and production
- Version control — Track infrastructure changes with Git
- Speed — New environments in minutes, not days
- Consistency — Eliminate "snowflake server" drift
Declarative vs Imperative
| Approach | Description | Examples | |----------|-------------|---------| | Declarative | Define "what I want" | Terraform, CloudFormation | | Imperative | Write "how to do it" | Bash scripts, Ansible |
Terraform Example
provider "google" {
project = "my-project"
region = "europe-west1"
}
resource "google_cloud_run_service" "app" {
name = "my-app"
location = "europe-west1"
template {
spec {
containers {
image = "gcr.io/my-project/my-app:latest"
resources {
limits = {
memory = "512Mi"
cpu = "1"
}
}
}
}
}
}
output "url" {
value = google_cloud_run_service.app.status[0].url
}
terraform init # Download providers
terraform plan # Preview changes
terraform apply # Apply changes
terraform destroy # Tear down everything
IaC Tools Compared
| Tool | Language | Cloud | Approach | |------|----------|-------|----------| | Terraform | HCL | Multi-cloud | Declarative | | Pulumi | TypeScript/Python | Multi-cloud | Programmatic | | CloudFormation | YAML/JSON | AWS only | Declarative | | Ansible | YAML | Any | Imperative | | CDK | TypeScript/Python | AWS/GCP | Programmatic |
Best Practices
- Use remote state — Local state files don't work for teams
- Create modules — Modularize repeating infrastructure
- Separate environments — Separate state for dev/staging/prod
- Review plan output — Always read
terraform planbefore applying - Detect drift — Identify manual changes with regular plans
- Manage secrets properly — Use Vault or Secret Manager, not state files
Conclusion
IaC is a cornerstone of modern DevOps. Managing infrastructure as code brings repeatability, consistency, and speed. Even small projects benefit — you'll see the difference on your next deployment.
Learn IaC and DevOps practices on the LabLudus platform.