Overview and What You Will Learn
An Organization Policy constraint is a rule that Google Cloud enforces on every project beneath the organization or folder where you set it. Where IAM controls who can act, an organization policy controls what any actor is allowed to create or configure. A constraint set on a folder blocks the risky setting in every current and future project inside it, with no per-project setup.
In this lab you will:
- Create a folder to scope the policy
- Read the effective state of the
compute.vmExternalIpAccessconstraint - Test a deny rule safely in dry-run mode before enforcing it
- Deny external IPs on VMs at the folder level and prove a new child project inherits it
- Add a narrow exception, then choose the right level (organization, folder or project) for a rule
Why This Matters in Production
Organization policies turn a security request into an enforced rule, so a risky setting is blocked at creation time instead of being found later in an audit. Imagine a security team at a company like Atlassian asking hundreds of engineers to avoid public IPs on VMs. Good intentions fail at that scale, and one forgotten flag exposes a database. A constraint on the production folder blocks the mistake everywhere, with no reminders needed.
Core Principles
Organization policies follow the resource hierarchy the same way IAM does: a constraint set on an organization or folder applies to everything below it. The effective policy for a resource is the result of combining every level above it, and where a constraint allows it, a lower level can override or extend the inherited value.
constraint set here] FLD --> P1[Project A] FLD --> P2[Project B] P1 --> BLK([VM with external IP
blocked]) classDef top fill:#475569,color:#FFFFFF classDef set fill:#326CE5,color:#FFFFFF classDef proj fill:#D1FAE5,color:#1E293B classDef risk fill:#EF4444,color:#FFFFFF class ORG top class FLD set class P1,P2 proj class BLK risk
| Concept | Meaning |
|---|---|
| Constraint | A named rule, such as compute.vmExternalIpAccess |
| Policy | The setting you apply for a constraint at one node |
| Effective policy | The merged result from every level above |
The person who sets the policy needs the Organization Policy Administrator role (roles/orgpolicy.policyAdmin) on the organization or folder. Being a project Owner is not enough, which is what makes these rules hard to bypass from inside a project.
Detailed Step-by-Step Practical Lab
Milestone 1: Create a Folder
A folder scopes the policy to the projects you choose. You need permission to create folders, such as roles/resourcemanager.folderCreator.
export ORG_ID=123456789012 gcloud resource-manager folders create \ --display-name="Policy Lab Folder" \ --organization=$ORG_ID export FOLDER_ID=456789012345Milestone 2: Check the Default State
Read the effective policy before changing anything. With nothing set, the constraint places no restriction on external IPs.
gcloud org-policies describe compute.vmExternalIpAccess \ --folder=$FOLDER_ID \ --effectiveMilestone 3: Test the Rule in Dry-Run Mode
A dry-run policy logs what would have been blocked without blocking anything. Use it on real environments before you enforce a rule, so you find the automation that quietly depends on external IPs.
cat > external-ip-dryrun.yaml << 'POLICYEOF'name: folders/456789012345/policies/compute.vmExternalIpAccessdryRunSpec: rules: - denyAll: truePOLICYEOF gcloud org-policies set-policy external-ip-dryrun.yamlViolations appear in Cloud Audit Logs, so you can review which creates would have failed. When the log is clean or the offenders are fixed, move on to enforcement.
Milestone 4: Enforce the Deny Rule at the Folder
Write the enforced policy and apply it at the folder. Every project in the folder is covered, now and in the future.
cat > external-ip-policy.yaml << 'POLICYEOF'name: folders/456789012345/policies/compute.vmExternalIpAccessspec: rules: - denyAll: truePOLICYEOF gcloud org-policies set-policy external-ip-policy.yaml gcloud org-policies describe compute.vmExternalIpAccess \ --folder=$FOLDER_ID --effectiveMilestone 5: Create a Child Project and Test It
Create a project inside the folder. It never receives its own copy of the policy, yet it is bound by it.
export PROJECT_ID=mumbai-policy-test-$(date +%s | tail -c 6) gcloud projects create $PROJECT_ID \ --name="Policy Lab Test" \ --folder=$FOLDER_IDgcloud billing projects link $PROJECT_ID \ --billing-account=012345-6789AB-CDEF01gcloud config set project $PROJECT_IDgcloud services enable compute.googleapis.com# This VM would get an external IP by default, so it should be refusedgcloud compute instances create mumbai-prod-vm-bad \ --zone=asia-south1-a \ --machine-type=e2-medium \ --image-family=debian-12 \ --image-project=debian-cloud \ --network=default# Expected: an error that names constraints/compute.vmExternalIpAccess# A new policy can take a few minutes to propagate, so retry if it succeeds # The same VM without an external IP is allowedgcloud compute instances create mumbai-prod-vm-1 \ --zone=asia-south1-a \ --machine-type=e2-medium \ --image-family=debian-12 \ --image-project=debian-cloud \ --network=default \ --no-addressFor outbound internet from VMs without external IPs, use Cloud NAT, and for administrator access use Identity-Aware Proxy rather than public SSH.
Milestone 6: Add a Narrow Exception
Some workloads, such as a bastion host, legitimately need an external IP. Allow named instances only, instead of turning the rule off.
cat > external-ip-exception.yaml << 'POLICYEOF'name: folders/456789012345/policies/compute.vmExternalIpAccessspec: rules: - values: allowedValues: - projects/mumbai-policy-test-12345/zones/asia-south1-a/instances/bastion-1POLICYEOF gcloud org-policies set-policy external-ip-exception.yamlReplace the project ID with your own. Only the listed instance may have an external IP, and every other VM in the folder is still blocked.
Milestone 7: Clean Up
gcloud compute instances delete mumbai-prod-vm-1 --zone=asia-south1-a --quietgcloud projects delete $PROJECT_ID --quietgcloud org-policies delete compute.vmExternalIpAccess --folder=$FOLDER_IDgcloud resource-manager folders delete $FOLDER_ID# The folder delete can fail while the project is still pending deletion.# An empty folder costs nothing, so retry later or leave it in place.Production Best Practices & Common Pitfalls
- Apply each constraint at the most specific fitting level. Production rules belong on a production folder, not the whole organization.
- Group projects into folders by environment or team. One policy then covers every current and future project.
- Roll out in dry-run mode first. Review the audit logs, fix offenders, then enforce.
- Prefer allow-lists for exceptions. Permit specific named resources instead of disabling the rule for a project.
- Use the effective view when debugging.
--effectiveshows the merged result that actually applies. - Keep policies in version control. Store the YAML in Git and apply it through CI so changes are reviewed.
- Check how a constraint can be overridden. Some cannot be loosened at lower levels, so read its documentation before designing exceptions.
Common MistakeSetting a constraint at the organization level for a rule that only belongs to production, then breaking legitimate dev or sandbox workflows. Scope each constraint to the folder that matches its intent.
TipName folders by environment (prod, staging, sandbox) so a single policy at the right folder covers every project beneath it.
SecurityA constraint only blocks new or changed resources. VMs that already have external IPs keep them until they are modified, so audit existing resources after enabling a deny rule.
RememberOrganization policies restrict what can be done, and IAM controls who may do it. You need both to build a safe environment.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud org-policies describe CONSTRAINT --effective |
Show the merged policy at a scope |
gcloud org-policies set-policy FILE |
Apply a policy from a YAML file |
gcloud org-policies list --folder=ID |
List policies set directly on a folder |
gcloud org-policies delete CONSTRAINT |
Remove a policy set at a scope |
gcloud resource-manager folders create |
Create a folder |
gcloud compute instances create --no-address |
Create a VM without an external IP |
| Symptom | Likely cause and fix |
|---|---|
| Cannot set the policy | Missing roles/orgpolicy.policyAdmin at that scope |
| VM still gets an external IP | Policy not propagated yet, or the resource existed before the policy |
| Unexpected block in one project | Inherited from a parent, check --effective at the project |
| Exception does not work | Instance path format wrong, use the full projects/.../instances/NAME form |