Apply Tags in Azure and why you should tag all Azure resources

  • Post author:
  • Post category:Main
  • Post last modified:September 10, 2023
  • Reading time:5 mins read

Tags in Azure are name-value pairs used to logically organise your Azure resource. They can be applied to individual resources or resource groups to help with automation, organisation and cost management. 

Tagging your Azure resources is a vital part of your cloud deployment and are often used as the basis for applying business policies with Azure policy and tracking departmental costs with cost management.

It is important you know the how’s and why’s of using tags in Azure and following best practices when it comes to tag governance. 

Why is using tags in Azure important?

Using tags to organise your Azure resources is a critical part of your cloud deployments. Even with small Azure environments, poor organisation and a lack of tagging could lead to unexpected costs or downtime. Some of the benefits of using tags include:

  • Cost management: Ensuring the different groups or departments in a business are easily able to visualise the cost of their cloud environment, calculate return on investment, create budgets and alerts, and create cost reporting.
  • Security: By tagging all of your resources, it will allow you to easily apply and enforce custom security policies across your environment.
  • Optimisation: If each resource in a specific workload is tagged it will help you identify if any optimisation within that workload can be done, saving time and reducing costs.
  • Resource Management: Tagging all resources within a workload, or enforcing the use of tags will help you quickly identify all the resources that make up a particular workload, for example a virtual machine or set of virtual machines.

What resource types can tags be applied to?

Many services across different Microsoft namespaces support tags. Microsoft have a full listing on the supported resource types.

Here: https://docs.microsoft.com/en-us/azure/azure-resource-manager/management/tag-support 

How do you apply tags to Azure resources?

You can add, modify and delete tags on Azure resources through the Azure Portal, Powershell, Azure CLI, Resource Manager Templates and the Rest API. 

Apply Tags in the Azure Portal

When creating your resources through the resource creation wizard in the Azure Portal, you will be presented with the option to apply tags.

Apply Tags to Virtual Machine

Apply Tags with Powershell

Below we are creating a new virtual machine with the name supervm01. Pay attention to the -tag parameter, you can see at the start of the code, we are declaring the $vmtags variable, then using that at the end. You can see we are applying two 2 tags (name-value pairs).

$vmtags = @{Department = "IT"; Billing = "IT"}

New-AzVm `
    -ResourceGroupName 'vm1ResourceGroup' `
    -Name 'Supervm1' `
    -Location 'East US' `
    -VirtualNetworkName 'myVnet' `
    -SubnetName 'mySubnet' `
    -SecurityGroupName 'myNetworkSecurityGroup' `
    -PublicIpAddressName 'myPublicIpAddress' `
    -OpenPorts 80,3389
    -tag $vmtags

The tag parameter is in the format of a hash table. In order to use this in our script, we first need to declare the hash table, above we are using $vmtags. Then we can call upon it when needed in our script.

Apply Tags with Azure CLI

Using Azure CLI is similar to the above, with a slightly different syntax. You can see from the below, when assigning a tag on creation of our resource, we are specifying all the tags within the final command and not calling upon a variable.

##First we must create a resource group
az group create --name myResourceGroup --location eastus

#Lets create our VM
az vm create \
    --resource-group myResourceGroup \
    --name myVM \
    --image Win2019Datacenter \
    --public-ip-sku Standard \
    --admin-username azureuser 
    --Tags 'mytag1=mytagvalue' 'mytag2=mytagevalue'

Summary

We hope this post has helped you understand; what Azure tags are? what is the purpose of Azure Tags? and how to apply Azure tags to resources using the Azure Portal, Azure PowerShell or the Azure CLI.

Are you looking to become certified in Azure? check out our Complete Azure Training and Online Guide or review the many posts we have on the different Azure Exams.

Daniel Bradley

My name is Daniel Bradley and I work with Microsoft 365 and Azure as an Engineer and Consultant. I enjoy writing technical content for you and engaging with the community. All opinions are my own.

Leave a Reply