Infrastructure as Code Concepts
Understand why infrastructure belongs in code, declarative vs imperative tools, state, drift, modules and the plan-review-apply workflow.
Intermediate · about 3 hours · difficulty 2/5
Learn first: Git and GitHub for Infrastructure Work, VPC, Subnets, Route Tables and Internet Gateways
Clicking through the console does not scale, cannot be reviewed and is impossible to reproduce exactly. Infrastructure as Code (IaC) describes your cloud resources in files kept in Git, so environments are repeatable, reviewable and easy to destroy.
Declarative vs imperative
- Declarative tools (Terraform, CloudFormation, Bicep) describe the desired end state; the tool works out what to create, change or delete.
- Imperative scripts (CLI commands, boto3) describe steps. Fine for one-off tasks, fragile for full environments.
- CDK and Pulumi let you write real programming languages that produce declarative templates.
Key ideas
- State: the tool's record of what it manages. Terraform keeps a state file; CloudFormation keeps state inside AWS as stacks.
- Plan / change set: preview exactly what will change before applying.
- Drift: someone changed a resource by hand, so reality no longer matches code.
- Modules / constructs: reusable building blocks (a standard VPC, a standard service).
- Environments: the same code with different variables for dev, staging and prod.
The workflow
Write code, open a pull request, review the plan output in CI, merge, apply from the pipeline. Humans should rarely apply from laptops in production.
Pitfalls
- Mixing console changes with IaC-managed resources.
- One giant configuration for everything, so every change risks the whole account.
- Hardcoding secrets in IaC files.
You are ready to move on when you can explain state, plan and drift, and why IaC makes tearing down your labs safer than clicking.
Key concepts
- Declarative vs imperative
- State and drift
- Plan and change sets
- Modules and reuse
- Environments and variables
- Immutable infrastructure
- Policy as code: Checkov, OPA, cfn-guard
Mini project
List every resource you created by hand for the VPC and EC2 labs, then write the same list as a table with resource type, key settings and dependencies. This becomes your blueprint for the IaC topics next. Paper exercise, no cost.
Check yourself
What is drift, how would you detect it, and why is it dangerous?
Free resources
- What is Infrastructure as Code? (AWS)
- Terraform documentation (HashiCorp)
- Terraform roadmap (roadmap.sh)