Skip to content
Visual explainer 2h 30m to learn 5 steps

Prepare an Azure lab with budgets and cleanup

Set up a lab with CLI context, tags, a budget alert and a resource group you can delete in one step.

By Invicly Tech, , in Microsoft Azure Cloud Engineering. About 13 minutes to read.

Topic 3 of 33

In short

A safe lab is one you can see, afford and remove. You work from the portal or Cloud Shell, point the Azure CLI at the right subscription, put every lab resource in one tagged resource group, and add a budget that emails you before spend gets out of hand. When you are done, deleting the resource group removes everything inside it in one step.

Think of it like this: Treat the lab like a rented workshop: label your toolbox, set a spending limit with the shop, and clear the bench before you hand back the key.

Explained simply

Before you build anything, set up a lab that is safe for your wallet. You need an Azure subscription you are allowed to experiment in. New users can start with the Azure free account, which gives a time-limited credit and some services free for a period; check the current terms on the offer page, because they change. A company may give you a sandbox subscription instead.

You can work in two places. The Azure portal is the website where you click through settings. Cloud Shell is a terminal inside the portal with the Azure CLI already signed in. You can also install the Azure CLI on your own computer and sign in with az login.

The CLI always works against one subscription at a time, called the current context. If you have access to several, set the right one with az account set and check it with az account show. Doing this first prevents the classic mistake of creating lab resources in a work subscription.

Put everything for one lab in one resource group, and give it tags such as env=lab, owner and an expiry date. Then add a budget in Cost Management, which emails you as spending passes a threshold. A budget only warns you; it does not switch anything off.

When you finish, delete the resource group. That removes every resource inside it in one step and stops the charges for them. A quick check afterwards confirms nothing is left behind.

Press Play on a diagram to walk through it one step at a time.

CycleThe lab lifecycle
  1. 1
    Set context

    az login, then az account set to the lab subscription

  2. 2
    Create tagged group

    One resource group per lab, with owner and expiry tags

  3. 3
    Build and test

    Deploy only into that group

  4. 4
    Watch cost

    Budget alerts and Cost analysis by tag

  5. 5
    Delete the group

    az group delete removes everything inside

  6. Back to step 1
MapWhat a budget can and cannot do
Cost Management budget
  • Can
    • Alert on actual spend
    • Alert on forecast spend
    • Email people or trigger an action group
    • Scope to a subscription or resource group
  • Cannot
    • Stop or delete resources by itself
    • React instantly to usage
    • Replace cleanup

In depth

Portal, Cloud Shell or local CLI

The portal is best for exploring: it shows options you did not know existed and links to documentation. The downside is that clicks are hard to repeat. The Azure CLI turns each step into a command you can save in a script and run again.

Cloud Shell gives you the CLI, PowerShell, Bicep and common tools in the browser, already signed in as you. It can run without a storage account, which gives a fresh session each time, or with an Azure Files share attached so your home directory persists; that share is a small cost. A local install is better for longer work with your own editor. Install it from the official instructions for your operating system, then run az login, which opens a browser to sign in. Keep the CLI updated with az upgrade, because new services and flags arrive often.

  • Portal: explore and verify
  • CLI: repeatable steps you can script
  • Cloud Shell: nothing to install
  • az version and az upgrade keep the CLI current

Setting and checking the CLI context

After az login the CLI picks a default subscription, which may not be the one you want. az account list shows every subscription you can reach, with IsDefault marking the current one. az account set --subscription with a name or ID switches the context, and az account show confirms it. Using the ID is safer than a name, because names are not unique.

If you have access to several tenants, add --tenant with the tenant ID to az login. In scripts, set the subscription at the top and print it, so logs prove where changes went. You can also set defaults such as a resource group and location with az config set defaults.group=rg-lab-api defaults.location=westeurope, which shortens commands but makes it easy to forget where things go, so print them too.

FlowStart of every lab session
  1. Step 1
    az login

    Sign in, with --tenant if you have several

  2. Step 2
    az account list -o table

    See every subscription you can reach

  3. Step 3
    az account set

    Switch to the lab subscription by ID

  4. Step 4
    az account show

    Confirm the name and ID before creating anything

