Using Tags in Azure
How and why we use Azure Tags, why they are so useful
Tags are a really simple feature in Azure, and are amazingly useful. If you’ve spent any time in the portal then you will probably have noticed them. They are essentially a set of key value pairs that are assigned to a resource to ‘tag’ them in some way. In this post we are going to look at how to use tags and how they can be applied.
These tags are free form text, so you can add just about anything you like (within limits). If a tag doesn’t already exist, you can just type away and save the new tag.
Searching and Filtering using Tags
One of the simplest ways to use Azure Tags is for searching. Much like you use Resource Groups to group things together, but tags can span many groups or environments etc. To demonstrate this, give something a tag and go to “All Resources” in the Portal. Click on the “Edit columns” button at the top of the resources and add the ‘Tags’ column.
In this example we can use the tags to see all the resources associated with my blog. This could be the Azure SQL DB and the App Service etc. It doesn’t have to be tied to the same Resource Group or object either.
Cost Management using Tags
Tags come in handy when we are looking at the bill too. Once you have tagged your resources, go to “View my Bill” and click on “Cost Analysis”. This will show you the total cost and some nice doughnut charts for Resource Group costs etc. On the top of the page there is a “Group by” filter where you can select Tags.
This information doesn’t appear straight away. It can take a few days to filter through, but once the tag is setup, you don’t have to touch it until you add more resources.
Side Note - I hadn’t realised how cheap my blog was. Feel free to link to it to make it more expensive ;)
Tagging with PowerShell
Of course we can tag things with PowerShell!
To find the tags that are assigned to resources is pretty simple using ‘Get-AzResource’, so I’m not going to go over that but these are some examples to give you an idea….
# Get a list of resources and the tags assigned.
Get-AzResource | Select-Object ResourceName, Tags
# List resources with a specific tag
Get-AzResource -Tag @{Blog="Dev"}
# List resources with NO tag assigned
Get-AzResource -Tag @{}
# Get all resoures with a specific tag name (with any value)
Get-AzResource -TagName Blog
This will add a tag to a resource. However it will over write an tags that are already applied.
Get-AzResource -Name dc01 | Set-AzResource -Tag @{Test="001"} -Force
If we want to add tags to the resource, and preserve the existing tags, then try…..
$resource = Get-AzResource -Name dc01
$resource.Tags.add("CostCode","ABC123")
Get-AzResource -Name DC01 | Set-AzResource -Tags $resource.Tags
Tag Limits
One thing to be aware of, is the limits on tags. You are limited by things like the number of tags per resource, or the length of tags. The limits are big, so it shouldn’t be a an issue in most environments but it is worth knowing about them.
- 50 = Max number of tags per resource
- 512 = Max characters for the tag name (128 for strage accounts)
- 256 = Max characters for the tag value (256 for storage accounts)
- Not all resources support tags! Most do though. See HERE for the Microsoft doc listing which ones do/don’t.
- Avoid special character. Stick to simple alphanumerics.
Summary
Tags are simple but really usefull. It’s worth tagging everything you create with a something. Even if you change this in the future, you will be able to identify and bill the items easier.
Share this post
Twitter
Facebook
Reddit
LinkedIn
Email