Exercise - Tag, query, and clean up resources
This guided project consists of the following exercises:
- Open Cloud Shell and explore the environment
- Create and list resources with CLI commands
- Tag, query, and clean up resources
In this exercise, you apply tags from the CLI, use JMESPath queries to filter and shape command output, compare your CLI work to the portal view, and then clean up by deleting the resource group. This completes the full resource lifecycle from the command line.
This exercise includes the following tasks:
- Tag the resource group
- Tag individual resources
- Query resources with JMESPath filters
- Compare CLI output to the portal
- Delete the resource group from the CLI
Outcome: Resources tagged from the CLI, queried with filters, and the resource group deleted from the command line.
Task 1: Tag the resource group
Apply tags to the resource group using a single command. Tags assigned from the CLI follow the same key-value format you use in the portal but are faster to apply across multiple resources.
Run the following command to add tags to the resource group:
az group update \ --name rg-gp-cli-demo \ --tags environment=test department=it-opsConfirm the JSON output includes the tags section with both key-value pairs.
Run the following command to verify the tags:
az group show --name rg-gp-cli-demo --query tagsConfirm the output shows environment: test and department: it-ops.
Note
Validation step: Confirm tags are applied to the resource group from the CLI.
Task 2: Tag individual resources
Apply tags to each storage account. In a real environment, consistent tagging across resources enables cost tracking and automated governance.
Run the following command to tag the first storage account (use your actual storage account name):
az resource tag \ --tags environment=test department=development \ --ids $(az storage account show --name stgpclidemo01 --resource-group rg-gp-cli-demo --query id --output tsv)Run the following command to tag the second storage account with different values:
az resource tag \ --tags environment=test department=operations \ --ids $(az storage account show --name stgpclidemo02 --resource-group rg-gp-cli-demo --query id --output tsv)Confirm both commands return JSON output with the expected tags.
Note
Validation step: Confirm both storage accounts are tagged with different department values.
Task 3: Query resources with JMESPath filters
Use the --query parameter to extract specific fields from CLI output. JMESPath is a built-in query language that lets you filter and reshape JSON results without piping to external tools.
Run the following command to list resource names and their tags:
az resource list \ --resource-group rg-gp-cli-demo \ --query "[].{Name:name, Department:tags.department, Environment:tags.environment}" \ --output tableConfirm the table shows each resource with its Department and Environment tag values.
Note
Validation step: Confirm the JMESPath query extracted and formatted tag values from CLI output.
Run the following command to list only resources tagged with department=development:
az resource list \ --resource-group rg-gp-cli-demo \ --query "[?tags.department=='development'].{Name:name, Type:type}" \ --output tableConfirm only the first storage account appears in the filtered output.
Note
Validation step: Confirm the JMESPath filter shows only resources where department equals development.
Run the following command to count the resources in the resource group:
az resource list \ --resource-group rg-gp-cli-demo \ --query "length([])"Confirm the output shows 2.
Note
Validation step: Confirm the resource count matches the expected number of resources.
Task 4: Compare CLI output to the portal
Open the portal alongside Cloud Shell to confirm the CLI and portal show the same data. This reinforces that both tools manage the same underlying resources.
- In the Azure portal (above the Cloud Shell pane), search for Resource groups in the portal search bar and select Resource groups.
- Select rg-gp-cli-demo from the list.
- Confirm the portal shows the same two storage accounts.
- In the left menu, select Tags.
- Confirm the environment and department tags match what you set from the CLI.
- Return to the rg-gp-cli-demo resource group overview.
- Select one of the storage accounts.
- In the left menu, select Tags.
- Confirm the resource-level tags match what you set from the CLI.
Note
Validation step: Confirm the portal and CLI display identical resource tags and metadata.
Task 5: Delete the resource group from the CLI
Clean up all resources with a single command. Deleting a resource group removes everything inside it, which is why organizing resources into groups is a best practice.
Run the following command to delete the resource group and all resources inside it:
az group delete --name rg-gp-cli-demo --yes --no-waitThe --yes flag skips the confirmation prompt. The --no-wait flag returns control immediately while deletion continues in the background.
Run the following command to check the deletion status:
az group show --name rg-gp-cli-demo --output tableIf the group still exists, the output shows it. If it's been deleted, you receive a "not found" error, which confirms successful removal.
Wait a minute and run the command again to confirm the resource group is fully deleted.
Note
Validation step: Confirm the resource group deletion is complete ("not found" error confirms removal).