Without infrastructure as code, someone builds the production environment by clicking through a cloud console. Nobody can say exactly what was configured, staging drifts away from production, and rebuilding after an outage depends on memory. Infrastructure as code (IaC) replaces the clicks with text files. The files live in Git, go through code review and are applied by a tool that talks to the cloud provider's API.
How it works
Most IaC tools are declarative. You describe the end state ("one Postgres database, encrypted, in two availability zones") and the tool works out the API calls needed to get there. The workflow has three steps:
- Write the desired resources in configuration files.
- Plan: the tool compares the files with what exists and prints the changes it would make (create, update, destroy).
- Apply: after review, the tool makes those changes and records the result in a state file, so the next plan knows what it already manages.
The plan step is what makes IaC safe. A reviewer sees "1 to add, 0 to destroy" before anything touches production. In a CI/CD pipeline, the plan runs on every pull request and the apply runs after merge.
Example: an encrypted bucket in Mumbai
This Terraform (or OpenTofu) file creates a private S3 bucket in the ap-south-1 region for invoice PDFs:
provider "aws" {
region = "ap-south-1"
}
resource "aws_s3_bucket" "invoices" {
bucket = "acme-prod-invoices"
}
resource "aws_s3_bucket_public_access_block" "invoices" {
bucket = aws_s3_bucket.invoices.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
Running terraform plan shows two resources to add. Copy the folder, change the bucket name and you have an identical staging bucket. If someone later turns off the public access block in the console, the next plan reports the drift and offers to put it back.
The main tools
- Terraform (HashiCorp, now part of IBM) uses the HCL language and has the largest provider ecosystem. It moved from the MPL 2.0 licence to the Business Source License in August 2023.
- OpenTofu is the open-source fork of Terraform under the Linux Foundation, still licensed MPL 2.0.
- Pulumi lets you write infrastructure in TypeScript, Python, Go, C# or Java.
- AWS CloudFormation and the AWS CDK, and Azure Bicep, are cloud-specific options.
- Ansible is often used next to them to configure software inside servers.
We compare the first three in Terraform vs OpenTofu vs Pulumi.
IaC and security
Because infrastructure is text, you can scan it before it exists. Policy checks in the pipeline can block a public bucket, an open security group or a database without encryption. Keep credentials out of the files and the state, and pull them from a secrets manager instead. State files can contain sensitive values, so store them in an encrypted, access-controlled backend, never in Git.
Common mistakes
- Mixing IaC and console changes. Every manual fix becomes drift that the next apply may undo.
- One huge state for everything. A small change then needs a slow, risky plan across the whole estate. Split by environment and component.
- No disaster recovery test. IaC lets you rebuild a region quickly, but only if you have actually tried it.
We set up IaC, pipelines and cloud accounts as part of our cloud and infrastructure work.