Infrastructure as Code
Manage cloud infrastructure with code for reproducible, version-controlled environments.
What you'll learn
- What Infrastructure as Code (IaC) is
- Benefits: reproducibility, versioning, review
- Declarative vs imperative approaches
- Popular IaC tools: Terraform, Pulumi, CDK
Concept
What Is IaC?
Infrastructure as Code (IaC) means defining your infrastructure — servers, networks, databases, load balancers — in configuration files rather than creating them manually through a cloud console. The configuration is version-controlled, reviewed, and applied with tools, just like application code. Instead of clicking through a web console to create a server, you write a file that describes the server and run a command to create it.
Benefits
IaC makes infrastructure reproducible — the same configuration always creates the same infrastructure, eliminating "it works in staging but not production" mysteries. It is version-controlled — every change is tracked in git, reviewable, and rollbackable. It is automatable — you can integrate it with CI/CD to test infrastructure changes before applying them. It is documentable — the code is the documentation of what exists. And it is shareable — new team members can understand the infrastructure by reading the code.
Declarative vs Imperative
Declarative IaC describes the desired end state: "I want 3 servers with a load balancer." The tool figures out how to get there. Terraform and CloudFormation are declarative. Imperative IaC describes the steps to take: "Create a server, then create a load balancer, then add the server to it." Declarative is generally preferred because it is simpler and handles drift — if someone manually changes the infrastructure, the next apply brings it back to the declared state.
Popular Tools
Terraform is the most popular IaC tool, using its own HCL language and supporting hundreds of providers. Pulumi lets you write infrastructure in real programming languages like TypeScript and Python. AWS CDK does the same for AWS specifically. Ansible focuses on configuration management — setting up servers after they exist. CloudFormation is AWS-native. Choose based on your cloud provider, team language preferences, and whether you need multi-cloud support.
Example
# Terraform: define infrastructure as code
# Save as main.tf:
# resource "aws_s3_bucket" "app" {
# bucket = "my-app-data-bucket"
# tags = { Environment = "production" }
# }
#
# resource "aws_instance" "web" {
# ami = "ami-0c02fb559f6fe2234"
# instance_type = "t3.micro"
# count = 2
# }
# Apply the infrastructure
terraform init # install providers
terraform plan # preview changes
terraform apply -auto-approve # create resources
terraform destroy # tear everything down
Terraform uses HCL to describe infrastructure declaratively. init installs providers, plan previews changes, apply creates resources, and destroy removes them.
Try it
- YAML Validator
Validate IaC configuration files.
- YAML to JSON
Convert IaC configs for inspection.
- Code Diff
Compare infrastructure changes.
Common mistakes
The mistake
Manually changing infrastructure that IaC manages
The fix
Never make manual changes to IaC-managed resources. The next terraform apply will overwrite them, causing confusion. Always make changes through the code and apply them.
The mistake
Not storing Terraform state securely
The fix
Store state in a remote backend (S3 + DynamoDB, Terraform Cloud) with locking. Local state files can be lost, cause conflicts, and may contain secrets.
Practice Infrastructure as Code
2 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.