OPA vs Custom Policy Engines: How to Choose for Terraform Governance
Compare OPA for Terraform with Sentinel, tfpolicy and Checkov custom policies, and decide what to police before you choose an engine.
Compare OPA for Terraform with Sentinel, tfpolicy and Checkov custom policies, and decide what to police before you choose an engine.


Writing Terraform is no longer the slow step. AI assistants now draft Terraform modules quickly, and platforms such as SGCode generate Terraform or OpenTofu for existing infrastructure. Approval is the slow step: deciding whether a proposed change is safe to apply. A change that reaches production without a check is the failure that governance exists to prevent, and faster code generation produces more changes to check.
This guide starts from the approval problem and then treats the engine as a second decision. It explains what a policy gate evaluates, lists what a Terraform team should police for security, compliance and governance, and compares OPA with custom policy engines. It then shows how Tirith acts as the governance layer in StackGuardian and how MCP handles configuration, and it ends with rollout advice and answers to common questions. The wider practice is covered in the companion guide on policy as code.
A Terraform policy gate reads the plan for a change and returns a verdict such as pass, warn or fail. The plan describes what Terraform intends to create, update or delete. Terraform prints it as JSON through terraform show -json, and that JSON format lists every resource in a resource_changes array. Each entry carries the resource address, its type and a change object with the planned actions.

The actions array drives deletion rules. Its values include create, update and delete, and a replacement appears as delete and create together in either order. A rule that blocks deletion therefore scans each entry for the word delete, which catches replacements too. The same data supports attribute rules about encryption settings or tags, because the planned values sit in the change object.
Every engine in this guide reads that same plan data. The verdict it returns decides whether a change moves toward approval, so the rules behind the verdict define what an organization policies. The next section covers why approval has become the hard part.
Approval is the step where a proposed change becomes an applied change, and it is where Terraform governance succeeds or fails. A reviewer who reads plans by eye can check a handful of changes carefully. A plan that looks routine can still contain a deleted security group or an unencrypted bucket, and a review queue that grows with every generated pull request leaves less time for each plan.
Generated code widens the gap. Code from an AI assistant, an SGCode pull request or an MCP client arrives faster than a person can read it, and it needs the same checks as code written by hand. A policy gate applies those checks the same way to every source. In StackGuardian, the attached policies apply to the resulting runs the same way they apply to any other change. The gate is only as useful as the list of things it polices, which the next section sets out.

