Lab — Python GCP Inventory¶
Lab Overview¶
Purpose: Produce a GCE instance inventory (name, zone, status, labels).
Scenario: Learners may lack GCP projects; fixtures keep the lab runnable everywhere.
Expected outcome: --fixture path always works; live path optional with Application Default Credentials.
This is a lab, not a tutorial
Business Scenario¶
A multi-project landing zone needs a weekly inventory of running VMs missing owner labels.
Learning Objectives¶
- Flatten instance records to a common schema
- Flag missing required labels
- Fixture-first; graceful live failures
Prerequisites¶
Knowledge¶
Software¶
Fixtures required; google-cloud-compute optional. Estimated cost: £0.
Architecture¶
Environment¶
mkdir -p ~/rebash-lab-python-gcp/{fixtures,out}
cd ~/rebash-lab-python-gcp
python3 -m venv .venv && source .venv/bin/activate
Initial State¶
cat > fixtures/instances.json << 'EOF'
[
{"name": "api-1", "zone": "europe-west2-a", "status": "RUNNING", "labels": {"owner": "platform"}},
{"name": "scratch", "zone": "europe-west2-b", "status": "TERMINATED", "labels": {}}
]
EOF
Task¶
gcp_inventory.py --fixture fixtures/instances.json → out/inventory.json plus out/missing_labels.json for instances lacking owner.
Validation¶
python gcp_inventory.py --fixture fixtures/instances.json
python -c 'import json; assert len(json.load(open("out/missing_labels.json")))==1'
- Fixture works offline
- Missing label detection works
- No service account JSON committed
Troubleshooting¶
| Symptom | Fix |
|---|---|
| ADC errors | Expected without GCP — use fixtures |
Cleanup¶
Production Discussion¶
Use workload identity federation for CI; prefer Cloud Asset Inventory for organisation-scale queries.