Overview and What You Will Learn
In this lab, you will view the default state of an Organization Policy constraint, apply a constraint blocking external IP addresses on VMs at the Folder level, and confirm it automatically applies to every project inside that Folder without needing to configure each one individually.
Why This Matters in Production
A security team asks every engineer to "please avoid assigning public IPs to VMs unless genuinely necessary," and despite good intentions, inconsistent adherence means some VMs end up with unintended public exposure anyway. An Organization Policy constraint turns that request from a matter of individual discipline into an automatically enforced rule that applies the moment a new project is created under the same Folder, with no reminder needed.
Core Principles
Organization Policies let you set constraints that apply across an entire Organization, Folder, or Project - and like IAM permissions, a constraint set at a higher level automatically applies to everything beneath it, unless a lower level explicitly overrides it (where the policy allows overrides at all).
+------------------------------------------+| Constraint set at Organization or Folder |+------------------------------------------+ | v+------------------------------------------+| Automatically applies to every Project || beneath that level |+------------------------------------------+ | v+------------------------------------------+| Resource creation attempts violating the || constraint are blocked outright |+------------------------------------------+Detailed Step-by-Step Practical Lab
- Create a Folder to scope this lab's policy to:
gcloud resource-manager folders create \ --display-name="Policy Lab Folder" \ --organization=YOUR_ORG_ID- Check the current, default state of the constraint that controls whether VMs can have external IP addresses:
gcloud resource-manager org-policies describe \ compute.vmExternalIpAccess \ --folder=YOUR_FOLDER_ID- Apply a constraint blocking external IP addresses on VMs, at the Folder level:
cat > external-ip-policy.yaml << 'POLICYEOF'name: folders/YOUR_FOLDER_ID/policies/compute.vmExternalIpAccessspec: rules: - denyAll: truePOLICYEOF gcloud resource-manager org-policies set-policy \ external-ip-policy.yaml- Create a Project inside that Folder to see the constraint automatically apply:
gcloud projects create policy-lab-test-2026 \ --name="Policy Lab Test" \ --folder=YOUR_FOLDER_IDgcloud config set project policy-lab-test-2026gcloud services enable compute.googleapis.com- Attempt to create a VM with an external IP address, and confirm it is blocked by the inherited policy:
gcloud compute instances create vm-should-fail \ --zone=asia-south1-a \ --machine-type=e2-medium \ --image-family=debian-12 \ --image-project=debian-cloud## Expected: this fails with a policy violation error, since the## default behavior without --no-address would attempt to assign## an external IP, which the Folder-level constraint now blocks- Confirm creating the same VM without an external IP succeeds normally, since that does not violate the constraint:
gcloud compute instances create vm-no-external-ip \ --zone=asia-south1-a \ --machine-type=e2-medium \ --image-family=debian-12 \ --image-project=debian-cloud \ --no-address- Clean up:
gcloud compute instances delete vm-no-external-ip --zone=asia-south1-a --quietgcloud projects delete policy-lab-test-2026 --quietgcloud resource-manager folders delete YOUR_FOLDER_IDProduction Best Practices & Common Pitfalls
Common MistakeSetting an Organization Policy constraint directly at the Organization level for a rule that should genuinely only apply to production environments, then discovering it also blocks a legitimate need in a dev or sandbox Folder. Apply constraints at the most specific level (Folder, not Organization) whenever the rule's intent doesn't genuinely apply everywhere.
TipSet Organization Policies at the Folder level when a rule should apply to every project within a specific team or environment, rather than configuring the same constraint individually on each project. Policy inheritance means you only need to set it once at the right level.
- Not every constraint supports being overridden at a lower level. Some Organization Policy constraints are strictly enforced from wherever they're set, with no ability for a child Folder or Project to loosen them - check a constraint's specific documentation before assuming it can be selectively relaxed further down the hierarchy.
- Test a new constraint in a sandbox Folder first, before applying it broadly at the Organization level, to confirm it doesn't unexpectedly block a legitimate, existing workflow somewhere in the hierarchy.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud resource-manager org-policies describe |
View a constraint's current state at a given scope |
gcloud resource-manager org-policies set-policy |
Apply a constraint policy from a YAML file |
gcloud resource-manager folders create |
Create a new Folder to scope policies to |
gcloud compute instances create --no-address |
Create a VM without an external IP address |