Home› Blog› Week 3 — Organization Policy on Google Cloud: Twel…
GCP Weekly Lab GCP Terraform

Organization policy guardrails, and what dry run is for

Find out what the platform already decided before adding anything — then deploy guardrails that report before they refuse.

Verified against current vendor documentation on 3 September 2026. Pricing, limits and API behaviour were checked against the official docs on that date. Cloud services change fast — if you are reading this much later, treat the specifics as a starting point and re-check the linked sources.
GCP Platform Engineering Lab · Week 3 of 58

Lab Overview

The problem. A guardrail that refuses something on its first day breaks whatever was already doing that thing, and you find out from an incident rather than a report. The other failure is quieter: adding a rule the platform already enforces, and believing you improved something.

Objective. Read what this organization already enforces, then add three managed constraints and one custom constraint in dry run, so violations are logged before anything is denied.

Expected outcome. Four policies reporting rather than refusing, one deliberate folder-level exception, and a measured figure for how long enforcement actually takes to take effect.

DifficultyIntermediate — the policies are simple; the API's concurrency and lag behaviour are not
DeploymentTerraform via HCP Terraform — the first fully remote week, no hand-run apply
Reproduce itLab guide — the baseline read, the 409, the lag measurement
Cost$0 — organization policy is free

Cloud Services and Concepts

ServicePurpose hereWhat it is
Organization Policy ServiceApplies the four guardrails this week setsApplies restrictions to a resource and everything beneath it. Evaluated when a resource is created or changed, so it refuses rather than reports after the fact.
Managed constraintsThree of the four rules, shipped by GoogleRules Google ships, named *.managed.*. Some are enforced by a Google default you never set.
Custom constraintsThe fourth rule, which Google does not shipA CEL expression over a resource's own fields, for rules Google does not ship.
Dry-run policiesLets every rule be observed before it denies anythingEvaluate and log, without denying. The difference between a guardrail and an outage on day one.
Cloud Audit LogsWhere the dry-run violations are read fromWhere dry-run violations land — in the project log, not the organization's.

Two concepts that decide how this week goes. First, "set" and "enforced" are different: a managed constraint can be fully in force because of a Google default, while appearing in no list of policies you configured. Only describe --effective tells the truth, and reading it first is the difference between adding a guardrail and re-adding one.

Second, dry run is not a test mode you skip. A constraint in dry run records what it would have refused. That log is the evidence you need before promoting it — and without a matching resource in the organization, it produces nothing at all, which looks exactly like a constraint that works.

Architecture and Workflow

Report before you refuse GCP Lab · Week 3 all DRY RUN organization requireOsLogin blockProjectSshKeys disableSerialPortAccess requireTerraformLabels workloads / dev one exception, scoped Icon_24px_Logging_Color the PROJECT log where violations land not the organization log that stream does not exist, and querying it returns empty with no error at all inherits down logs to 7 constraints were already enforced before any of this list shows what is SET, --effective shows what is ENFORCED jayanthkatta.com org:96,160,420,220;exc:96,470,420,130;note:680,520,500,160

The flow

  1. Read what is already enforced with describe --effective, before designing anything.
  2. Write three managed Compute constraints and one custom bucket-label constraint, all in dry run.
  3. Chain them with depends_on, because the API serializes policy changes per parent.
  4. Add one deliberate exception at workloads/dev, disabling the serial-port constraint for that folder only.
  5. Create a violating resource on purpose and confirm it is logged rather than refused.
  6. Leave them in dry run until there is evidence to promote on — which, for the Compute constraints, meant waiting for Week 04.

Two decisions worth naming

Dry run, not enforce. Three of these constraints govern Compute Engine, and at this point the organization contained no Compute resources at all. A constraint with nothing to evaluate produces no violations, which is indistinguishable from a constraint that is working — so promoting on "no violations" would have proven nothing.

One exception, named and scoped. workloads/dev overrides the serial-port constraint. An exception that exists is honest; an exception nobody can find is how a guardrail quietly stops applying.

