Skip to content

Latest commit

 

History

History
191 lines (117 loc) · 9.51 KB

virtual-machines-tagging-arm.md

File metadata and controls

191 lines (117 loc) · 9.51 KB

How to Tag a Virtual Machine in Azure

[AZURE.INCLUDE learn-about-deployment-models] classic deployment model.

This article describes different ways to tag a virtual machine in Azure through the Azure Resource Manager. Tags are user-defined Key/Value pairs which can be placed directly on a resource or a resource group. Azure currently supports up to 15 tags per resource and resource group. Tags may be placed on a resource at the time of creation or added to an existing resource. Please note, tags are supported for resources created via the Azure Resource Manager only.

Tagging a Virtual Machine through Templates

First, let’s look at tagging through templates. This template places tags on the following resources: Compute (Virtual Machine), Storage (Storage Account), and Network (Public IP Address, Virtual Network, and Network Interface).

Click the Deploy to Azure button from the template link. This will navigate to the Azure portal where you can deploy this template.

Simple deployment with Tags

This template includes the following tags: Department, Application, and Created By. You can add/edit these tags directly in the template if you would like different tag names.

Azure tags in a template

As you can see, the tags are defined as Key/Value pairs, separated by a colon (:). The tags must be defined in this format:

    “tags”: {
        “Key1” : ”Value1”,
        “Key2” : “Value2”
    }

Save the template file after you finish editing it with the tags of your choice.

Next, in the Edit Parameters section, you can fill out the values for your tags.

Edit Tags in Azure portal

Click Create to deploy this template with your tag values.

Tagging through the Portal

After creating your resources with tags, you can view, add, and delete tags in the portal.

Select the tags icon to view your tags:

Tags icon in Azure portal

Add a new tag through the portal by defining your own Key/Value pair, and save it.

Add new Tag in Azure portal

Your new tag should now appear in the list of tags for your resource.

New Tag saved in Azure portal

Tagging with PowerShell

To create, add, and delete tags through PowerShell, you first need to set up your PowerShell environment with Azure Resource Manager. Once you have completed the setup, you can place tags on Compute, Network, and Storage resources at creation or after the resource is created via PowerShell. This article will concentrate on viewing/editing tags placed on Virtual Machines.

First, navigate to a Virtual Machine through the Get-AzureVM cmdlet.

    PS C:\> Get-AzureVM -ResourceGroupName "MyResourceGroup" -Name "MyWindowsVM"

If your Virtual Machine already contains tags, you will then see all the tags on your resource:

    Tags : {
            "Application": "MyApp1",
            "Created By": "MyName",
            "Department": "MyDepartment",
            "Environment": "Production"
           }

If you would like to add tags through PowerShell, you can use the Set-AzureResource command. Note when updating tags through PowerShell, tags are updated as a whole. So if you are adding one tag to a resource that already has tags, you will need to include all the tags that you want to be placed on the resource. Below is an example of how to add additional tags to a resource through PowerShell Cmdlets.

This first cmdlet sets all of the tags placed on MyWindowsVM to the tags variable, using the Get-AzureResource and Tags function. Note that the parameter ApiVersion is optional; if not specified, the latest API version of the resource provider is used.

    PS C:\> $tags = (Get-AzureResource -Name MyWindowsVM -ResourceGroupName MyResourceGroup -ResourceType "Microsoft.Compute/virtualmachines" -ApiVersion 2015-05-01-preview).Tags

The second command displays the tags for the given variable.

    PS C:\> $tags

    Name		Value
    ----                           -----
    Value		MyDepartment
    Name		Department
    Value		MyApp1
    Name		Application
    Value		MyName
    Name		Created By
    Value		Production
    Name		Environment

The third command adds an additional tag to the tags variable. Note the use of the += to append the new Key/Value pair to the tags list.

    PS C:\> $tags +=@{Name="Location";Value="MyLocation"}

The fourth command sets all of the tags defined in the tags variable to the given resource. In this case, it is MyWindowsVM.

    PS C:\> Set-AzureResource -Name MyWindowsVM -ResourceGroupName MyResourceGroup -ResourceType "Microsoft.Compute/VirtualMachines" -ApiVersion 2015-05-01-preview -Tag $tags

The fifth command displays all of the tags on the resource. As you can see, Location is now defined as a tag with MyLocation as the value.

    PS C:\> (Get-AzureResource -ResourceName "MyWindowsVM" -ResourceGroupName "MyResourceGroup" -ResourceType "Microsoft.Compute/VirtualMachines" -ApiVersion 2015-05-01-preview).Tags

    Name		Value
    ----                           -----
    Value		MyDepartment
    Name		Department
    Value		MyApp1
    Name		Application
    Value		MyName
    Name		Created By
    Value		Production
    Name		Environment
    Value		MyLocation
    Name		Location

To learn more about tagging through PowerShell, check out the Azure Resource Cmdlets.

Tagging with Azure CLI

Tagging is also supported for resources that are already created through the Azure CLI. To begin, set up your Azure CLI environment. Log in to your subscription through the Azure CLI and switch to ARM mode. You can view all properties for a given Virtual Machine, including the tags, using this command:

    azure vm show -g MyResourceGroup -n MyVM

Unlike PowerShell, if you are adding tags to a resource that already contains tags, you do not need to specify all tags (old and new) before using the azure vm set command. Instead, this command allows you to append a tag to your resource. To add a new VM tag through the Azure CLI, you can use the azure vm set command along with the tag parameter -t:

    azure vm set -g MyResourceGroup -n MyVM –t myNewTagName1=myNewTagValue1;myNewTagName2=myNewTagValue2

To remove all tags, you can use the –T parameter in the azure vm set command.

    azure vm set – g MyResourceGroup –n MyVM -T

Now that we have applied tags to our resources via PowerShell, Azure CLI, and the Portal, let’s take a look at the usage details to see the tags in the billing portal.

Viewing your tags in the usage details

Tags placed on Compute, Network, and Storage resources through the Azure Resource Manager will be populated in your usage details in the billing portal.

Click on Download usage details to view the usage details in your subscription.

Usage details in Azure portal

Select your billing statement and the Version 2 usage details:

Version 2 Preview Usage Details in Azure portal

From the usage details, you can see all of the tags in the Tags column:

Tags column in Azure portal

By analyzing these tags along with usage, organizations will be able to gain new insights into their consumption data.

Additional resources