Infrastructure as Code Principles
Why infrastructure belongs in code, the difference between declarative and imperative tools, and the provisioning vs configuration split.
Beginner · about 3 hours · difficulty 2/5
Learn first: Git Fundamentals, Cloud Computing Concepts
Clicking in a console does not scale: nobody can review it, repeat it or roll it back. Infrastructure as Code (IaC) stores the desired state of your infrastructure in files, reviews changes through pull requests, and applies them with a tool.
Declarative vs imperative
- Declarative tools (Terraform, CloudFormation, Kubernetes manifests) describe the end state; the tool works out the steps.
- Imperative scripts describe steps; rerunning them may break things unless you write the checks yourself.
Declarative tools are idempotent by design: applying the same code twice changes nothing the second time.
Two layers of work
- Provisioning creates resources: networks, VMs, databases, DNS records, IAM roles. Terraform, Pulumi, CloudFormation, Bicep.
- Configuration management configures what runs inside machines: packages, files, users, services. Ansible, Chef, Puppet, Salt.
With containers and immutable images, configuration management shrinks: you bake an image once (Packer, Dockerfile) and replace machines instead of patching them.
Practices that matter
- Drift: changes made outside the code. Detect it and either import or revert it.
- Environments from the same code with different inputs, not copy-pasted folders.
- Plan before apply, and have a second person review the plan.
- Small blast radius: split state so one mistake cannot delete everything.
- Immutable infrastructure: replace, do not mutate.
Good enough to move on: you can explain idempotency, drift and immutable infrastructure, and say which tool category you would use to create a VPC and which to install a package on a VM.
Key concepts
- Declarative vs imperative
- Idempotency
- Provisioning vs configuration management
- Drift detection
- Immutable infrastructure and golden images
- Packer for machine images
Mini project
Take something you built by hand in the cloud console, write down every setting you chose, and list which parts would become provisioning code and which configuration code.
Check yourself
Someone changed a security group rule in the console after it was created by code. What is this called, and what are your two options for fixing it?
Free resources
- What is Infrastructure as Code with Terraform? (HashiCorp)
- Packer documentation (HashiCorp)