Infrastructure as Code and State

Summary

This note explains the core idea behind infrastructure as code and why state matters. The goal is to understand why IaC is more than writing config files: it is also about disciplined change, repeatability, and knowing what the system believes exists.

Terraform workflow from official documentation

Official Terraform workflow diagram showing the basic path from writing configuration to planning and applying managed infrastructure changes.

Why this matters

  • infrastructure as code is central to modern cloud and DevOps workflows
  • state handling is one of the most important ideas behind safe IaC use
  • cloud changes become much easier to review and explain when they are codified

Environment / Scope

ItemValue
Topicinfrastructure as code fundamentals
Best use for this noteunderstanding repeatable cloud change
Main focusdesired state, actual state, and controlled updates
Safe to practise?yes

Key concepts

  • Infrastructure as code - defining infrastructure through versioned files rather than manual portal clicking
  • Desired state - what the code says should exist
  • Actual state - what currently exists in the environment
  • State - the record a tool uses to track managed resources
  • Drift - when real infrastructure no longer matches the expected definition

Mental model

Think about the workflow like this:

code definition -> plan of change -> apply -> state update

IaC is powerful because it makes infrastructure changes reviewable, repeatable, and easier to troubleshoot later.

Everyday examples

SituationWhy IaC helps
rebuild a lab environmentsame setup can be recreated more consistently
review cloud changes before applyingplan stage shows intended changes
compare expected infra to realitystate and drift become visible
collaborate on infra workcode review is easier than portal memory

Common misunderstandings

MisunderstandingBetter explanation
”IaC means no more mistakes”it reduces manual drift, but design and state issues still matter
”The code file is the whole truth”the state and real environment still need to match
”Portal changes are harmless”manual changes can create drift and confusion
”If apply worked once, state is healthy forever”state can still drift or become inconsistent later

Verification

CheckExpected result
Desired state is readablecode reflects intended architecture
Plan is understandablechanges are visible before apply
State is healthymanaged resources map correctly
Drift is controlledactual environment matches code closely enough

Pitfalls / Troubleshooting

ProblemLikely causeWhat to check
Apply behaves unexpectedlydesired state or state file mismatchplan output and state
Infra differs from codedrift from manual changesrecent portal changes
Team is confused about what existsweak state disciplinecurrent state and code review
Fixes are hard to repeatenvironment was built manuallyIaC coverage

Key takeaways

  • IaC is about controlled, repeatable infrastructure change
  • state is a core part of understanding what your tool believes exists
  • cloud work gets easier when infrastructure is reviewable instead of remembered

Official documentation