How-to & Guides ยท How-to
How to Implement policy-as-code
Express cloud guardrails as versioned, testable policies with preventive, detective, deployment-time, and exception controls across multiple providers.
Document class: How-to Guides implementation guide Normative terms: MUST, MUST NOT, SHOULD, SHOULD NOT, and MAY are requirements levels. Applicability: Versioned policy definitions, testing, preventive, detective, and deployment enforcement, parameters, exceptions, and multi-cloud mapping. Exception process: Deviations require a documented risk assessment, compensating controls, named risk owner, and expiry date.
| Control field | Value |
|---|---|
| Document ID | HTG-27 |
| Owner | Cloud Center of Excellence |
| Review cycle | At least annually and after material policy, provider, or compliance changes |
| Evidence | Policy source, test matrix, assignment state, compliance results, remediation logs, exception record, and review date |
Decision in brief: Express one control objective as versioned tests and provider-specific enforcement, with exceptions explicit, owned, and expiring.
Document type: Governance implementation guide Primary example: Azure Policy with Terraform and CI validation Operating principle: Normalize the control objective, then implement the strongest safe enforcement available in each cloud.
Objective
Convert architecture, security, cost, residency, and operational requirements into policies that are reviewed, tested, deployed, measured, and safely changed. Policy-as-code includes provider-native controls, IaC scanning, admission policy, compliance queries, exception workflow, and remediation evidence.
Enforcement model
Build the policy catalog
For every policy, define a stable control ID, requirement, rationale, scope, severity, mode, parameters, provider mappings, exclusions, owner, remediation, evidence, version, and deprecation plan. Group policies into approved initiatives or bundles aligned to landing-zone archetypes.
Implementation procedure
- Prioritize controls that prevent public exposure, excessive privilege, unencrypted data, missing logs, unsupported regions, and unowned resources.
- Write positive, negative, boundary, exemption, and backward-compatibility tests.
- Evaluate Terraform or other IaC in pull requests before provider deployment.
- Roll out provider policy in audit mode and establish the current compliance baseline.
- Correct false positives and narrow policy parameters; never use a broad exclusion to solve one workload issue.
- Enforce on new resources, then remediate existing resources through an approved plan.
- Monitor evaluation failures, denied deployments, exemptions, drift, and policy changes.
- Promote policy releases through protected environments with rollback artifacts.
Provider mapping
| Capability | Azure | AWS | GCP | OCI |
|---|---|---|---|---|
| Organization guardrails | Azure Policy / management groups | SCPs, Config, Control Tower controls | Organization Policy and custom constraints | Security Zones and Cloud Guard |
| IaC evaluation | OPA/Conftest or scanners | OPA/Conftest or scanners | OPA/Conftest or scanners | OPA/Conftest or scanners |
| Kubernetes admission | Gatekeeper, Kyverno, or managed policy | Gatekeeper/Kyverno | Policy Controller/Gatekeeper | Gatekeeper/Kyverno |
| Evidence | Resource Graph and compliance state | Config aggregators and Security Hub | Asset Inventory and SCC | Search, Cloud Guard, and Audit |
Exception lifecycle
Require requester, business reason, affected resources, compensating control, risk owner, approval, issue reference, start date, expiry, and review cadence. Encode exemptions at the narrowest scope. Alert before expiry and fail closed unless renewal is approved. Permanent exemptions indicate that the policy or architecture must be redesigned.
Safe remediation
Auto-remediate only when the action is idempotent, low risk, bounded, and tested. Adding a required tag may be safe; changing routes, encryption keys, identity assignments, or public access can cause outage or data loss. Use a reviewed change workflow for high-impact correction.
Policy-engine availability and bypass resistance
Treat policy evaluation as a production dependency. For each enforcement point, define what happens when the evaluator is unavailable or its policy data is stale: block the change, use a verified last-known-good policy, or permit a narrowly scoped emergency path. Keep provider-side preventive controls active when CI evaluation is unavailable; an evaluator outage must not become a global bypass. Alert on policy-sync age and the time of the last successful evaluation. Test outage behavior in a non-production scope, and record every override with an owner and expiry.
Validation
- Policy tests cover compliant, noncompliant, boundary, and exempt resources.
- Pull requests fail with an actionable control ID and remediation message.
- Provider enforcement cannot be bypassed by normal workload identities.
- Compliance state aggregates every account boundary and reports stale evaluation.
- Exceptions are scoped, approved, monitored, and expire automatically.
- Policy rollback and remediation reversal are tested before production rollout.
Operational boundaries and acceptance criteria
Before adopting this guidance, record the accountable service owner, supported environments, critical dependencies, data classification, and the operational tier. State which decisions belong to the platform team and which remain with the workload team; document any exception with a risk owner and expiry date. This prevents a reference pattern from being treated as a universal fit.
Define observable acceptance checks before rollout. Include a positive path, a denied or invalid request, a dependency failure, and recovery after the failure clears. Capture the configuration or command output that proves each check, along with the software and policy versions used. Make checks repeatable in a representative non-production environment, and ensure they fail closed when required evidence is missing.
For production changes, identify the safe rollback or forward-recovery action, the person authorized to invoke it, and the condition that triggers it. Confirm that monitoring, support ownership, capacity limits, and escalation contacts are in place before declaring the change complete. Review these assumptions after a material platform, provider, or regulatory change.
Exceptions, evidence, and control ownership
For each guardrail, identify its control owner, enforcement point, evidence source, and response when it fails. Separate preventive controls from detective controls: a policy decision at deployment time does not prove that runtime configuration remains compliant. Reconcile declared intent with provider inventory and audit events, and assign findings according to service ownership rather than leaving them in a central queue.
Re-evaluate each exception after a material architecture or threat change, and automate expiry reminders. Keep evidence tamper-resistant and attributable to a person or workload identity. Test that emergency access can restore service without silently bypassing audit, and verify that temporary privileges and policy exemptions are removed after recovery.
Related topics
- Policy Guardrails and Compliance
- Infrastructure as Code Engineering Standard
- How to Validate Infrastructure Before Release
Related repos
- andyxuan2010/azure-landingzone โ provides an Azure landing-zone implementation where management-group policy bundles and compliance controls can be applied.
- andyxuan2010/aws-landingzone โ provides an AWS multi-account foundation suitable for SCP and Config guardrails.
- andyxuan2010/oci-landingzone โ provides OCI foundation code for governed compartments, networking, and policy enforcement.