Terraform Security and Governance · Topic 3 of 3
Policy Rules and Risky Changes
Terraform Professional (002): layer core and project policy sets, pick enforcement levels, read dangerous plans and apply lifecycle safeguards.
- 5 min
- A policy set uses one framework, and scope can be global, projects, workspaces or tags.
- Policies evaluate only after a successful plan and before apply.
- Advisory warns; soft-mandatory can be overridden, as can OPA mandatory when the set allows it; hard-mandatory in policy checks cannot.
What it is
Governance has two layers. Policy sets apply automatic rules to every run, from organization-wide compliance down to rules for one team or project. Plan review and lifecycle settings catch what rules cannot, such as a replacement that would lose data.
Both act before anything changes: policies are checked after the plan and before the apply, and humans read the same plan.
How it works
Layering policy sets
A policy set holds policies from one framework, Sentinel or OPA, and a workspace can have sets from both frameworks applied. The scope you choose decides who is covered.
| Requirement | Scope |
|---|---|
| Core compliance for everyone | Global, with optional exclusions |
| Rules for one team's project | Selected projects |
| Rules for specific workspaces | Selected workspaces |
| Rules for a class of workspaces | Tags |
Typical design: a global set holds the rules nobody may skip, such as approved regions and private module use. A project set holds the team's own rules, such as allowed instance sizes.
Keep each team's workspaces in a project so that a project-scoped set also covers future workspaces. Managing sets needs the manage policies permission.
Enforcement levels
| Framework | Level | When the policy fails |
|---|---|---|
| Sentinel, OPA | advisory | Warning; the run continues |
| Sentinel | soft-mandatory | Run stops; a permitted user may override |
| Sentinel | hard-mandatory | Run stops; policy checks allow no override |
| OPA | mandatory | Run stops; a permitted user may override if the set allows it |
Overriding requires the manage policy overrides permission, and for OPA the policy set must also allow overrides. Audit trails record policy check events, and reading them needs an organization or audit trail token, not a user or team token.
A real example: a Sentinel policy using the tfconfig/v2 import can fail any run whose root module calls do not come from your private registry.
Reading a risky plan
Look past the summary line to the symbols and reasons.
| Plan shows | Meaning | Risk |
|---|---|---|
-/+ | Destroy, then create | Downtime or data loss |
+/- | Create, then destroy | Unique names must not clash |
# forces replacement | That argument cannot update in place | Often unexpected |
~ | Update in place | Usually low; still check the attribute |
A change of one line can replace a database. Save the plan with terraform plan -out=tfplan, review it, then run terraform apply tfplan, so what you approved is exactly what runs.
Lifecycle and other safeguards
| Safeguard | What it does | Limit |
|---|---|---|
prevent_destroy | Rejects a plan that destroys the object | Removing the whole block still destroys it |
create_before_destroy | Builds the new object first | Both objects must be able to coexist |
precondition | Fails before creating if an assumption is false | Checks only what you wrote |
removed with destroy = false | Stops managing without destroying | The object now has no owner |
Provider protection, such as deletion_protection | The API refuses the delete | Must be set before you need it |
-target is for exceptional circumstances only: in HCP Terraform, Sentinel sees only the targeted subset and cost estimation is disabled.
Worked example: a database major version upgrade
HashiCorp treats a major RDS upgrade as a risky change. The new engine needs a new parameter group family, and that argument forces replacement of the group, which is still attached to the running instance.
resource "aws_db_parameter_group" "pg16" {
name_prefix = "app-pg16-"
family = "postgres16"
lifecycle {
create_before_destroy = true
}
}
Without create_before_destroy, Terraform would try to replace the group first and fail, because the instance is using it. With it, the order is safe.
Take the snapshot first, and check the plan before approving: the expected summary here is 1 to add, 1 to change and 1 to destroy. Using name_prefix avoids a name clash while both groups exist.
How the exam asks about this
- "Security requires a rule no workspace can bypass, while each team adds its own." Use a global set with hard-mandatory policies plus project-scoped sets.
- "A failed cost rule should stop the run, but a manager may approve exceptions." Use soft-mandatory and grant the override permission.
- "The plan shows the production database with
-/+." Stop and find the argument marked forces replacement; do not apply until you understand it.
What to remember for the exam
- A policy set uses one framework, and scope can be global, projects, workspaces or tags.
- Policies evaluate only after a successful plan and before apply.
- Advisory warns; soft-mandatory can be overridden, as can OPA mandatory when the set allows it; hard-mandatory in policy checks cannot.
-/+destroys first;+/-creates first.prevent_destroydoes not protect against deleting the resource block from configuration.- Apply a saved plan so the reviewed change is exactly what runs.
prevent_destroy guards only while the argument remains in the configuration. If someone deletes the resource block, Terraform plans a normal destroy, so pair it with provider deletion protection for critical data.
Common mistakes
Making every rule hard-mandatory. A rule with legitimate exceptions then blocks all work until someone removes it. Keep hard-mandatory for true compliance rules.
Reading only the plan summary. "1 to add, 1 to destroy" may be a harmless swap or your database. Check which resource and which attribute caused it.
Using create_before_destroy with a fixed unique name. The new object cannot be created while the old one holds the name. Use a generated name or prefix.
Check yourself
Which scope applies a rule to all current and future workspaces in a team's project?
A project-scoped policy set, which covers every workspace in the selected project.
Who can override a soft-mandatory Sentinel failure?
A user with the manage policy overrides permission, and the run otherwise stays stopped until it is overridden or discarded.
Does prevent_destroy stop a destroy if the resource block is deleted?
No. The argument must be present in the configuration, so removing the block lets Terraform destroy the object.
Topic 3 of 3 in Terraform Security and Governance
Test yourself on Terraform Security and Governance
The full HashiCorp Terraform Infrastructure Engineer Professional question bank has 600 questions, with timed practice exams and an explanation for every answer.
This study guide is independent and is not affiliated with, endorsed by, or sponsored by HashiCorp. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.