Infrastructure as Code (IaC)
Infrastructure as Code (IaC) is the practice of managing infrastructure (servers, networks, databases, load balancers) through declarative configuration files rather than manual setup through cloud consoles or CLI commands.
IaC enables version control for infrastructure (track changes in Git), reproducibility (create identical environments from the same config), automation (provision entire environments with one command), and consistency (eliminate configuration drift between environments).
Popular IaC tools include Terraform (multi-cloud, declarative, HCL syntax), AWS CloudFormation (AWS-specific), Pulumi (use programming languages instead of DSLs), and Ansible (procedural configuration management).
A typical IaC workflow involves writing configuration, planning changes (terraform plan shows what will change), applying changes (terraform apply creates/updates resources), and storing state (tracking what's currently deployed). Infrastructure changes go through the same code review process as application code.
How it works
Resources are described in configuration files that live in version control. Terraform's documentation puts it as defining cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share. Changes then go through the same review as application code.
Declarative tools work from desired state. Terraform's plan step compares the configuration with what already exists and produces an execution plan of what it will create, update or destroy; apply carries out the approved plan, ordering operations by resource dependencies.
To know what already exists, Terraform keeps state: bindings between real objects in the remote system and the resources declared in configuration. By default this is a local file named terraform.tfstate. HashiCorp says not to edit it by hand, and warns that keeping it in version control or without locking can lose data or expose secrets stored in it, so teams usually use a remote backend.
Procedural approaches list the steps to run rather than the end state, and configuration-management tools typically set up software on machines that already exist. Many setups combine the two: a declarative tool provisions networks and servers, and an image or configuration tool decides what runs on them.
Example
The resource block from Terraform's documentation, and the workflow that turns it into a real server.
resource "aws_instance" "web" {
ami = "ami-a1b2c3d4"
instance_type = "t2.micro"
}terraform init # download the provider plugins this config needs
terraform plan # show what will be created, changed or destroyed
terraform apply # make the changes and record the instance in state Edit instance_type and run plan again: Terraform compares the file with its state and shows only the difference before anything is touched. Delete the block and plan shows the instance being destroyed. The file, not a console click history, is the record of what should exist.
Common confusion
What is the difference between infrastructure as code and configuration management?
Infrastructure-as-code tools usually provision the infrastructure itself: networks, load balancers, databases, virtual machines. Configuration-management tools usually configure software on machines that already exist: packages, files, services. The categories overlap, and both keep their definitions in version-controlled files.
How does infrastructure as code relate to CI/CD?
Infrastructure as code describes the environment; CI/CD is the automated pipeline that tests and ships changes. They meet when infrastructure changes go through the pipeline too, for example running a plan on every pull request so reviewers see the exact diff, and applying it after merge.
Related tools & demos
- CI/CD The pipeline that runs plan and apply for you.
- Docker Compose Generator Declarative config for a local multi-container stack, the same desired-state idea at small scale.
- Kubernetes Another desired-state system, for workloads rather than cloud resources.
- Containerization Packaging what runs on the infrastructure you provision.
Further reading
- What is Terraform? HashiCorp Developer
- State HashiCorp Developer
- Resource blocks HashiCorp Developer
Want to learn more?
Explore more developer terms or read in-depth articles on the blog.
Browse all terms