Policy as code
Policy as code means the rules an organization wants enforced are written as files, versioned with the infrastructure, and applied by machines: by the cloud at request time, by a compliance service after the fact, and by scanners and guard scripts before a pull request can merge. In every edition of the BuiltForProd Baseline every rule has a file, and most have a check that fails the build when the rule is broken.
Why it matters in production
A rule that lives in a wiki is a suggestion. A rule that lives in a guardrail above the accounts, subscriptions or projects is enforced against every principal below it, including administrators, and its history is in Git. Rules as code also produce evidence automatically: an auditor can read the policy file, the compliance results and the pipeline logs instead of collecting screenshots for two weeks.
How each edition implements it
Every edition has the same three kinds of rule: guardrails the cloud enforces at request time, evaluation the cloud runs continuously, and checks the pipeline runs before merge.
Rules the cloud enforces at request time
AWS Enterprise Baseline. Three SCPs are defined in modules/organizations/scps.tf and attached to both organizational units by the organizations unit:
| Policy | Denies | Exceptions |
|---|---|---|
acme-security-guardrails | Creating IAM users, login profiles and access keys; deleting or changing S3 Block Public Access; any action without MFA | Root for IAM users; AWSReservedSSO_*, acme-terraform-* and acme-github-actions-* roles for the block; SSO sessions, acme-* roles, service-linked roles, root and roles tagged mfa-exempt = true for MFA |
acme-region-restriction | Any action outside the allowed regions (global services excluded) | Allowed regions are the home region, every region with folders and us-east-1, computed by the unit |
acme-audit-protection | Stopping, deleting or updating CloudTrail; stopping or deleting the Config recorder or delivery channel | Root |
The allowed-region list is computed from the same folder discovery IPAM uses, so a new region folder extends the policy without editing it. AWS allows five SCPs per OU including FullAWSAccess; the platform uses three.
The organization tag policy in modules/organizations/tags.tf pins the key casing and allowed values of Namespace, Environment, Stage, ManagedBy and Repository (and the casing of Team). It is report-only until enforce_tag_policy = true, because enforcement rejects tagging calls from the console, auto scaling and EKS that use a value outside the list; the switch is flipped only once the compliance report is clean. The Environment list is fed from every region.hcl in the tree.
Azure Enterprise Baseline. Four policy sets are assigned at mg-acme from modules/organizations/, so no subscription Owner can remove them: the security guardrails initiative (seven Deny and three Audit built-ins, plus a custom definition that denies role assignments to individual users), the region restriction, the audit protection initiative with DenyAction on the audit workspace, the audit storage and diagnostic settings, and the tag policy. Built-in definitions are looked up by display name, so no definition ID appears in the code. acme-core-public holds the one exemption, acme-allow-public-blobs. The tag policy audits by default; enforce_tag_policy switches it to Deny, and Modify policies copy missing tags from the resource group. The Azure Policy explainer lists every assignment.
GCP Enterprise Baseline. modules/organizations/org-policies.tf sets 19 organization policy constraints at the organization node, 11 boolean and 8 list: service account key creation and upload, public access prevention, uniform bucket access, OS Login, Shielded VMs, external IPs, peering and Shared VPC host limits, Cloud Run ingress, contact domains, the member domains, the Workload Identity Federation provider and resource locations, among others. gcp.resourceLocations is computed from the region folders, like the AWS region list. An IAM deny policy protects the audit trail. A custom constraint that requires the platform's labels runs in dry run until enforce_label_policy is set.
Rules the cloud evaluates continuously
AWS Enterprise Baseline. AWS Config records every supported resource type in every account and region and aggregates in core-audit. The aws-config unit of the management account deploys organization conformance packs from a library of 29 in modules/aws-config/conformance-packs/. BuiltForProd-soc2-baseline (13 rules, such as encrypted volumes, multi-region CloudTrail, S3 SSL-only, MFA for console and VPC flow logs) is on by default; the other 28 (HIPAA, PCI DSS v4, NIST CSF, CMMC 2.0 levels 1 and 2, CIS Critical Security Controls v8 IG1 to IG3, the Well-Architected security and reliability pillars, and service packs for S3, IAM, EKS, RDS, Lambda, WAF and more) are switched on per compliance requirement in the unit's conformance_packs map. plat-sandbox is excluded. Security Hub standards (CIS v5, AWS Foundational Security Best Practices, PCI DSS v4, the tagging standard) are separate switches in security.hcl.
Findings flow to Security Hub in core-security; selected non-compliance and high-severity findings reach the alerts topic, and a public S3 bucket is remediated automatically, as described under defense in depth.
Azure Enterprise Baseline. Azure Policy compliance runs continuously at mg-acme. The Microsoft cloud security benchmark and the custom initiative BuiltForProd-soc2-baseline, assigned as acme-soc2-base with 13 controls, are on. Nine built-in regulatory initiatives (CIS Azure v3.0.0, NIST SP 800-53 Rev. 5, NIST SP 800-171 Rev. 2, PCI DSS v4.0, HIPAA HITRUST, SOC 2 Type 2, ISO 27001:2013, CMMC Level 2 and FedRAMP Moderate) are switches in security.hcl, assigned in DoNotEnforce mode so they report without blocking, and all free. The sandbox subscription carries waiver exemptions. A custom definition, acme-audit-storage-cmk, checks that the audit storage uses a customer-managed key.
GCP Enterprise Baseline. Security Command Center evaluates the organization continuously (Security Health Analytics in the Standard tier), and 13 log-based detections over the central audit bucket alert on security-relevant changes, eight of them from section 2 of the CIS Google Cloud Foundations Benchmark. The edition has no conformance packs: the preventive constraints carry most of the rules.
Rules the pipeline enforces before merge
Every infrastructure repository of every edition runs the same kind of checks in pre-commit and in the lint job of plan.yml, before any cloud call. The guard scripts have the same names in the three Baseline repositories, except the address-plan guard:
| Check | Rule |
|---|---|
scripts/check-no-hardcoded-cidrs.py | No private CIDR literal in .hcl or .tf outside comments, 0.0.0.0/0, /32 routes and mock blocks (landing zone) |
check-vpc-maps.py, check-vnet-maps.py, check-network-maps.py | The address plan valid, nested, non-overlapping; every unit's ipam_key resolves (AWS, Azure, GCP) |
scripts/check-mock-outputs.py | Every dependency has complete mock_outputs allowed for validate and plan |
scripts/check-stack-layout.py | A stack file beside every region.hcl, no hand-written unit under environments/, every definition referenced, repo-root sources |
scripts/check-module-versions.py | Provider pins live in root.hcl; a module versions.tf repeats them exactly |
scripts/check-required-inputs.py | Every generated unit supplies its module's required inputs |
tofu fmt, terragrunt hcl format, tflint v0.64.0 | Formatting and the recommended ruleset plus the edition's ruleset: AWS 0.48.0, tflint-ruleset-azurerm 0.32.0, tflint-ruleset-google 0.40.0 |
| Checkov, Trivy | Insecure resource configuration in modules/; Checkov is a hard fail |
Skips are allowed only with a reason on the same line (#checkov:skip=CKV_AWS_7: <why>, # tflint-ignore: <rule> # <why>); the Azure registry module, for example, explains each skipped check that only the Premium SKU can satisfy. Code ownership routes reviews by blast radius in every edition: environments/core/, the address plan, root.hcl, common.hcl, .github/ and scripts/ to the infra admins; security.hcl to the infra admins and the security team; plat/prod to platform leads and security. On Azure and GCP the security team also owns policies/ (the WAF and Cloud Armor templates), on GCP also src/ (the notifier), and on Azure the DevOps leads co-own runner-image/.
The application chart carries its own policy in code in every edition: a default-deny Kubernetes NetworkPolicy with an explicit allow-list, enforced by the VPC CNI's network policy agent on AWS, Cilium on Azure and Dataplane V2 on GCP.
Worked example: a pull request that hard-codes a subnet
An engineer adds private_subnets = ["10.2.0.0/18", ...] to a unit to work around an IPAM lookup. Pre-commit fails on the CIDR literal before the commit exists. If the hook was skipped, plan.yml fails in the first job, before any AWS call, with the file and line. The fix is to read the address from dependency.ipam.outputs as every other VPC unit does. The same guard, scripts/check-no-hardcoded-cidrs.py, runs in the Azure and GCP repositories against vnet_map.yaml and network_map.yaml. Nothing about the rule was discussed in review, because the rule is not a matter of opinion; it is infrastructure as code applied to the rules themselves.
Common mistakes
- Relaxing a guardrail to unblock a deployment. The guardrail applies to everything below it. On AWS the exceptions are principal-based: name the role
acme-*or tag itmfa-exempt = true. - Enabling every compliance evaluation at once. AWS conformance packs add Config rule evaluations and cost; Azure initiatives are free but add findings to triage. Switch them on per compliance requirement.
- Enforcing a tag or label rule before its report is clean (AWS, Azure, GCP). The AWS tag policy and the Azure tag policy in Deny mode reject calls with unlisted values, and the GCP label constraint rejects unlabeled resources; enforce each only once its compliance report or dry-run log is clean.
- Skipping a scanner finding without a reason. A skip needs its reason on the same line; reviewers read it.
- Running the guards only in CI. The pre-commit hooks run the same scripts locally, which is cheaper than a failed pipeline.