Prerequisites

  • An organization with a folder hierarchy — Week 1 supplies it, and the exception needs somewhere to live.
  • roles/orgpolicy.policyAdmin at the organization on the apply identity.
  • roles/logging.viewer at the organization for whoever reads dry-run violations — granted out of band, because the pipeline identity cannot be impersonated.
  • orgpolicy and logging APIs enabled on the quota project. A clean plan is not evidence the apply will work — writing policy needs the API, reading it does not, and a plan only reads.
  • Terraform 1.9+ with hashicorp/google ~> 6.0.

The baseline-reading commands and the lag measurement are in the lab guide.

Implementation

Eight resources, all free. Commands are in the lab guide.

Repository structure: LAB.md, terraform, scripts and docs, each with a one-line responsibility.
How the week is organised. Full source on GitHub.

Step 1 — Read what the platform already decided

Why: because list shows what is set, not what is enforced. This organization already had seven enforced constraints from Google's security baseline, all carrying the same updateTime from the moment it was created.

Checkpoint: list reported 12 policies; the console reported 26 active. One constraint was added to this lab as a new guardrail and turned out to be enforced already — removing our policy left it enforced, which proves the default is independent of anything set here.

Organization policies page showing policies in dry run alongside actively enforced ones.
Validates Step 1: dry-run policies alongside the baseline's enforced ones. The distinction is invisible to gcloud org-policies list, which is why the console and the CLI disagreed by fourteen policies.

Step 2 — Write the constraints, chained

Why chained: the Organization Policy API serializes changes per parent, and Terraform's default parallelism of 10 produces HTTP 409 CONCURRENT_POLICY_CHANGES partially and non-deterministically. The CLI fix is -parallelism=1; HCP refuses it, so ordering has to be an edge in the dependency graph.

The custom constraint definition requiring Terraform labels on buckets.
Validates Step 2: the custom constraint, scoped to bucket CREATE and UPDATE. Custom constraints are where rules Google does not ship get written.

Step 3 — Prove it reports rather than refuses

Action: create an unlabelled bucket on purpose, as an identity that holds permission to create it.

Expected: the request succeeds and a violation is logged.

HCP Terraform run showing Applied with eight resources, after three failed attempts.
Validates deployment only: eight resources applied — on the fourth attempt. The three failures are in Troubleshooting and none was a flake.
The custom constraint later shown in enforced state.
The promotion, which came later: the custom constraint moved from dry run to enforce once there was violation evidence to justify it. The three Compute constraints had to wait for Week 04, because nothing in the organization used Compute yet.

Validation and Evidence

TestExpectedActualStatus
Infrastructure deployed8 resources8 added, on the fourth attemptPass
What was already enforcedread before designing7 constraints from Google's baselineFinding
This week's four, and each stateall dry runall dry runPass
The custom constraint's definitionCEL expression presentpresent, CREATE + UPDATE on bucketsPass
The deliberate exception at workloads/devenforce = FALSEpresent and scoped to one folderPass
A violating request is logged, not refusedbucket created, violation loggedcreated; OrgPolicyDryRunAuditMetadata in the project logPass
Enforcement lag, both directionsmeasured, not assumed75–100 s to enforce, 77–108 s to un-enforce, 3 trialsPass
The three Compute constraints deny anythinga real denialnot tested this week — no Compute resource existed to evaluate; promoted in Week 04Unverified

Row two changed the week. Reading --effective first revealed seven constraints already enforced by Google's baseline — one of which this lab had been about to add as an improvement.

Row seven is a real measurement and it started as a bad one. The lag was first written up from a single success followed by a single failure, with the same confidence as the reproducible findings around it. One success and one failure cannot distinguish a propagation delay from a transient error. Repeating it three times produced a range — and turned up the reverse lag, which is the operationally dangerous direction, by accident.

