Gaining visibility into Terraform costs with tools like Infracost is a critical first step in managing cloud spend. However, visibility alone is passive; it relies on a human reviewer to notice a cost spike in a pull request and manually block it. To truly scale FinOps and create robust financial guardrails, organizations must move from cost visibility to cost governance. This is achieved through policy as code (PaC), a practice that uses automated, code-based rules to enforce cost-related policies before infrastructure is ever deployed.
What is Policy as Code (PaC) for FinOps?
Policy as Code is the practice of defining, managing, and enforcing rules as executable code. In the context of FinOps, these policies codify an organization's financial and operational best practices. Instead of relying on manual reviews or documentation, a policy engine automatically evaluates infrastructure changes against these rules.
Common examples of cost-related policies for Terraform include:
Budgetary Controls: "Deny any deployment that increases the monthly cost by more than $500."
Resource Restrictions: "Only allow the use of specific, cost-effective instance types (e.g., t3.micro, t3.small) in development environments."
Tagging Mandates: "Require all provisionable resources to have a cost-center and owner tag for accurate cost allocation."
Regional Constraints: "Only allow resources to be deployed in approved, low-cost cloud regions to comply with data sovereignty and budget goals."
By integrating these checks into a CI/CD pipeline, non-compliant deployments can be automatically blocked, preventing policy violations from reaching production and causing budget overruns.
Key Tools for Terraform Policy Enforcement
The Terraform ecosystem is dominated by two primary policy as code frameworks: HashiCorp Sentinel and Open Policy Agent (OPA). Both are powerful, but they differ in their language, scope, and integration model.
HashiCorp Sentinel
Sentinel is a proprietary policy as code framework developed by HashiCorp, designed for deep integration with its Enterprise products, including Terraform Cloud/Enterprise.
How it Works: Sentinel policies are written in their own purpose-built language. Within Terraform Cloud, Sentinel runs between the plan and apply stages. It has access to rich contextual data, including the Terraform plan, configuration, state, and cost estimation data. If a policy fails, the run is halted before any infrastructure is changed.
Enforcement Modes: Sentinel offers flexible enforcement levels: advisory (warns but doesn't block), soft-mandatory (blocks but can be overridden by an administrator), and hard-mandatory (blocks with no overrides).
Strengths: Its tight integration with the HashiCorp ecosystem provides access to detailed Terraform-specific data, making it powerful for writing granular policies. It is the native choice for teams heavily invested in Terraform Cloud.
Weaknesses: It is a proprietary language and framework, limited primarily to HashiCorp products. Testing policies locally can be more complex than with other tools.
Open Policy Agent (OPA)
OPA is an open-source, general-purpose policy engine that has become a de facto standard across the cloud-native ecosystem, governed by the Cloud Native Computing Foundation (CNCF).
How it Works: OPA policies are written in a declarative language called Rego. OPA is decoupled from any specific tool; it makes decisions by evaluating arbitrary JSON input against policies. For Terraform, this means converting a Terraform plan to JSON and feeding it to OPA. Many CI/CD tools and IaC platforms, like Scalr and Spacelift, have native OPA integration.
Integration with Cost Data: To enforce cost policies, a cost estimation tool's output must be provided to OPA. For example, Infracost can generate a JSON output of its cost estimate, which is then passed to OPA along with the plan JSON. The Rego policy can then make decisions based on fields like
delta_monthly_cost.Strengths: OPA is a universal, open-source standard. A single policy language (Rego) can be used to govern Terraform, Kubernetes, API gateways, and more, providing a unified policy framework across your entire stack.
Weaknesses: It requires an extra step to convert the Terraform plan to JSON and to integrate cost data from a separate tool like Infracost. The Rego language can have a steeper learning curve than Sentinel for simple policies.
Other Static Analysis Tools
Tools like Checkov, tfsec, and TFLint are static analysis linters that scan Terraform HCL code for misconfigurations. While their primary focus is security and best practices, they can be used to enforce certain cost-related rules indirectly. For example, a Checkov policy can flag the use of un-versioned S3 buckets, which can prevent storage cost issues. However, they cannot evaluate the dynamic cost of a plan, making them less suited for direct budget enforcement.
Strategies for Effective Implementation
Start with Tagging: The foundation of any cost governance strategy is good data. Begin by implementing a PaC policy that requires all resources to be tagged with essential information like owner, project, and cost-center. This ensures you can attribute every dollar of spend.
Use Advisory Mode First: When introducing new policies, especially restrictive ones, deploy them in advisory mode first. This allows you to see the impact of the policy and gather feedback from developers without blocking their workflows. Once the policy is well-understood and refined, you can escalate it to a mandatory enforcement level.
Integrate into CI/CD: Policy enforcement should be an automated, non-negotiable step in your deployment pipeline. This ensures that every single change is validated consistently, removing the potential for human error and creating a reliable governance gate.
Combine Cost Estimation with Policy: A policy engine alone doesn't know the cost of a resource. The most powerful cost governance comes from combining a cost estimation tool (like Infracost) with a policy engine (like OPA or Sentinel). The estimator provides the data, and the engine provides the logic to act on that data.
Conclusion
Terraform cost policy enforcement is the evolution of FinOps, moving from passive observation to active, automated governance. By leveraging policy as code frameworks like Sentinel and OPA, organizations can codify their financial best practices and build a safety net that prevents budget overruns before they happen. This programmatic approach not only reduces financial risk but also empowers developers to innovate with speed and confidence, knowing that clear, automated guardrails are in place to guide them.
All in One Place
Atler Pilot decodes your cloud spend story by bringing monitoring, automation, and intelligent insights together for faster and better cloud operations.