Tags that make cleanup and billing easy

Tags are name and value pairs, and a resource can carry up to 50 of them. For labs, three tags do most of the work: env=lab, owner=your name or alias, and expires=a date. Add a project or cost-centre tag if someone else pays.

Resources do not inherit tags from their resource group by default. You can tag each resource, or use Azure Policy with a modify effect to copy tags down, which a later topic covers. Tags show up in Cost analysis, so you can group spend by owner or project. They also make cleanup scripts possible: list groups where expires is in the past and delete them after a check. Keep tag names and casing consistent; Cost analysis groups different spellings separately.

  • env, owner and expires cover most labs
  • Resource tags are not inherited by default
  • Group costs by tag in Cost analysis
  • Agree on spelling and case up front

Budgets, quotas and what they do not do

A budget lives in Cost Management at a subscription or resource group scope. You set an amount, a reset period such as monthly, and alert conditions, for example 50, 80 and 100 percent of actual cost and 100 percent of forecast cost. Alerts go to email addresses and can trigger an action group. Cost data usually arrives several hours after usage and budgets are evaluated periodically, so alerts are late rather than instant. Set thresholds low enough to leave room to react.

A budget never stops resources. Some credit-based offers, such as the free account credit, have a spending limit that disables services when the credit runs out, but pay-as-you-go subscriptions do not. Quotas are different again: they limit how much you can create, such as vCPUs per region, and new subscriptions often have small quotas. az vm list-usage shows them.

  • Budget: alert on actual or forecast spend
  • Alerts lag usage by hours
  • No budget switches anything off
  • Quota: a cap on what you can create

Cleanup and checking for leftovers

az group delete --name rg-lab-api --yes removes the group and everything in it. Add --no-wait to return immediately; then check with az group exists or az group list. A delete fails if a lock is present, which is a feature: remove the lock deliberately first.

Some things live outside your lab group. Role assignments at subscription scope stay behind. Enabling some network features creates a NetworkWatcherRG group. Diagnostic settings may send logs into a shared Log Analytics workspace. Soft-deleted Key Vaults keep their names reserved until purged or the retention period ends. A final az resource list filtered by your tag, plus a glance at Cost analysis the next day, confirms the lab has stopped costing money.

Learn it in 5 short steps

About 30m each. Track them with a timer in your plan.

  1. 1
    Use the Portal and Cloud Shell~30m
  2. 2
    Set the CLI account and subscription~30m
  3. 3
    Tag resources consistently~30m
  4. 4
    Create a budget with alerts~30m
  5. 5
    Delete the lab resource group~30m

Key points

  • Always run az account show before creating anything, so you know which subscription you are about to change.
  • Tags such as env, owner and expires let you find and bill lab resources later; resources do not inherit tags from their group by default.
  • A budget only alerts you; it does not stop resources or cap spend.
  • Cost data can lag by several hours, so set alert thresholds below the amount you actually care about.
  • Deleting the resource group is the cleanup step; check for leftovers outside it, such as role assignments or a Network Watcher group.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
az login
az account list -o table
az account set --subscription "<subscription-id>"
az account show --query "{name:name, id:id}" -o table

az group create --name rg-lab-api --location westeurope \
  --tags env=lab owner=alex expires=2026-12-31

# ...build things in rg-lab-api...

az resource list --resource-group rg-lab-api -o table
az group delete --name rg-lab-api --yes --no-wait

Set the context, create one tagged group, then delete it when done.

Creating a tagged lab and a budget with alerts

Prerequisites: a lab subscription where you have Contributor, and access to Cost Management. Step 1: set the CLI context and create rg-lab-api with tags. Step 2: create a monthly budget on the subscription. In the portal, open Cost Management, Budgets, Add, set 20 in your billing currency, then add alerts at 50 and 80 percent actual and 100 percent forecasted with your email. The Bicep below does the same and can be deployed with az deployment sub create. Evidence: the group appears with tags in az group show, and the budget shows in the Budgets list. Rollback: delete the budget and the group.