A Terraform policy gate should police three areas: security, compliance and governance. The table gives one example rule for each area and the plan data that rule reads. All of these rules can be checked from plan JSON, so they apply to every plan the same way.
Coverage is the second question. A rule only polices what it is attached to, so a workflow with no policy attached is approved without any check. Listing which workflows have no policy is therefore as useful as writing the rules, and the product section shows how MCP answers that question.
Open Policy Agent, known as OPA, is a general purpose policy engine that the CNCF graduated in January 2021. Its rules are written in a language called Rego, and the engine evaluates any JSON document, so one skill and one toolchain can serve Terraform plans and other systems.
The OPA documentation for Terraform describes a workflow with three steps: save the plan with terraform plan --out tfplan.binary, convert it with terraform show -json tfplan.binary > tfplan.json, and evaluate Rego rules against the resource_changes array with opa exec. The same page warns that plan JSON may lack computed attributes that stay unknown until apply, along with dynamic blocks and the results of function calls. A rule that reads such a value cannot judge it from the plan alone, so those cases need a different check.
The running example is a platform team that wants to stop any plan that deletes or replaces an aws_security_group, the first security rule in the table. The Rego rule below collects the addresses of the affected resources into a set and denies the plan when that set is not empty. It starts from the rego.v1 import, which opts the file into the current Rego syntax.
package terraform.governance
import rego.v1
deleted_security_groups contains rc.address if {
some rc in input.resource_changes
rc.type == "aws_security_group"
"delete" in rc.change.actions
}
deny if count(deleted_security_groups) > 0This rule checks the aws_security_group resource type and the planned actions only. A plan that deletes or replaces a security group makes deny true, and a plan with no security group leaves deny undefined. A caller that needs an explicit false adds a default rule. The rule does not tell production from other environments; that needs a tag or workspace condition added to it.
Rego has a learning curve, and OPA documents ways to keep policies fast as the rule set grows. The policy performance guide covers the linear fragment, rule indexing and profiling with opa eval --profile. Teams that prefer a command line wrapper can use Conftest, a tool that tests configuration files against Rego policies and extends the same skills to pipeline checks.
A custom policy engine is any tool whose policy language or input model was built for Terraform or for a small set of infrastructure formats. This section covers four options, starting with the two from HashiCorp. Sentinel is HashiCorp's policy framework. Its documentation calls it a language and framework for policy built to be embedded in existing software, with documented integrations for Terraform, Vault, Consul and Nomad. In HCP Terraform, policy enforcement supports Terraform policy in HCL, Sentinel and OPA. The page states that Sentinel and OPA policy sets only support workspaces, and that HCP Terraform checks the plan against the policy set during each run.
HashiCorp also introduced tfpolicy, a declarative policy workflow written in HCL. The July 16, 2026 announcement says it is now generally available in HCP Terraform and describes a conversion agent for existing Sentinel and OPA libraries. Teams on HCP Terraform therefore choose among three policy languages, and OPA is one of them.
Checkov supports custom policies in two forms. Python policies check an attribute's status, and YAML policies can also check connections between resource types. The companion piece on where Checkov and Tirith each fit compares Checkov with StackGuardian's Tirith, which a later section covers as a governance layer.
The last option is a homegrown script that parses plan JSON with jq or Python and fails a pipeline step. Scripts like this are quick to start. Testing, versioning and reuse then fall to the team that wrote them, which is a common reason to move the same rules into one of the engines above.
Five criteria separate OPA from custom policy engines for Terraform governance. The table compares them, with the custom column describing the category as a whole. Individual tools may differ from the general pattern, so check a candidate against each row before adopting it. Read each row as a question to ask about the rules a team plans to write over the next year.
No single criterion in the table settles the choice, and none of them says what to police. A team that already uses OPA for another system gains the most from reuse, while a team that governs only Terraform may value a shorter path to a first rule. The decision guide that follows applies the criteria after the policing question is answered.
A platform team that manages Terraform for several product teams on AWS shows the decision order. It first lists what to police: security group deletions, bucket encryption and owner tags. Only then does it pick how to express those rules, because each rule can be written in any engine covered above. Three cases cover the outcomes.
Choose OPA when the team already runs OPA for another system or expects to extend the same rules to Kubernetes or API authorization. Rego then carries all three rules in one language, and the engineers who write them build a skill that applies to other systems. The cost is the learning curve, so plan time for authors to learn Rego and to write tests.
Choose a custom engine when the team governs only Terraform, wants authors who do not know Rego to write rules, or already runs its workspaces in HCP Terraform where Sentinel and tfpolicy are built in. The first rule ships sooner. The limits are narrower input types and less reuse outside the tool.
Use one governance layer when shared rules and quick plan checks have different owners. The security team may keep a Rego library that spans several systems, while the platform team writes small JSON policies for plan checks. Both need one place that evaluates them together and returns a single verdict, and the next section shows how StackGuardian does it with Tirith.

Tirith is the governance layer in StackGuardian, and it decides whether a Terraform plan passes policy. The policy types documentation describes Tirith as a JSON based framework for infrastructure as code and workflow compliance that scans Terraform and CloudFormation configurations. The same page describes OPA as having first class support, with Rego sourced from a GitHub or git based repository. Teams keep existing Rego and add Tirith policies beside it. The deciding query for an OPA rule is optional and must return a boolean, so a rule like the earlier example can add default deny := false to return false when no security group is in the plan.
Tirith policies are declarative JSON files, so the deployment gate is a deterministic set of declared rules instead of a model's opinion. The Tirith README describes it as an IaC governance plugin that reads the plan a pipeline already produces, checks it against policies and exits non-zero so a violating change never reaches apply. The same policy files can gate a GitHub Actions job, a GitLab job and a laptop, and evaluation runs on your own runner. With the --fail-on-error flag a failed policy exits with code 3, and exit code 1 reports that Tirith could not complete the evaluation.
The Tirith version of the security group rule is one evaluator. It names the terraform_plan provider, selects the aws_security_group resource type, reads the planned action and applies a NotEquals condition against the value delete. The eval_expression points at the evaluator by its id.
{
"meta": {"required_provider": "stackguardian/terraform_plan", "version": "v1"},
"evaluators": [
{
"id": "protect-security-group",
"description": "Fail any plan that deletes an aws_security_group",
"provider_args": {
"operation_type": "action",
"terraform_resource_type": "aws_security_group",
"terraform_resource_attribute": ""
},
"condition": {"type": "NotEquals", "value": "delete", "error_tolerance": 1}
}
],
"eval_expression": "protect-security-group"
}Run against a plan that deletes a security group, Tirith reports a failed final evaluation, and a plan that replaces one fails the same way. A plan with no security group is skipped. As with the Rego rule, this policy checks the resource type and the planned action only and does not separate production from other environments. For policy kept in one place across many repositories, the README describes tirith platform check, which evaluates against the policies a StackGuardian organization enforces. It masks the document on your machine before anything leaves it, uploads it with the Terraform source, runs the policies on StackGuardian and prints the verdict.
Inside StackGuardian, the policy overview states that each time a Workflow or Stack runs, StackGuardian evaluates every Policy Set in scope and applies the action for each rule. A rule can use a template from the SGMarketplace, a Tirith policy or an OPA policy. Scope can be Workflows, Workflow Groups, Stacks, Connectors, Connector Groups or the whole organization, and a fail always wins the overall result. A rule can also use the APPROVAL_REQUIRED verdict for changes that need a person's sign-off.

