Policy as code decision model
What a policy checks
A policy is a rule that evaluates evidence about a proposed infrastructure change. A guardrail is the mechanism that uses the decision to constrain a workflow. For a fictional bookstore, requiring private storage is a policy; preventing an unapproved apply when the rule fails is a guardrail. Valid HCL and a successful plan do not establish compliance. A policy decision does not itself configure a firewall or monitor a running application. Start by naming the protected asset, the requirement, the evidence needed and the action on failure.
Read the proposed actions
A saved Terraform plan can be rendered as structured JSON with the command below. Use only a reviewed plan in an isolated environment; the file can expose sensitive values. Do not send it to an external policy service without authorization.
terraform show -json lab.plan > lab-plan.jsonThis differs from the event stream produced by terraform plan -json. The resource_changes array contains resource addresses, types, modes and change objects. The actions list can represent create, update, no-op, delete or a replacement with both create and delete. The following abbreviated synthetic fragment is not a full Terraform plan and its provider is fictional.
{ "address": "fictional_storage.orders", "mode": "managed", "change": { "actions": [ "create", "delete" ], "after": { "public": false }, "after_unknown": { "region": true } }}The replacement has a deletion step despite the new object being private. The unknown region prevents a definitive region compliance decision. Inspect both action order and value evidence; do not infer a real resource name from a Terraform address.
Choose rules that can be measured
The training policy permits regions lab-north and lab-south, requires public=false, size between 1 and 4 inclusive, a nonblank owner tag and no delete action. These names and sizes are fictional, not cloud limits. Normalize the plan to a deliberately small record before evaluation; a production adapter must validate format version, resource type, nested unknown markers, missing fields, scope and completeness. Evaluate every relevant change, including module instances. Pure deletions must be evaluated using actions before accessing after, which can be null. A private flag alone cannot prove private connectivity; real policies must understand the provider schema and other access controls.
Complete evidence, all rules pass -> ALLOWKnown prohibited action or value -> DENYMissing or unresolved evidence -> UNKNOWNMandatory gate proceeds only on ALLOWA known violation takes precedence over missing evidence in this training model. An unknown value is not false, an absent property is not necessarily safe, and a null value may have provider-specific semantics. An empty set of findings is not sufficient if the evaluator skipped resources or failed to parse its input.
Try a local evidence source
This built-in resource stores a synthetic record rather than deploying cloud infrastructure. It provides an optional source for inspecting plan JSON, not a real storage resource.
terraform { required_version = "= 1.14.8"}resource "terraform_data" "inventory" { input = { region = "lab-north" public = false size = 2 owner = "inventory" }}output "inventory" { value = terraform_data.inventory.input}In a fresh authorized lab directory with the provisional CLI version, initialize and validate, then create a saved plan and inspect its JSON locally. No apply is required for policy practice. The literal input fields are known in the configuration; generated identity/output fields may still be unknown in the plan. No native run has been performed. The supplied case records are independent synthetic fixtures, not captured output.
Practice and verify
Classify the cases in lab-fixtures/policy-guardrails/cases.json using the six rules. Explain why unknown blocks a mandatory gate without claiming the resource is definitely public. Then identify which changes would require a separately authorized exception. A policy pass permits only the scope evaluated, not unrestricted apply. Next: integrate decisions into a workflow.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Policy as code decision model — knowledge check
Review infrastructure policy decisions and enforcement.
A valid Terraform plan proves which statement?
Which action list indicates replacement?
A required region is unknown at plan time. What should the training mandatory gate do?
Why protect plan JSON?
Which case passes the fictional size requirement?