IaC review and policy as code
Configuration, plan and reality
Infrastructure as code (IaC) describes desired infrastructure in versioned files. Static lint can detect malformed or disallowed declarations; a plan can describe proposed changes in context; actual deployment changes resources; runtime evidence shows observed behavior. These are separate stages. Terraform speculative plans describe proposed effects without intent to apply. Even planning may contact providers or expose sensitive state, so this chapter uses invented normalized JSON instead.
Make policy explicit
Policy as code turns an agreed requirement into a versioned decision rule with input, output and tests. OPA evaluates structured input using Rego, a declarative policy language. A policy decision alone changes nothing unless a caller enforces it. The offline Python alternative below illustrates equivalent Boolean decisions for one classroom requirement, not Rego syntax or a complete provider policy engine.
An executable classroom policy
The fictional storage policy requires private access and encryption. Missing values fail closed.
def allowed(record): return record.get("public") is False and record.get("encrypted") is True
assert allowed({"public": False, "encrypted": True})assert not allowed({"public": False})assert not allowed({"public": True, "encrypted": True})The strict Boolean comparisons reject ambiguous strings such as "false". A real review also needs resource type, inherited policy, key access and data classification.
Review drift and unknowns
Drift is a difference between the intended and observed resource configuration. Compare snapshots with their collection times and scope. A missing observation is unknown, not compliant. Investigate authorized emergency changes and reconcile through reviewed configuration rather than blindly overwriting live resources. Protect plan and state data as sensitive evidence.
Practice a static review
A mock storage record says public=false and encrypted=true, but the live snapshot is absent. Report static policy pass and runtime unverified separately. For a second record with public=true, suggest the desired private setting and a review of legitimate consumers before change. No provider connection, plan command or apply is needed.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
IaC review and policy as code — knowledge check
Review CI trust, configuration policy, evidence and approvals.
What does static lint establish?
What does Rego provide in OPA?
What happens to missing encryption in the example?
What is configuration drift?
Why is a policy decision not enforcement by itself?