Taking control of your Azure resources – Part 3 – Tags

It’s been a busy few months since I started this series, but here is the next part finally! It’s about Tags, and how to use them for billing etc.

Other parts:

What is Tags?

Tags let’s you organize resources beyond what Resource Groups do, as you can search for resources with a tag, and results will not be limited to a Resource Group which is typically a deployment boundary.

There is a lot of ways to use Tags, but I primarily think of it as tool to help separate and identify cost of a single resource or group of resources. Up until ARM we didn’t have a way to separate resources that way, so figuring out your bill could be quite a challenge. Now I can tag a group of resources, for example “website” and easily get the cost of those resources on my bill, and see what the website costs to run.
There are some other things I usually recommend tagging:

  • Environment – dev/test/prod etc
  • Role – web/db/app etc
  • Department – finance/legal/IT etc
  • Responsible party – could be a person or team, or maybe a partner company

Tags consist of a key/value pair, where key is the name of the tag, and value is.. well, your value. Using the environment example from above, Environment would be the name, and AzureDemo could be the value.

A question I get often is “Can I enforce a specific tag to be supplied on resources?” – Yes! Go back to part 2 and look at my examples there.

Beneath I will run through how to use Tags in the portal and with PowerShell. I might add a post about ARM templates, but for now it’s out of the scope. Lets start with the portal.

 

Portal

In the below screenshot I have a resource group where I’ve already added one tag (environment : demo) and is now adding another one, ‘owner’ with a value of ‘jj’. Remember to hit Save before you close the blade.

portaltag01

When we’ve added tags to resources, we can also browse our tags. Click browse on the menu to the left and scroll down to Tags:

portaltag02

There you will see all the Tags for the selected subscriptions, and you can then see which resources belongs to each Tag:

portaltag03

portaltag04

 

Powershell

Notice that the following commands requires at least Azure PowerShell v1.0. If you are not already running this version, you should look into it asap as a lot has changed. More info here: https://azure.microsoft.com/blog/azps-1-0/

Now, I would like to add a ‘owner’ tag to vm01 from the above example. To do this I must find my resource by using Get-AzureRmResource – and not Get-AzureRmVM which would be the logical choice for many people.

[powershell]Get-AzureRmResource -ResourceGroupName demo-rg -ResourceName vm01[/powershell]

pstag01

So, I’ve done a poor job naming my resources, as you can see. Let’s filter a bit more by the resource type:

[powershell]Get-AzureRmResource -ResourceGroupName demo-rg -ResourceName vm01 -ResourceType "Microsoft.Compute/virtualMachines"[/powershell]

pstag02

That’s better! Throw that in to a variable, in my case $vm so we can work with it. In the above screenshot you can see Tags contains a hashtable. Let’s take a look at that:

[powershell]$tags = $vm.tags
$tags[/powershell]

pstag03

Great, that’s what we saw in the portal too. Let’s add another one to the hashtable:

[powershell]$tags += @{Name="owner";value="jj"}
$tags
$tags[1]
$tags[0][/powershell]

pstag04

As you can see it now contains two tags.  To add these to your resource, use Set-AzureRmResource:

[powershell]Set-AzureRmResource -ResourceGroupName demo-rg -ResourceName vm01 -ResourceType "Microsoft.Compute/virtualMachines" -Tag $tags[/powershell]

pstag05

And to check it:

[powershell](Get-AzureRmResource -ResourceGroupName demo-rg -ResourceName vm01 -ResourceType "Microsoft.Compute/virtualMachines").Tags[/powershell]

pstag06

If we go to the portal, we can see it there too:

pstag07

A big difference between using PowerShell and the portal, is that for a lot of resource (if not all) you can not add tags when you create them through the portal. PowerShell adds an extra feature there, and let’s you do that. For example when creating a new VM, you can use –Tags parameter on New-AzureRmVM. If you have a policy defined that requires Tags, be aware of this.

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s