Share via


Adding tags to resources

Adding tags to resources helps us to organize our resources and to automate some tasks. Here is a small intro on how to use tags in the Azure portal, in templates, or with PowerShell. In this article we will use tags only for virtual machines, but you can add tags to any resource created with the Azure Resource Manager (including resourcegroups).

Tags are simple name-value pairs that we can define the way we like it. An (often used) example is the tagging of a resource with the appropriate cost center (like "Department" = "Sales"), or to separate development from production ("Environment" = "Test"), or whatever else we have in mind to organize.

Portal

If you look at the resource in the portal, there is a "Tags" entry. Click on it (1), enter a name-value pair (2) and hit "Save" (3). That's it. You can choose from previously used tags by selecting from the dropdown menu.

[caption id="attachment_686" align="alignnone" width="1024"]using tags Using tags in the Azure portal[/caption]

Here we use the name "purpose" and the value "blog-example" to make it easier to delete resources that we needed for screenshots... :-)

We can add more tags to a resource, up to 15 per resource. And there is a main "Tags" menu (1) that we can use to search across all our resources for special tags (2):

Templates

When using templates to deploy resources (which is a very good idea by the way...) simply add them (as a list) into the template:

[code gutter="false"]
...
"resources": [
{
"apiVersion": "2016-01-01",
"type": "Microsoft.Storage/storageAccounts",
"name": "[concat('storage', uniqueString(resourceGroup().id))]",
"location": "[resourceGroup().location]",
"tags": {
"Dept": "Finance",
"Environment": "Production"
},
...

Here we set two tags, "Dept" and "Environment", at a storage resource. This is really simple... and of course those resources and tags can be found via the portal also. If you use parameters for the tags, you will be - as usual - prompted before the deployment to enter values for those tags.

[code gutter="false"]
...
"resources": [
{
"apiVersion": "2016-01-01",
"type": "Microsoft.Storage/storageAccounts",
"name": "[concat('storage', uniqueString(resourceGroup().id))]",
"location": "[resourceGroup().location]",
"tags": {
"Dept": "[parameters('DepartmentTag')]",
"Environment": "[parameters('EnvironmentTag')]"
},
...

Note that it is not possible to set tags on resourcegroups with a template, only to resources. If you want to add tags to resourcegroups, you must do this via the portal or PowerShell.

PowerShell

Tags are a property of a resource or a resourcegroup object. To list them, simply enter Get-AzureRmResourceGroup and you might get something like this:
[code gutter="false"]
C:> Get-AzureRmResourceGroup -Name test01-rg

ResourceGroupName : test01-rg
Location : germanycentral
ProvisioningState : Succeeded
Tags :
Name Value
======= ===============
purpose unknown
status unknown/unknown

ResourceId : /subscriptions/06(...)/resourceGroups/test01-rg

Similar for a resource inside a resourcegroup:

[code gutter="false"]
C:> Get-AzureRmResource -ResourceGroupName test01-rg -ResourceName rw01-vm1

Name : rw01-vm1
ResourceId : /subscriptions/06(...)/virtualMachines/rw01-vm1
ResourceName : rw01-vm1
ResourceType : Microsoft.Compute/virtualMachines
ResourceGroupName : test01-rg
Location : germanycentral
SubscriptionId : 06d(...)
Tags : {purpose}

Tags are dictionary objects, so we can work with them once we saved the object:

[code gutter="false"]
C:> $tags = (Get-AzureRmResource -ResourceGroupName test01-rg -ResourceName rw01-vm1).Tags
C:> $tags

Name Value
---- -----
purpose VM

We can add name/value pairs:
[code gutter="false"]
C:> $tags.Add("status","active")
C:> $tags

Name Value
---- -----
purpose VM
status active

We can modify them (like adding some text):

[code gutter="false"]
C:> $tags.Item("status") += " (do not delete)"
C:> $tags

Name Value
---- -----
purpose VM
status active (do not delete)

or we can delete pairs:

[code gutter="false"]
C:> $tags.Remove("status")

Once we are finished with modifying the dictionary object, we can write it back by using the "Set- instead of the "Get-" cmdlet:
[code gutter="false"]
C:> Set-AzureRmResource -ResourceGroupName test01-rg -ResourceName rw01-vm1 -Tags $tags

C:> Set-AzureRmResourceGroup -Name test01-rg -Tags $tags

Summary

Tags are a great way to organize resources just the way you like it. And if you think further, it is easy to assign tasks to all resources with a special value for a key, like garbage collection for all resources with "Status" = "delete me".

To be consequent, use Azure policies to make sure that all your resources or resourcegroups at least have a tag.