Cost and teardown planning
What you will learn
Cloud estimates combine usage quantities and rates for compute, databases, storage, networking, requests and telemetry. A budget alert reports spend; it does not automatically guarantee a hard ceiling. An approved lab must have a resource inventory, owner, expiration and verified teardown.
- Calculate a cost estimate with explicit units and fictional rates.
- Identify indirect costs and estimate limitations.
- Design an ownership-scoped teardown verification.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Standalone Java 21 functional examples. These are offline Java 21 models, not cloud or hosted CI execution. Companion local pipeline evidence is separate.
Calculate an illustrative subtotal
public class Main { static java.math.BigDecimal cost(String units,String rate) { var u=new java.math.BigDecimal(units); var r=new java.math.BigDecimal(rate); if(u.signum()<0 || r.signum()<0) throw new IllegalArgumentException("negative usage or rate"); return u.multiply(r); } public static void main(String[] args) { var total=cost("100","0.06").add(cost("20","0.10")).add(cost("10","0.20")); System.out.println(total.setScale(2,java.math.RoundingMode.HALF_UP)); }}Expected output:
10.00Each multiplication uses compatible billing units; all rates are fictional.
A dated, reproducible estimate
Use a selected provider calculator and record region, currency, date, SKU, billing units, discounts, tax, support, license terms and workload assumptions. As of the source review on 2026-11-28, Cloud Run pricing documentation distinguishes billing configuration and regional tiers; Azure publishes estimated Redis pricing; AWS offers a pricing calculator. No account-specific quote or region-specific rate was verified for this lab. The offline model uses fictional USD rates: 100 instance-hours × $0.06/hour = $6.00; 20 GB egress × $0.10/GB = $2.00; 10 GB-month × $0.20/GB-month = $2.00. This $10.00 excludes DB, requests, tax and telemetry. Add those explicitly for a real estimate. Use BigDecimal from strings; preserve precision until display.
Teardown and stop conditions
Set an approved owner, account/project, region, allowed services, quota and time limit before provisioning. Use budget notifications alongside enforced quotas and a stop policy; billing can lag. Track each exact resource ID and dependencies: application, compute, load balancer, registry artifacts, public IPs, DB, disks, cache, queues, logs and backups. Remove only lab-owned resources with explicit authorization in dependency-safe order; respect retention requirements and shared infrastructure. Verify each resource no longer exists and inspect subsequent bills. If cleanup fails, stop and report remaining IDs rather than expanding spend. The exercise only filters a synthetic inventory and cannot delete anything.
Find remaining owned resources
public class Main { record Resource(String id, String owner, boolean present) {} static java.util.List<String> remaining(java.util.List<Resource> inventory,String owner) { return inventory.stream().filter(r->owner.equals(r.owner()) && r.present()).map(Resource::id).sorted().toList(); } public static void main(String[] args) { var inventory=java.util.List.of(new Resource("db","lab",true),new Resource("app","lab",false),new Resource("shared","other",true)); System.out.println(remaining(inventory,"lab")); }}Expected output:
[db]The model reports an undeleted database and excludes a resource belonging to another owner.
Evaluate an offline spending gate
public class Main { static boolean within(java.math.BigDecimal estimate,java.math.BigDecimal limit) { if(estimate.signum()<0 || limit.signum()<=0) throw new IllegalArgumentException("budget"); return estimate.compareTo(limit)<=0; } public static void main(String[] args) { System.out.println(within(new java.math.BigDecimal("10.00"),new java.math.BigDecimal("9.99"))); }}Expected output:
falseAn estimate over the limit fails this offline approval gate; it is not provider billing enforcement.
Common mistakes
Do not treat free-tier marketing as a cost guarantee, compare incompatible units, ignore persistent storage or assume deleting the app deletes its database and backups. Never delete shared resources because their names resemble a lab prefix.
Practice
Estimate 50 instance-hours at fictional $0.08/hour and 5 GB egress at fictional $0.12/GB. Print the sum to two decimal places.
Expected output:
4.60Compare with the manual review criteria.
Further reading: Google pricing · AWS pricing · Azure pricing · Budget alerts. Independent editorial review remains pending.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Cost and teardown planning — knowledge check
Cloud deployment and CI/CD reasoning.
What belongs in a cost estimate?
Are the sample rates current provider quotes?
Does an alerts-only budget guarantee a hard cap?
What must teardown verify?
What to do if authorized cleanup fails?