Row eight is honest about the limit: three of the four constraints govern Compute Engine, and nothing in the organization used Compute. They denied nothing because there was nothing to deny.

Security and Cost

Security decisions

ControlDecision and trade-off
Dry run firstReport before refusing. A guardrail enforced on day one breaks whatever was already doing the thing, and you learn from an incident.
Read the baseline firstSeven constraints were already enforced. Adding a rule that already exists is not a security improvement, it is a false sense of one.
Scoped exceptionOne folder, one constraint, visible in the configuration. An exception nobody can find is how a guardrail silently stops applying.
Out-of-band grantroles/logging.viewer at the organization was granted by hand so violations could be read — read-only, reversible, and recorded with a removal command.
Accepted gapThe Compute constraints could not be tested here. "Deployed" and "demonstrated" are different, and this week only achieved the first.

A monitoring trap worth repeating: the dry-run audit record carries status.code 7 POLICY_VIOLATED on a request that succeeded. Anything alerting on status.code != 0 will report an outage that is not happening.

Cost

$0. Organization policy is free, and this week creates no billable resource at all.

Troubleshooting and Lessons

A clean plan, then `SERVICE_DISABLED`

Problem: terraform plan reported no problems and the apply failed immediately.

Root cause: writing org policy needs orgpolicy.googleapis.com on the quota project. Reading it does not, and a plan only reads.

Lesson: a clean plan is not evidence an apply will work. This is the general form, and it recurs: the plan exercises read paths only.

HCP Terraform run failing with SERVICE_DISABLED.
The first of three failures, each a different cause. Kept because none was a flake, and "it worked on the fourth try" is not a diagnosis.

HTTP 409 `CONCURRENT_POLICY_CHANGES`

Problem: policies applied partially and non-deterministically.

Root cause: the API serializes changes per parent; Terraform runs ten in parallel by default.

Resolution: -parallelism=1 fixes it on the CLI, and HCP Terraform refuses the flag — "Custom parallelism values are currently not supported." On remote execution the ordering must be an edge in the dependency graph, so the resources are chained with depends_on.

Lesson: applies to every later week that writes several policies to one parent.

HCP Terraform run failing with HTTP 409 CONCURRENT_POLICY_CHANGES.
The second failure. The fix that works locally is unavailable remotely, which is the kind of difference worth knowing before an incident.

A script that degraded a control and left it that way

Problem: the first version of the lag-measurement script crashed mid-cycle, having already flipped a constraint back to dry run, and left the organization in a state the published post contradicted — live, until someone noticed.

Root cause of the crash: trivial. subprocess cannot launch a bare gcloud on Windows, because it is a .CMD shim and CreateProcess does not apply PATHEXT. Resolve with shutil.which.

The damage was the failure path, not the crash.

Lesson: a script that degrades a control must restore it in a finally block, never at the end of the happy path.

Cleanup and Key Takeaways

Cleanup

terraform destroy removes all eight resources. Then, out of band, remove the operator's organization-level roles/logging.viewer if it is no longer needed — scripts/cleanup.sh carries the command, because a grant made by hand will not be removed by Terraform.

Consider keeping it. These constraints are the baseline every later week assumes, and the three Compute ones were promoted to enforce in Week 04. Commands are in the lab guide.

Ongoing charges: none.

Key takeaways

  1. gcloud org-policies list shows what is set, not what is enforced. Always read --effective before concluding a constraint is unset — seven were already in force here.
  2. One success followed by one failure is not a measurement. The enforcement lag was first written up from n=1; repeating it three times produced a range and revealed the reverse lag.
  3. A clean plan is not evidence an apply will work, because a plan exercises only read paths.
  4. Dry-run violations land in the PROJECT audit log, and the record reports POLICY_VIOLATED on a request that succeeded — which will trip any alert keyed on a non-zero status code.
  5. A script that degrades a control must restore it in finally. The crash is never the damage; the failure path is.

References

Comments

How was your experience?
Your feedback helps improve this site.
PoorExcellent