CLOUD CONCEPT

Policy as Code

Policy as Code expresses governance and security requirements as version-controlled, machine-evaluable rules that automated workflows can test and enforce.

Cloud Concepts Hub →

What is Policy as Code

Policy as Code turns written requirements into executable checks against infrastructure definitions, deployment plans, requests, or running resources. It helps teams apply consistent controls across accounts and environments, detect violations early, and retain reviewable evidence of policy changes and evaluation results.

Policy as Code treats governance requirements as software artifacts rather than relying only on documents and manual reviews. Engineers express requirements in a rule language or general-purpose code, store them in version control, test their behavior, and run them through automated evaluation. Examples include requiring encryption for storage, prohibiting unrestricted administrative network access, and requiring ownership tags. Policies should produce actionable results that identify the resource, violated requirement, and expected correction.

Infrastructure as code and Policy as Code serve different purposes. Infrastructure as code describes resources to create or modify; Policy as Code evaluates whether those definitions or their resulting resources satisfy requirements. A reusable infrastructure template can contain secure defaults, but those defaults are not an enforcement boundary if callers can override them or deploy through another path. A practical design combines approved templates with independent policy checks and appropriately restricted deployment permissions.

In a deployment pipeline, validate the infrastructure definition before provisioning. For AWS CloudFormation, cfn-lint checks templates against resource specifications and other validation rules, helping detect invalid properties and configuration mistakes. AWS CloudFormation Guard evaluates structured data against explicitly authored policy rules, such as requiring an encryption property or restricting allowed resource configurations. Passing cfn-lint does not establish compliance with an organization's security requirements, and Guard does not enforce rules merely because a rule file exists. The pipeline must execute the checks and stop deployment when required checks fail.

For example, a pull request that introduces an unencrypted database should fail a policy test before deployment. The same repository can include passing and failing fixtures to verify that the rule detects missing encryption settings without rejecting approved configurations. Third-party infrastructure tools can use comparable workflows, such as evaluating Terraform plan JSON with Open Policy Agent. Evaluate the artifact that will actually be deployed, and account for unresolved values: static checks cannot always determine properties that become known only during deployment.

Across accounts and Regions, CloudFormation StackSets can distribute approved baseline templates and updates consistently. StackSets provide deployment orchestration, not a general-purpose security policy evaluator. Validate templates before distributing them, control deployment roles, and use staged rollouts to limit the impact of mistakes. Where appropriate, CloudFormation Hooks can add provisioning-time checks, while organization-level permission controls can restrict prohibited actions through other deployment paths.

Predeployment checks do not replace assessment of running resources. Console changes, alternative automation, and changing dependencies can introduce noncompliance after a successful deployment. Runtime evaluations, such as AWS Config rules, complement pipeline checks; detection and remediation are separate capabilities and must be configured deliberately. Drift detection identifies differences from declared infrastructure but does not independently prove security compliance.

The main trade-offs are policy maintenance, incomplete evaluation context, false positives, and the disruption caused by incorrect blocking rules. Begin new rules in reporting mode when appropriate, test them against representative workloads, and promote them to enforcement after review. Exceptions should have an owner, justification, scope, and expiration. Protect policy repositories and pipeline configuration as carefully as deployment credentials, because an actor who can weaken checks or bypass the pipeline can undermine the control.

Key aspects of Policy as Code

Policy as Code combines executable rules, software delivery practices, explicit enforcement, and ongoing evaluation to apply governance consistently.

Executable requirements

Translate requirements into deterministic rules with clear inputs and actionable outcomes. Define how missing properties, unknown values, and evaluation errors affect the result.

Versioning and testing

Review policy changes through version control and test them with compliant and noncompliant examples. Record policy versions alongside evaluation results so teams can explain why a deployment passed or failed.

Explicit enforcement points

Run checks in pull requests, deployment pipelines, provisioning controls, or runtime evaluations as appropriate. A policy becomes preventive only when a violation blocks the relevant operation and bypass paths are controlled.

Consistent multi-account deployment

Apply shared policy rules to approved infrastructure templates before distributing them across accounts and Regions. Separate deployment orchestration from policy evaluation, and restrict who can change either.

Layered validation

Combine template linting, security policy evaluation, permission boundaries, and runtime compliance checks. Each addresses different failure modes; syntactically valid infrastructure is not necessarily secure infrastructure.

Managed exceptions and rollout

Use scoped, time-limited exceptions with accountable owners rather than disabling checks broadly. Stage policy changes and monitor their impact before enforcing them across all workloads.

Cloud services for Policy as Code

CodePipeline can orchestrate a workflow in which CodeBuild lints infrastructure definitions, tests policy rules, and evaluates the proposed deployment. Only artifacts that pass required checks proceed to CloudFormation, with StackSets distributing approved baselines across accounts and Regions. AWS Organizations adds permission guardrails that can restrict prohibited actions outside the pipeline. AWS Config evaluates supported running-resource configurations to identify noncompliance introduced after deployment. Together, these services separate validation, deployment, permission control, and runtime assessment rather than treating any single check as complete enforcement.

AWS CloudFormation image AWS CloudFormation

CloudFormation provides declarative infrastructure deployment, while StackSets distribute baseline templates across accounts and Regions. CloudFormation Guard is a separate policy-evaluation tool, and cfn-lint is a template-validation tool that can run before deployment; CloudFormation Hooks can provide provisioning-time validation.

Open AWS CloudFormation →
AWS Config image AWS Config

AWS Config evaluates recorded resource configurations against rules, including custom policy rules authored with Guard. Conformance packs package rules and remediation actions for repeatable deployment, complementing predeployment checks with evaluation of running resources.

Open AWS Config →
AWS Organizations image AWS Organizations

AWS Organizations supports centrally managed service control policies that limit available permissions in member accounts. Managing these policies as reviewed, versioned artifacts applies Policy as Code to organization-level permission guardrails; service control policies do not grant permissions or validate every resource property.

Open AWS Organizations →
AWS CodeBuild image AWS CodeBuild

CodeBuild can execute cfn-lint, CloudFormation Guard, policy unit tests, and third-party policy engines as automated build steps. Configure failed policy evaluations to fail the build and preserve reports for troubleshooting and audit evidence.

Open AWS CodeBuild →
AWS CodePipeline image AWS CodePipeline

CodePipeline can place validation and policy-test stages before infrastructure deployment. Protect pipeline configuration and deployment roles so that required checks cannot be bypassed by an alternative deployment path.

Open AWS CodePipeline →