bicep
targetScope = 'subscription'

resource labBudget 'Microsoft.Consumption/budgets@2023-05-01' = {
  name: 'lab-monthly'
  properties: {
    category: 'Cost'
    amount: 20
    timeGrain: 'Monthly'
    timePeriod: { startDate: '2026-12-01' }
    notifications: {
      actual80: { enabled: true, operator: 'GreaterThan', threshold: 80, thresholdType: 'Actual', contactEmails: ['alex@example.com'] }
      forecast100: { enabled: true, operator: 'GreaterThan', threshold: 100, thresholdType: 'Forecasted', contactEmails: ['alex@example.com'] }
    }
  }
}

Finding and deleting expired labs

After a few weeks a learner has several lab groups. They list every group tagged env=lab with its expiry tag, decide which have expired, and delete them. The script prints first and deletes second, so a wrong filter cannot silently remove the wrong things. Evidence: az group list no longer shows the deleted names, and the next day's Cost analysis shows those resources stopped accruing cost.

bash
az account show --query name -o tsv

az group list --tag env=lab \
  --query "[].{name:name, expires:tags.expires}" -o table

TODAY=$(date +%F)
az group list --tag env=lab --query "[].[name, tags.expires]" -o tsv |
while read -r RG EXP; do
  if [[ -n "$EXP" && "$EXP" < "$TODAY" ]]; then
    echo "Deleting $RG (expired $EXP)"
    az group delete --name "$RG" --yes --no-wait
  fi
done

Quick reference

  • az login, then az account set --subscription ID, then az account show. Every session.
  • az group create -n rg-lab-api -l westeurope --tags env=lab owner=alex expires=2026-12-31
  • az resource list -g rg-lab-api -o table lists everything in the lab group.
  • az group delete -n rg-lab-api --yes --no-wait removes the group and its contents.
  • Budgets alert; they never stop resources. Alerts can lag usage by hours.
  • az vm list-usage -l westeurope -o table shows vCPU quota used and available.
  • Resources do not inherit resource group tags unless a policy copies them.

Common mistakes

  • Running commands while signed in to a work subscription instead of the lab one.
  • Relying on a budget as a hard cap; it is only an alert.
  • Creating lab resources in several resource groups, so cleanup misses some and they keep billing.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1Your budget reaches 100 percent. What happens to running resources?

Question 2

2Which command confirms which subscription the CLI will change?

Question 3

3You tag rg-lab-api with owner=alex. Does a storage account created inside it get the tag?

Question 4

4A VM create fails with a quota error on a new subscription. What is the likely cause?

Question 5

5Why put all of a lab's resources in one resource group?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Open Cloud Shell (or a local CLI), list your subscriptions, set the lab one and print its name and ID. Nothing is created, so there is no cost.

  2. Exercise 2medium

    Create rg-lab-api with env, owner and expires tags, create a free-tier-friendly storage account in it with the same tags, list both, then delete the group and prove it is gone. Expected cost: a few cents at most.

  3. Exercise 3hard

    Create a monthly budget on your lab resource group (not the subscription) with alerts at 50 and 90 percent actual cost, deploy it as code, and show it exists. If you cannot use Cost Management (for example on some sponsored offers), write the Bicep and explain the expected alert behaviour instead.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Can I use Azure without a credit card?
Some offers, such as Azure for Students, do not need one, and Microsoft Learn sandboxes give short-lived free environments for specific modules. The standard free account asks for a card to verify identity.
Is Cloud Shell free?
The shell itself is free. If you attach a storage account to keep files between sessions, you pay a small amount for that storage.
How quickly does a budget alert arrive?
Not instantly. Cost data usually appears hours after usage and budgets are checked periodically, so set thresholds with room to react.
I deleted the resource group but still see charges. Why?
Charges for usage before the delete can appear a day later. If cost continues, look for resources in other groups, such as shared log workspaces or a NetworkWatcherRG group.

Go deeper

ShareXLinkedInWhatsApp