Azure for Architects
上QQ阅读APP看书,第一时间看更新

Azure tags

Azure allows the tagging of resource groups and resources with name-value pairs. Tagging helps in the logical organization and categorization of resources. Azure also allows the tagging of 50 name-value pairs for a resource group and its resources. Although a resource group acts as a container or a placeholder for resources, tagging a resource group does not mean the tagging of its constituent resources. Resource groups and resources should be tagged based on their usage, which will be explained later in this section. Tags are bound to a subscription, resource group, or resource. Azure accepts any name-value pair, and so it is important for an organization to define both the names and their possible values.

But why is tagging important? In other words, what problems can be solved using tagging? Tagging has the following benefits:

Categorization of resources: An Azure subscription can be used by multiple departments within an organization. It is important for the management team to identify the owners of any resources. Tagging helps in assigning identifiers to resources that can be used to represent departments or roles.

Information management for Azure resources: Again, Azure resources can be provisioned by anyone with access to the subscription. Organizations like to have a proper categorization of resources in place to comply with information management policies. Such policies can be based on application life cycle management, such as the management of development, testing, and production environments. They could also be based on usage or any other priorities. Each organization has its own way of defining information categories, and Azure caters for this with tags.

Cost management: Tagging in Azure can help in identifying resources based on their categorization. Queries can be executed against Azure to identify cost per category, for instance. For example, the cost of resources in Azure for the development of an environment for the finance department and the marketing department can be easily ascertained. Moreover, Azure also provides billing information based on tags. This helps in identifying the consumption rates of teams, departments, or groups.

Tags in Azure do have certain limitations, however:

Azure allows a maximum of 50 tag name-value pairs to be associated with resource groups. 

Tags are non-inheritable. Tags applied to a resource group do not apply to the individual resources within it. However, it is quite easy to forget to tag resources when provisioning them. Azure Policy provides the mechanism to use to ensure that tags are tagged with the appropriate value during provision time. We will consider the details of such policies later in this chapter.

Tags can be assigned to resources and resource groups using PowerShell, the Azure CLI 2.0, Azure Resource Manager templates, the Azure portal, and the Azure Resource Manager REST APIs.

An example of information management categorization using Azure tags is shown here:

Information management categorization using Azure tags
Figure 5.2: Information management categorization using Azure tags

In this example, the Department, Project, Environment, Owner, Approver, Maintainer, Start Date, Retire Date, and Patched Date name-value pairs are used to tag resources. It is extremely easy to find all the resources for a particular tag or a combination of tags using PowerShell, the Azure CLI, or REST APIs. The next section will discuss ways to use PowerShell to assign tags to resources.

Tags with PowerShell

Tags can be managed using PowerShell, Azure Resource Manager templates, the Azure portal, and REST APIs. In this section, PowerShell will be used to create and apply tags. PowerShell provides a cmdlet for retrieving and attaching tags to resource groups and resources:

To retrieve tags associated with a resource using PowerShell, the Get-AzResource cmdlet can be used:

(Get-AzResource -Tag @{ "Environment"="Production"}).Name

To retrieve tags associated with a resource group using PowerShell, the following command can be used:

Get-AzResourceGroup -Tag @{"Environment"="Production"}

To set tags to a resource group, the Update-AzTag cmdlet can be used:

$tags = @{"Dept"="IT"; "Environment"="Production"}

$resourceGroup = Get-AzResourceGroup -Name demoGroup

New-AzTag -ResourceId $resourceGroup.ResourceId -Tag $tags

To set tags to a resource, the same Update-AzTag cmdlet can be used:

$tags = @{"Dept"="Finance"; "Status"="Normal"}

$resource = Get-AzResource -Name demoStorage -ResourceGroup demoGroup

New-AzTag -ResourceId $resource.id -Tag $tags

You can update existing tags using the Update-AzTag command; however, you need to specify the operation as Merge or Replace. Merge will append the new tags you are passing into the existing tags; however, the Replace operation will replace all the old tags with the new ones. Here is one example of updating the tags in a resource group without replacing the existing ones:

$tags = @{"Dept"="IT"; "Environment"="Production"}

$resourceGroup = Get-AzResourceGroup -Name demoGroup

Update-AzTag -ResourceId $resourceGroup.ResourceId -Tag $tags -Operation Merge

Let's now look at tags with Azure Resource Manager templates.

Tags with Azure Resource Manager templates

Azure Resource Manager templates also help in defining tags for each resource. They can be used to assign multiple tags to each resource, as follows:

{

    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",

    "contentVersion": "1.0.0.0",

    "resources": [

    {

      "apiVersion": "2019-06-01",

      "type": "Microsoft.Storage/storageAccounts",

      "name": "[concat('storage', uniqueString(resourceGroup().id))]",

      "location": "[resourceGroup().location]",

      "tags": {

        "Dept": "Finance",

        "Environment": "Production"

      },

      "sku": {

        "name": "Standard_LRS"

      },

      "kind": "Storage",

      "properties": { }

    }

    ]

}

In the previous example, a couple of tags, Dept and Environment, were added to a storage account resource using Azure Resource Manager templates.

Tagging resource groups versus resources

It is a must for architects to decide the taxonomy and information architecture for Azure resources and resource groups. They should identify the categories by which resources will be classified based on the query requirements. However, they must also identify whether tags should be attached to individual resources or to resource groups.

If all resources within a resource group need the same tag, then it is better to tag the resource group, even though tags don't inherit the resources in the resource group. If your organization requires tags to be passed to all the underlying resources, then you can consider writing a PowerShell script to get the tags from the resource group and update the tags for the resources in the resource group. It is important to take queries on tags into consideration before deciding whether tags should be applied at the resource level or the resource group level. If the queries relate to individual resource types across a subscription and across resource groups, then assigning tags to individual resources makes more sense. However, if identifying resource groups is enough for your queries to be effective, then tags should be applied only to resource groups. If you are moving resources across resource groups, the tags applied at the resource group level will be lost. If you are moving resources, consider adding the tags again.