Infrastructure as Code (IaC): Definition, Tools, Examples
Infrastructure as Code (IaC) manages infrastructure via versioned, declarative code — Terraform, Pulumi, CDK, Ansible. Replaces manual cloud clicks.
What is Infrastructure as Code (IaC)?
Infrastructure as Code (IaC) is the practice of managing infrastructure (servers, networks, databases, load balancers, DNS, IAM policies) through code instead of manual console clicks or one-off scripts. The code lives in version control, gets reviewed in pull requests, and is applied via CI/CD — exactly like application code.
IaC has become foundational for any non-trivial cloud deployment. AWS, Azure, GCP, and Kubernetes are all configurable via APIs; IaC tools wrap those APIs in higher-level languages and provide idempotency, drift detection, and rollback.
Why IaC?
- Reproducibility. Spin up identical environments (dev, staging, prod) from the same code.
- Version control. Every infrastructure change is git-tracked, diffable, reviewable.
- Disaster recovery. Lost a region? Re-deploy infrastructure from code in minutes.
- Documentation by default. The code IS the documentation.
- Auditability. Who changed what, when — visible in git history.
- Testing. Plan/preview changes before applying.
- Collaboration. Pull requests + reviews on infrastructure changes.
- Modularity. Reusable modules across teams/projects.
IaC tools comparison
| Tool | Type | Language | Best for |
|---|---|---|---|
| Terraform / OpenTofu | Declarative | HCL | Multi-cloud, most popular |
| AWS CloudFormation | Declarative | YAML/JSON | AWS-only, native |
| AWS CDK | Imperative | TypeScript, Python, Java | AWS, programmer-friendly |
| Pulumi | Imperative | TS, Python, Go, .NET | Multi-cloud, programmer-friendly |
| Azure Bicep | Declarative | Bicep DSL | Azure-only |
| Google Deployment Manager | Declarative | YAML | GCP-only (legacy) |
| Ansible | Imperative | YAML | Config management + IaC |
| Chef / Puppet | Imperative | Ruby DSL | Config management (legacy) |
| Kubernetes manifests / Helm | Declarative | YAML | K8s app deployment |
| Crossplane | Declarative | YAML (K8s CRDs) | K8s-native cloud control |
Declarative vs imperative IaC
| Aspect | Declarative (Terraform, CFN) | Imperative (Pulumi, CDK) |
|---|---|---|
| You describe | Desired end state | Steps to reach state |
| Tool figures out | How to get there | What you wrote |
| Idempotency | Built-in | You manage |
| Logic / loops | Limited | Full programming language |
| Learning curve | Quicker for ops | Quicker for devs |
Most teams now prefer declarative for predictability; imperative (Pulumi/CDK) wins for complex multi-cloud logic.
Terraform example
# main.tf
provider "aws" {
region = "eu-west-1"
}
resource "aws_s3_bucket" "website" {
bucket = "my-marketing-site"
tags = {
Environment = "production"
}
}
resource "aws_cloudfront_distribution" "cdn" {
origin {
domain_name = aws_s3_bucket.website.bucket_regional_domain_name
origin_id = "s3-website"
}
enabled = true
default_cache_behavior {
target_origin_id = "s3-website"
viewer_protocol_policy = "redirect-to-https"
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
}
# ... viewer cert, restrictions, etc.
}terraform init
terraform plan # Preview changes
terraform apply # Apply changesIaC workflow
- Write infrastructure code in a repo
- Open a pull request with the change
- CI runs
terraform plan+ posts diff to PR - Reviewer checks the plan
- Merge → CI runs
terraform apply - State + outputs stored in remote backend (S3 + DynamoDB lock)
IaC best practices
- Remote state with locking. S3 + DynamoDB (Terraform), AWS-managed (CFN). Prevents concurrent applies.
- Modularize. VPC module, RDS module, etc. Reusable across environments.
- Separate environments. Different state files per dev/staging/prod.
- Plan before apply. Always review the plan; never blindly apply.
- Pin tool + provider versions. Avoid surprises from upstream changes.
- Don't commit secrets. Use AWS Secrets Manager / Vault / SOPS.
- Use workspaces / aliasing for multi-region.
- Drift detection. Run plans regularly; investigate any drift.
- Policy-as-code. Sentinel, OPA, Checkov — enforce rules in CI.
- Tag everything. Cost allocation + ownership.
Common IaC pitfalls
- Manual changes drift the state. Someone clicks in console;
terraform planwants to revert. - State file in git. Contains secrets. Use remote backend.
- No state locking. Two engineers run apply simultaneously; corrupt state.
- Mega-monolith state. One state file for everything; blast radius huge. Split.
- Hardcoded values. Use variables + locals; parametrize for environments.
- Insufficient testing. Apply to staging first; never straight to prod.
- Ignored deprecation warnings. Provider/CFN deprecations bite later.
- Forgetting the destroy plan. When tearing down, plan + review carefully — destroy is irreversible.
FAQ: Infrastructure as Code
Terraform or CloudFormation?
Terraform if multi-cloud or you prefer HCL. CloudFormation if AWS-only and want native + free state management.
What's the difference between IaC and configuration management?
IaC provisions infrastructure (cloud resources). Config management (Ansible, Chef) configures the OS/apps inside provisioned hosts. Increasingly blurred — Ansible can do both.
Should I use modules?
Yes for anything reusable. Don't over-modularize prematurely; start simple, refactor when you have ≥3 use cases.
Where do I store Terraform state?
Remote backend with locking: S3 + DynamoDB on AWS, GCS on GCP, Azure Storage on Azure, Terraform Cloud, Spacelift, etc.
How do I handle secrets in IaC?
Reference secrets from external stores (AWS Secrets Manager, Vault). Never commit secrets to git. Use SOPS or git-crypt for encrypted-at-rest values.
What's drift?
When real infrastructure differs from what the code declares. Caused by manual changes. Run terraform plan regularly; reconcile or block manual changes.
Can I use IaC for Kubernetes?
Yes — Terraform, Pulumi, Crossplane, or Helm/Kustomize for K8s manifests. K8s is itself declarative.
Test IaC-deployed apps with LoadFocus
After IaC provisions your infrastructure, LoadFocus verifies it handles real traffic — running JMeter and k6 scripts from 25+ regions. Sign up free at loadfocus.com/signup.
Related LoadFocus Tools
Put this concept into practice with LoadFocus — the same platform that powers everything you just read about.