The MCP policy tools include create_policy and update_policy. A policy takes one to 25 rules, and each rule names a source and a verdict. The source can be an inline Tirith document, a git repository or a policy template. The verdict can be FAIL, WARN, APPROVAL_REQUIRED or PASS. Scope must be set explicitly, and the tool validates Tirith documents, OPA rules, git and secret references and template versions. For the platform team, a prompt to a connected MCP client can start the work:
Create a policy named protect-security-groups with one rule. The rule uses the OPA policy in our platform-policies git repository and applies the FAIL verdict. Scope it to the production workflow group.
The documentation says the assistant should confirm scope and verdicts with the user before it creates the policy. Later changes go through update_policy, which reads the stored policy first and can add, remove or replace scope entries. An empty scope list detaches the policy from everything, and changes apply only to runs created afterward. The search_policies tool finds policies, which supports the coverage check described earlier. The MCP tools reference also lists create_workflow_run for starting runs, so an agent that deploys through MCP triggers the same Policy Set evaluation as any other client. The documentation notes that write tools are enabled per environment, so confirm that setting before relying on these prompts.
Rolling out policy checks starts with tests that run before any rule can block a change. Rego tests are rules whose names start with test_, and opa test . -v runs them. The policy testing guide also covers coverage reports with --coverage and mocking with the with keyword. For the security group rule, one test passes a plan fixture that contains a delete action and asserts that deny is true. A second fixture with a replacement should give the same result. Tirith policies can be checked the same way from the command line, since the CLI takes a policy path and an input path. The README also lists tirith lint, which checks policy files for mistakes that would gate nothing or fail as a false violation.
New rules can start with the WARN verdict so the team sees results before anything blocks. After a review of the results across runs, the rule can move to FAIL. Storing Rego files and Tirith JSON in git gives rule changes the same review as Terraform changes. Scope changes go through update_policy, and because they apply only to runs created afterward, a change never alters a run that already started.

Teams that leave HCP Terraform face a rewrite for Sentinel policies, because they do not port to another platform. The Terraform Cloud migration guide covers that move. Existing Rego can often be reused if the new platform sources Rego from Git, which is one more reason to keep the shared rules in OPA.
Approval is the hard part of Terraform governance now that generated code arrives quickly, so the first decision is what gets policed and the second is which engine expresses the rules. OPA suits teams that want one policy language across systems, custom engines suit teams that govern only Terraform, and a governance layer such as Tirith evaluates both kinds of rules together. The list of security, compliance and governance rules decides more than the engine behind it.
To start, ask an MCP client the question from the MCP server documentation: "Which workflows in the production workflow group don't have any policy attached in StackGuardian?" Then add one rule, such as the security group example to the workflows it finds. The StackGuardian Orchestrator page describes the Workflows and Policy Sets that those rules attach to.
Start with changes that delete or weaken security controls, such as security group deletions and unencrypted buckets. Then add tag rules for compliance and sign-off rules for high-impact changes. Before writing rules, list workflows with no policy attached, because those workflows are approved without any checks. The policing table in this guide gives one example rule for each area.
No. A policy evaluates the plan, so the same rules apply whether an engineer, an AI assistant or an MCP client produced the code. In StackGuardian, the attached policies apply to the resulting runs the same way they apply to any other change. What changes is the volume of plans, which makes automated checks more important than manual review.
Tirith is the governance layer in StackGuardian, and it does not replace existing Rego. Policies are declarative JSON files, and OPA policies run alongside them in the same Policy Sets. A team can start with Tirith rules for plan checks and keep its Rego library for shared rules. Teams that already use Rego keep that investment.
Policy as code is the practice of writing governance rules as versioned, testable files. A policy engine is the software that evaluates those files against an input and returns a verdict. OPA, Sentinel and Tirith are engines, and the practice works with any of them. Choosing an engine is one decision inside that wider practice, which also covers where rules live and how changes are reviewed.
Yes, in environments where write tools are enabled. The create_policy tool takes one to 25 rules, validates the sources and requires an explicit scope, and the assistant should confirm scope and verdicts before it creates the policy. The update_policy tool handles later changes.