What Is Policy as Code? Check Cloud Rules and Block Violations Before Deployment
Policy as Code means expressing security and operational rules in files that people can review and software can evaluate, then applying version control and automated checks to them. For example, an organization can encode “every S3 bucket must enable encryption and block public access,” then run the same check before each infrastructure change is deployed. Writing a rule file alone does not block a deployment: an evaluator must run the rule, and a pipeline or platform must decide what to do when it fails.
Policy as Code means expressing security and operational rules in files that people can review and software can evaluate, then applying version control and automated checks to them. For example, an organization can encode “every S3 bucket must enable encryption and block public access,” then run the same check before each infrastructure change is deployed. Writing a rule file alone does not block a deployment: an evaluator must run the rule, and a pipeline or platform must decide what to do when it fails.
This article explains the concept and where to apply it using infrastructure changes as the example. AWS CloudFormation Guard, the general-purpose Open Policy Agent (OPA), and the policy frameworks in HCP Terraform are different tools and are not feature-equivalent. Product comparisons reflect official documentation reviewed on November 20, 2026.
What changes when policies are code?
A policy defines a required or prohibited state or action. If a document says “storage must have encryption enabled,” someone must interpret and check it each time. Expressing the same requirement in machine-evaluable policy code lets teams connect each change to code review, version history, automated tests, and continuous integration (CI).
The workflow has three roles:
**The policy author** turns an organizational rule into explicit conditions. For example, every bucket must have an encryption property and all public-access-block properties must be `true`.
**The policy evaluator** compares the rules with input such as an infrastructure template, deployment plan, or API request. The result may be pass, violation, not applicable, or a decision with an explanation.
**The enforcement point** decides whether to report a warning, require a human exception approval, or stop deployment. Without this connection, an evaluator can find a violation while the change still proceeds.
Policy as Code is therefore an operating process for creating, evaluating, and enforcing rules, not just a rule file. It improves traceability and testability, but the organization must first define what each rule means and how exceptions work.
Example: checking an S3 bucket template
Suppose a platform team requires every new S3 bucket to specify encryption and enable all four public access block settings. CloudFormation Guard lets teams write rules in the Guard language and check JSON or YAML input. AWS’s example checks that an S3 bucket has BucketEncryption and that all four properties in PublicAccessBlockConfiguration are true.
When a developer submits a template, a CI job passes the template and rules to Guard. If the check passes, the pipeline can continue. If a rule fails, Guard can report the failed rule and resource property path and return a failure code, which the build system can use to stop the run. The developer can then identify the bucket and property to fix and submit the change again.
This is template validation. It is not the same as continuously monitoring live resources in an AWS account or intercepting every change sent to a deployment API. AWS documents Guard checks for templates, change sets, and AWS Config configuration items; to enforce rules during CloudFormation operations, AWS documents the separate Guard Hooks feature. Keep the checked input and the enforcement point distinct.
Where should policy checks run?
The evaluation point affects how early a problem is found and how easy it is to bypass the check.
| Evaluation point | Input and purpose | Advantage | Limitation |
|---|---|---|---|
| Editor or local check | Check a template or policy while it is being written | Fast feedback | A developer may skip the check, or local tool versions may differ |
| Pre-commit hook | Check changed files before recording a commit | Catches simple issues early | A hook can be bypassed, so it does not replace central CI |
| CI build | Consistently check changes submitted to version control | Shared rules and recorded results; a failed check can block deployment | The pipeline must treat failure as a real blocking condition |
| Deployment platform policy or hook | Evaluate during planning, applying, or a supported resource operation | Can enforce at a platform-controlled stage | Available input and timing vary by provider and platform |
Teams can combine these points. Local feedback helps developers fix problems quickly, while managed CI or the deployment platform provides the authoritative enforcement. Decide separately whether a violation is a warning or a blocker and how approved exceptions are recorded.
Which tool fits?
Choose according to the input to evaluate and where the decision must be enforced.
**For JSON or YAML rules on CloudFormation templates**, CloudFormation Guard is one fit. It can also evaluate change sets and AWS Config configuration items. A template check alone does not control every change at deployment time.
**For reusing policy decisions across different systems**, consider OPA. It evaluates structured input such as JSON against policies and data and expresses rules in Rego. OPA can separate decision-making from enforcement: an application or service asks OPA for a decision and then applies it. This can connect policy to Kubernetes, APIs, CI/CD, and other systems, but teams must design input data, integrations, policy distribution, and operations.
**For checking Terraform plans within HCP Terraform workflows**, compare the platform’s policy frameworks. HashiCorp documents Terraform policy, Sentinel, and OPA, with differences in language, testing, evaluation stages, and enforcement levels. Terraform policy uses HCL and has deeper platform integration; OPA is a general-purpose Rego engine but does not have the same Terraform-specific integration. Verify the current product and version before selecting a capability.
There is no single best tool for every environment. First decide whether you are checking one template format, need different services to call the same decision, or need access to data at a particular deployment stage. Then consider the organization’s CI, developer skills, policy testing, and operations.
A safe adoption sequence
**Start with one verifiable rule.** For example, “every S3 bucket specifies encryption” is directly checkable in configuration data. Vague requirements such as “configure storage securely” need clearer criteria before they can become code.
**Document the requirement and exceptions.** Record why the rule exists, what it covers, who can approve exceptions, and whether a violation warns or blocks. Where supported, pass sensitive or environment-specific values as parameters rather than hard-coding them in a rule.
**Write pass, fail, and not-applicable cases.** Include compliant configuration, a clear violation, and input with no relevant resource. AWS Guard supports expected `PASS`, `FAIL`, and `SKIP` results in rule test data. Check boundary cases and invalid input so the rule does not pass accidentally.
**Initially review results in observation mode.** Evaluate existing configurations and sort unexpected findings and false positives. Separate legitimate exceptions from policy defects, and record an owner and remediation date.
**Make CI handle the result explicitly.** Preserve the report and configure defined violations to fail the build and block deployment. A pipeline that runs a check but ignores its result is not enforcement.
**Review and manage policy changes too.** Apply code review, tests, version control, and gradual rollout to policy edits. Because a rule can block many deployments, define rollback, exception expiry, and a policy owner.
To verify the workflow, confirm that designed pass and fail cases produce the expected results, CI actually stops on failing input, and policy versions and exception records can be traced. This article describes a process; no commands or configurations were run or tested in a particular environment.
Limitations and operational cautions
**A policy cannot infer business meaning by itself.** Information absent from the input, operational context, and approved exceptions must be supplied separately or reviewed by a person.
**A mismatch between the checked input and enforcement point leaves gaps.** Passing a template check does not prove that live state remains compliant. Continuous state monitoring and deployment-time blocking may be separate controls.
**False positives can stop development, while weak rules can miss risk.** Test edge cases, include useful remediation messages, and review results before applying a policy broadly.
**Warnings and blocking are different enforcement levels.** A team can begin with warnings to understand impact and move to blocking when evidence and recovery procedures are ready. Without documented exceptions and audit trails, people may create informal ways around a blocker.
**The same policy language may run in different environments.** Input schemas, policy versions, data distribution, and evaluation timing can change results. Manage test inputs and runtime context alongside the policy.
Summary
Policy as Code makes organizational rules testable, versioned, and repeatable during infrastructure changes. A rule file alone does not enforce anything. Teams must decide what to check, what data to evaluate, whether failure warns or blocks, and where evaluation runs. Start with one clear rule and tested pass/fail cases, then assign distinct roles to local checks, CI, and the deployment platform.
Sources
[Open Policy Agent: overview and separating policy decisions](https://www.openpolicyagent.org/docs)
[Open Policy Agent: Rego policy language](https://www.openpolicyagent.org/docs/policy-language)
[Open Policy Agent: integration methods](https://www.openpolicyagent.org/docs/integration)
[Validate templates with AWS CloudFormation Guard](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cloudformation-guard.html)
[HCP Terraform: policy framework overview](https://developer.hashicorp.com/terraform/cloud-docs/policy-enforcement/define-policies)
[Compare Terraform policy, Sentinel, and OPA](https://developer.hashicorp.com/terraform/policy/compare)