Azure cost and cleanup
Understand resource group and state boundaries
A resource group is an ARM lifecycle and management scope, not a Terraform state file. One state may manage several groups; one group may contain resources maintained by different systems. Deleting a group can remove resources you did not manage in your current Terraform root. Dependencies or locks can also prevent deletion. Keep retained databases, backups, shared networks and monitoring destinations in scopes appropriate to their owners. Use tags to identify lab resources and owners, but do not assume tags enforce permissions or that every resource supports identical tags.
Review management locks and deletion protections
An Azure management lock can block management-plane changes independently of a user’s RBAC role. A CanNotDelete lock prevents deletion while allowing other permitted changes; a ReadOnly lock has wider effects and may interfere with operations implemented through management-plane requests. Locks inherit down the resource hierarchy. They do not generally protect service data-plane operations. Compare an Azure lock to Terraform lifecycle prevent_destroy: the latter is a configuration-level planning guard, not an Azure policy.
# Static fragment; no live changes. Review lock ownership before teardown.resource "azurerm_management_lock" "retained_data" { name = "retain-data" scope = var.database_id lock_level = "CanNotDelete" notes = "Deletion needs separate data-owner approval."}If the lock itself is managed in the same root, a destroy plan may propose removing it first. A lock is therefore not a substitute for independent approval or recovery. A group deletion blocked by a retained resource is a review signal; never strip all locks as a routine fix. Confirm the lock owner and data-retention agreement, then approve only necessary scoped changes.
Account for soft deletion and name reuse
Key Vault soft deletion allows recovery during its configured retention interval; purge protection prevents premature permanent deletion while the retention policy applies. A deleted vault can reserve its globally scoped name until recovery or expiry, depending on its state and protections. Storage accounts also have globally scoped naming constraints, while many subnet names are scoped to their VNet. Check each service’s naming rules and availability; app and database endpoint naming may vary. Do not automatically purge a vault to make a deployment succeed. Review AzureRM provider feature settings for recovery and purge behavior before an authorized run; exact version semantics have not been verified here. For storage and databases, identify backups, retention policies, deletion protection and restore tests separately; do not equate soft delete with backup.
Estimate the cost before provisioning
Azure services charge using different meters. A VM that is deallocated generally stops compute charges but disks and some network resources can continue billing. App Service plans, database tiers, private endpoints, storage, backups, log ingestion and retention may each have their own charges. Stopping an app does not necessarily remove its plan cost. The following arithmetic uses invented prices and a 24-hour teaching window; it is not an Azure quote.
App plan: 24 hours x $0.10 = $2.40Private links: 3 x 24 hours x $0.01 = $0.72Database: 24 hours x $0.08 = $1.92Stored data: 10 GB x $0.02 = $0.20Log ingestion: 2 GB x $1.50 = $3.00Illustrative total: $8.24The sum is $8.24. If logs increase by 1 GB at the invented $1.50 rate, the sum becomes $9.74. The model omits taxes, transfer, requests, retention and other possible meters. Obtain real regional SKU prices and quotas, bound the duration and size, and assign a budget owner before an optional live exercise. Budget alerts can report spending but do not stop it by default. Diagnostic settings can incur destination and volume-related costs; review ingestion and retention rather than exporting everything indefinitely.
Practice a safe cleanup decision
Use the synthetic inventory in the worksheet. The catalog application and its private endpoint belong to the lab; the shared network and log workspace do not. A retained database has a deletion lock and approved backup retention. Do not run any destructive command to resolve this exercise. A reviewer should record each resource’s owner, proposed action, dependencies, expected residual cost and retention obligation. Reconcile plan actions against this inventory, stop if a retained item is proposed for removal, and obtain an approved plan from the appropriate owner. A state entry removed without deleting its remote object can still leave a billed resource.
app plan: remove only after all lab apps are removedprivate endpoint: remove only lab-owned endpoint and DNS associationshared network: keep; confirm other consumersretained database: keep; verify restore evidencelog workspace: keep; stop only lab-owned diagnostic exportsAfter an authorized cleanup, verify actual inventory and billing, not just Terraform exit status. Check separately for retained disks, snapshots, backups, public IPs, monitoring data, soft-deleted vaults and resources managed in other states. Preserve recovery evidence and access controls until retention obligations are satisfied. If deletion partially fails, inspect the current state and reconcile before retrying. No real account actions are included here.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Azure cost and cleanup — knowledge check
Review Azure architecture, identity and lifecycle.
A Terraform root owns one app inside a shared resource group. What is the safe cleanup scope?
What is a CanNotDelete management lock designed to prevent?
Why might a recently deleted Key Vault name be unavailable?
What may still incur charges after VM deallocation?
The hypothetical total is $8.24 at 2 GB log ingestion. What is the total at 3 GB, at $1.50 per GB?