Implement policy as code for cloud cost guardrails by writing your cost rules, such as required tags, allowed instance sizes, and permitted regions, as machine-evaluated code that runs against the infrastructure plan in CI. Use an engine such as Open Policy Agent with Conftest or HashiCorp Sentinel, set each rule to warn or deny, and fail the build on violations so waste is blocked before it is provisioned. Policy as code gives engineers fast, specific feedback at the point of change; pair it with an API-layer guardrail such as an SCP as the backstop that cannot be bypassed.
Last updated: June 2026
Policy as code is the practice of expressing rules as version-controlled code that a policy engine evaluates automatically, rather than as documents people are meant to remember. For cloud cost guardrails, that means rules like "every resource carries owner, environment, and cost-center tags", "no GPU instances in sandbox", and "only approved regions" are checked against each infrastructure change in the pipeline. This is the Lock step of FinOps made automatic: guardrails so spend does not drift back, enforced at the moment a change is proposed.
This article is part of our governance, tagging and allocation cluster. For the full picture start with the complete guide to cloud cost governance, the pillar this piece links up to. Guardrails that block spend before it happens pair naturally with reactive controls, covered in how to set up cloud budgets that trigger automated actions.
What is policy as code for cloud cost?
Policy as code for cloud cost is the practice of writing cost-control rules as code that a policy engine evaluates against infrastructure changes in the deployment pipeline. Instead of catching an oversized database in a manual review or on next month's invoice, the pipeline checks the Terraform or CloudFormation plan against the policies and fails the build when a rule is broken. The result is prevention rather than cleanup: the waste never gets provisioned, so there is nothing to chase down later.
What is the difference between policy as code and an SCP?
Policy as code runs in CI against a plan before deployment, while a Service Control Policy runs in AWS at the API layer and blocks the action itself. The two are complementary layers. Policy as code gives the engineer fast, readable feedback at the point of change and works across any cloud or tool; the SCP is the non-bypassable backstop that holds even if someone clicks in the console. A mature setup uses both, and the SCP-based approach is covered in how to enforce tagging with AWS Service Control Policies.
How do I implement policy as code for cost guardrails, step by step?
Follow these six steps to move from written cost rules to guardrails that fail the build automatically.
- List the cost rules to enforce. Write down the concrete, checkable rules that prevent waste: required tags, blocked instance families or sizes, allowed regions, storage lifecycle requirements, and any mandatory commitment coverage. Vague intentions cannot be codified; specific rules can.
- Choose a policy engine. Pick one that fits your stack, such as Open Policy Agent with Conftest for Terraform and Kubernetes manifests, HashiCorp Sentinel for Terraform Cloud, or Checkov for infrastructure-as-code scanning. The pattern is the same across all of them.
- Codify each rule as a testable policy. Express every rule as code with an unambiguous pass and fail, and write unit tests for the policy itself so a change to a rule cannot silently stop enforcing it.
- Run policies against the plan in CI. Evaluate the policies against the generated Terraform or CloudFormation plan inside the pipeline, so a violation fails the build before any resource is created.
- Set warn versus deny per rule. Mark high-confidence rules as blocking and softer rules as advisory, so the pipeline stops genuine waste while only flagging borderline cases. Promote rules from warn to deny as teams adapt.
- Report and iterate on exemptions. Collect policy results centrally, grant time-boxed exemptions that carry a named owner and an expiry, and add or tighten rules as new patterns of waste appear in the bill.
Waste that keeps getting provisioned despite the rules?
Our cost audit turns your cost rules into tested policy-as-code guardrails, wires them into CI, and sets the warn-versus-deny thresholds so waste is blocked at the source. On the performance model, you pay only from realized savings. No savings, no fee.
Book a cloud cost audit →Which cost rules give the most return when codified?
The highest-return rules are the ones that prevent recurring, structural waste rather than one-off mistakes. Mandatory tagging keeps allocation complete; blocking oversized or wrong-family instances stops the most common form of overprovisioning; restricting regions avoids accidental spend in expensive or non-compliant locations; requiring storage lifecycle policies stops data piling up at hot-tier prices. Each of these is checkable against a plan and each maps to a line you can see growing on the bill. Start with tagging and sizing, since they cover the largest share of avoidable spend, and the standard policy engines are documented at the Open Policy Agent documentation.
How do I roll out guardrails without blocking every team on day one?
Roll out in warn mode first, measure, then promote rules to deny once teams have adapted. Starting every rule as blocking produces a wall of failed builds and erodes trust in the system, so begin by surfacing violations as warnings and reporting them, which shows teams what would have failed. Fix the noisy false positives, give engineers time to update their modules, and move each rule to deny only when its violation rate is low and well understood. This mirrors the staged enforcement discipline we use everywhere in governance, where guardrails are proven safe before they become blocking.
The Cloud Cost Governance and Tagging Toolkit includes a starter policy library for tagging, sizing, and region guardrails plus the CI wiring pattern. It is the downloadable companion to this article.
Frequently asked questions
What is policy as code for cloud cost?
Policy as code for cloud cost is the practice of expressing cost-control rules, such as required tags, allowed instance sizes, and permitted regions, as machine-evaluated code that runs in the deployment pipeline. Instead of relying on review or after-the-fact cleanup, the pipeline checks each infrastructure change against the policies and fails the build when a rule is violated, so waste is prevented before it is provisioned.
What is the difference between policy as code and an SCP?
Policy as code runs in the CI pipeline against an infrastructure plan before deployment, giving fast feedback to the engineer and covering any cloud or tool. A Service Control Policy runs in AWS at the API layer and blocks the action regardless of how it is invoked. They are layers of the same defense: policy as code catches issues early and explains them, while the SCP is the backstop that cannot be bypassed.
Which tools are used for policy as code?
Common engines are Open Policy Agent with Conftest for evaluating Terraform and Kubernetes manifests, HashiCorp Sentinel for Terraform Cloud, and Checkov or cloud-native policy services for infrastructure-as-code scanning. The choice depends on your stack, but the pattern is the same: rules as code, evaluated against the plan in CI.
Should cost guardrails warn or block?
Use both. High-confidence rules, such as missing required tags or an oversized GPU instance in a sandbox, should block the build. Softer rules, such as a slightly larger size than the recommendation, should warn so engineers see the signal without being stopped. Starting most rules as warn and promoting them to deny once teams adapt avoids a wall of failed builds on day one.
The short version
Implement policy as code for cloud cost guardrails by listing your cost rules, codifying each as a tested policy in an engine like OPA or Sentinel, running them against the plan in CI, and setting warn versus deny per rule. It prevents waste at the point of change instead of cleaning it up later. When you want the policy library written, wired into CI, and tuned for your environments, that is exactly what our FinOps implementation service delivers.
Written by Fredrik Filipsson and reviewed by Morten Andersen, applying the See, Cut, Lock, Run method. Independent and vendor neutral.
Cloud pricing and service behavior change frequently. Verify the specifics in this guide against the providers’ own current documentation and the FinOps Foundation: FinOps Foundation Framework ↗ and FinOps Governance, Policy & Risk capability ↗. This article also reflects Cloud Cost Room’s hands-on, vendor-neutral engagement experience.