Apply Tags to organize your Azure Resources

  1. Home
  2. Apply Tags to organize your Azure Resources

Return to AZ-104 Tutorial

We can apply tags to our Azure resources, resource groups, and subscriptions and logically organize them into a taxonomy. Each tag consists of a name and a value pair. Note, tag names are case-insensitive for operations. Any tag with a tag name, irrespective of the casing, is updated or retrieved. But, the resource provider may keep the casing you provide for the tag name.

Required access to Apply Tag

In order to apply tags to a resource, we must have write access to the Microsoft.Resources/tags resource type. In which case the Tag Contributor role assists in applying tags to an entity without having access to the entity itself. At present, the tag contributor role cannot apply tags to resources or resource groups through the portal. It can apply tags to subscriptions through the portal. Also, it supports all tag operations through PowerShell and REST API.

Note – Contributor role allows the required access to apply tags to any entity. In order to apply tags to only one resource type, we suggest to use the contributor role for that resource.

Steps to Apply Tags using PowerShell

The Azure PowerShell has two commands for applying tags namely – New-AzTag and Update-AzTag. In which case you must have the Az.Resources module 1.12.0 or later. We can check the version with Get-Module Az.Resources.

New-AzTag replaces

The New-AzTag is used to replace all tags on the resource, resource group, or subscription. When calling the command, pass in the resource ID of the entity that we wish to tag.

Example –

$tags = @{“Dept”=”Finance”; “Status”=”Normal”}
$resource = Get-AzResource -Name demoStorage -ResourceGroup demoGroup
New-AzTag -ResourceId $resource.id -Tag $tags

Output

Properties :
Name Value
====== =======
Dept Finance
Status Normal

Update-AzTag

We use Update-AzTag to add tags to a resource that already has tags. For this we set the -Operation parameter to Merge.

Example –

$tags = @{“Dept”=”Finance”; “Status”=”Normal”}
Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Merge

Output

Properties :
Name Value
=========== ==========
Status Normal
Dept Finance
Team Compliance
Environment Production

Practice Test for AZ-104
Types of Tags

We will now use example to illustrate the uses of various tags

Replace Tag

Set the -Operation parameter to Replace, the existing tags are replaced by the new set of tags.

$tags = @{“Project”=”ECommerce”; “CostCenter”=”00123”; “Team”=”Web”}
Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Replace

Add a New Tag to a resource group

$tags = @{“Dept”=”Finance”; “Status”=”Normal”}
$resourceGroup = Get-AzResourceGroup -Name demoGroup
New-AzTag -ResourceId $resourceGroup.ResourceId -tag $tags

Update Tags for a resource group

$tags = @{“CostCenter”=”00123”; “Environment”=”Production”}
$resourceGroup = Get-AzResourceGroup -Name demoGroup
Update-AzTag -ResourceId $resourceGroup.ResourceId -Tag $tags -Operation Merge

Add a new set of tags to a subscription

$tags = @{“CostCenter”=”00123”; “Environment”=”Dev”}
$subscription = (Get-AzSubscription -SubscriptionName “Example Subscription”).Id
New-AzTag -ResourceId “/subscriptions/$subscription” -Tag $tags

Update the tags for a subscription

$tags = @{“Team”=”Web Apps”}
$subscription = (Get-AzSubscription -SubscriptionName “Example Subscription”).Id
Update-AzTag -ResourceId “/subscriptions/$subscription” -Tag $tags -Operation Merge

List tags

In order to get the tags for a resource, resource group, or subscription, use the Get-AzTag command and pass in the resource ID for the entity.

Remove tags

In order to remove specific tags, use Update-AzTag and set -Operation to Delete. Pass in the tags you want to delete.

$removeTags = @{“Project”=”ECommerce”; “Team”=”Web”}
Update-AzTag -ResourceId $resource.id -Tag $removeTags -Operation Delete

Steps to Apply Tags using Azure CLI

When adding tags to a resource group or resource, you can either overwrite the existing tags or append new tags to existing tags.

Overwrite the tags on a resource

az resource tag –tags ‘Dept=IT’ ‘Environment=Test’ -g examplegroup -n examplevnet –resource-type “Microsoft.Network/virtualNetworks”

Append a tag to the existing tags on a resource

az resource update –set tags.’Status’=’Approved’ -g examplegroup -n examplevnet –resource-type “Microsoft.Network/virtualNetworks”

Overwrite the existing tags on a resource group

az group update -n examplegroup –tags ‘Environment=Test’ ‘Dept=IT’

Append a tag to the existing tags on a resource group

az group update -n examplegroup –set tags.’Status’=’Approved’

At present, Azure CLI doesn’t have a command for applying tags to subscriptions. But, we can use CLI to deploy an ARM template that applies the tags to a subscription.

Steps to Apply Tags using Portal

In case a user does not have the required access for applying tags, we can assign the Tag Contributor role to the user.

Applying Tags for resource or resource group
  • To view the tags for a resource or a resource group, look for existing tags in the overview. If you have not previously applied tags, the list is empty.
  • To add a tag, select Click here to add tags.
  • Provide a name and value.
  • Continue adding tags as needed. When done, select Save.
  • The tags are now displayed in the overview.
  • To add or delete a tag, select change.
  • To delete a tag, select the trash icon. Then, select Save.

Tags Bulk Assignment to Multiple Resources

  • From any list of resources, select the checkbox for the resources you want to assign the tag. Then, select Assign tags.
  • Add names and values. When done, select Save.

Reference: Microsoft Documentation

Return to AZ-104 Tutorial

Menu