Tag resources, resource groups, and subscriptions for logical organization - Azure Resource Manager (2023)

  • Article
  • 17 minutes to read

Tags are metadata elements that you apply to your Azure resources. They are key-value pairs that help you identify resources based on settings relevant to your organization. If you want to track the deployment environment of your resources, add a key named Environment. To identify production-ready resources, assign them the Production value. The key-value pair for Environment = Production is fully formed.

You can apply tags to your Azure resources, resource groups, and subscriptions.

For recommendations on how to implement a tagging strategy, seeDecision support for resource naming and labeling.

Resource tags support all paid services. To ensure paid services are tagged, use one of theLabel Guidelines.

warning

Tags are stored as plain text. Never add sensitive values ​​to tags. Sensitive values ​​can be exposed through many methods, including cost reports, commands that return existing tag definitions, deployment histories, exported templates, and audit logs.

warning

Be careful when using a language other than English on your labels. It may cause a decode progress error when loading your VM's metadata from IMDS (Instance Metadata Service).

Important

In operations, tag names are not case sensitive. A tag with a tag name is updated or retrieved regardless of the case. However, the resource provider can preserve the case that you specify for the tag name. You will see this case in cost reports.

Label values ​​are case sensitive.

use

This article provides steps to remove personal data from your device or service and can be used to support your obligations under the GDPR. You can find general information about the GDPR atGDPR section of the Microsoft Trust Centerand theGDPR section of the Service Trust portal.

access required

There are two ways to get the access needed to tag resources.

  • Has write access to theMicrosoft.Resources/Tagsresource type. With this access, you can tag any resource even if you don't have access to the resource itself. Helabel contributorThe role grants this access. For example, the Tag Contributor role cannot apply tags to resources or resource groups through the portal. However, you can apply tags to subscriptions through the portal. Supports all tag operations via Azure PowerShell and REST API.

  • You can have write access to the resource itself. HetaxpayerThe role grants the necessary access to apply labels to any entity. To apply tags to only one type of resource, use the Contributor role for that resource. For example, to apply tags to virtual machines, use theVirtual Machine Contributor.

Power Shell

apply labels

Azure PowerShell provides two commands to apply labels:New-AzTagYUpdate-AzTag. you must have itThe resourcesModule version 1.12.0 or higher. You can check your version withGet-InstalledModule -Nombre Az.Resources. You can install this module orInstalar Azure PowerShellVersion 3.6.1 or higher.

HeNew-AzTagreplaces any tag for the resource, resource group, or subscription. When you invoke the command, pass in the resource ID of the entity you want to tag.

The following example applies a tag set to a storage account:

$tags = @{"Department"="Finance"; "Status"="Normal"}$resource = Get-AzResource -Name demoStorage -ResourceGroup demoGroupNew-AzTag -ResourceId $resource.id -Tag $tags

After the command completes, notice that the resource has two tags.

Properties: Name Value ====== ====== Department Finance Status Normal

If you run the command again, but this time with different labels, notice that the old labels disappear.

(Video) AZ-900 Episode 8 | Resources, Resource Groups & Resource Manager | Azure Fundamentals Course

$tags = @{"Team"="Compliance"; "Environment"="Production"}New-AzTag -ResourceId $resource.id -Tag $tags
Properties: Name Value ============ ========= Environment Production Equipment Compliance

Use it to add tags to a resource that already has tagsUpdate-AzTag. set those-Operationparameters tobind.

$tags = @{"Department"="Finance"; "Status"="Normal"}Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Merge

Notice that the existing tags grow as the two new tags are added.

Properties: Name Value ============ ========= Status Normal Department Finance Team Compliance Environment Production

Each tag name can only have one value. If you specify a new value for a tag, it replaces the old value even if you use the merge operation. The following example changes thestateday ofNormalAVerde.

$tags = @{"Status"="Green"}Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Merge
Properties: Nombre Value =========== ========== Estado Green Dept Finance Team Compliance Environment Production

When configuring the-Operationparameters toSubstitute, the new set of tags replaces the existing tags.

$tags = @{"Project"="E-commerce"; "Cost Center"="00123"; "Team"="Web"}AzTag-Update -ResourceId $resource.id -Tag $tags -Operation Replace

Only new tags remain on the resource.

Properties: Name Value ========= ========= CostCenter 00123 Team Web Project ECommerce

The same commands also work with resource groups or subscriptions. Pass them in the ID of the resource group or subscription you want to tag.

To add a new set of tags to a resource group, use:

$tags = @{"Department"="Finance"; "Status"="Normal"}$resourceGroup = Get-AzResourceGroup -Name demoGroupNew-AzTag -ResourceId $resourceGroup.ResourceId -tag $tags

To update the tags for a resource group, use:

$etiquetas = @{"CostCenter"="00123"; "Entorno"="Producción"}$resourceGroup = Get-AzResourceGroup -Name demoGroupUpdate-AzTag -ResourceId $resourceGroup.ResourceId -Tag $tags -Operation Merge

To add a new set of tags to a subscription, use:

$etiquetas = @{"CostCenter"="00123"; "Environment"="Dev"}$subscription = (Get-AzSubscription -SubscriptionName "Beispielabonnement").IdNew-AzTag -ResourceId "/subscriptions/$subscription" -Tag $tags

To update labels for a subscription usage:

$tags = @{"Team"="Web Apps"}$subscription = (Get-AzSubscription -SubscriptionName "Ejemplo de suscripción").IdUpdate-AzTag -ResourceId "/subscriptions/$subscription" -Tag $tags -Operation Merge

You can have more than one resource with the same name in a resource group. In this case, you can configure each resource with the following commands:

$resource = Get-AzResource -ResourceName sqlDatabase1 -ResourceGroupName sample group $resource | ForEach-Object { Update-AzTag -Tag @{ "Dept"="IT"; "Environment"="Test" } -ResourceId $_.ResourceId -merge operation }

list labels

To get the tags for a resource, resource group, or subscription, use theGet AzTagcommand and pass the resource ID of the entity.

To view the labels for a resource usage:

$recurso = Get-AzResource -Name demoStorage -ResourceGroup demoGroupGet-AzTag -ResourceId $resource.id

To view the tags for a resource group, use:

$resourceGroup = Get-AzResourceGroup -Name demoGroupGet-AzTag -ResourceId $resourceGroup.ResourceId

To view the labels for a subscription usage:

$subscription = (Get-AzSubscription -SubscriptionName "Beispielabonnement").IdGet-AzTag -ResourceId "/subscriptions/$subscription"

list per day

To get resources with a specific tag name and value, use:

(Get-AzResource-Tag @{ "CostCenter"="00123"}).Name

To get resources that have a specific tag name with any tag value, use:

( Get - AzResource - TagName " Dept " ).Name

To get resource groups with a specific tag name and value, use:

(Get-AzResourceGroup -Tag @{ "CostCenter"="00123" }).ResourceGroupName

remove bookmarks

To remove specific tags, useUpdate-AzTagand adjust-OperationAextinguish. Pass the resource IDs of the tags you want to remove.

$removeTags = @{"Project"="E-commerce"; "Team"="Web"}Update-AzTag -ResourceId $resource.id -Tag $removeTags -Remove task

The specified tags will be removed.

Properties: Name Value ========== ===== CostCenter 00123

To remove all tags, use theRemove-AzTagDomain.

$subscription = (Get-AzSubscription -SubscriptionName „Beispielabonnement“).IdRemove-AzTag -ResourceId „/subscriptions/$subscription“

CLI de Azure

apply labels

Azure CLI provides two commands to apply tags:Create label a-zYthe member update. You need Azure CLI version 2.10.0 or higher. You can check your version withblue version. To update or install, seeInstall the Azure CLI.

HeCreate label a-zreplaces any tag for the resource, resource group, or subscription. When you invoke the command, pass in the resource ID of the entity you want to tag.

The following example applies a tag set to a storage account:

resource=$(az resource show -g demoGroup -n demoStorage --resource-type Microsoft.Storage/storageAccounts --query "id" --output tsv)az tag create --resource-id $resource --tags Dept=Finanzas Estado=Normal

After the command completes, notice that the resource has two tags.

"properties": { "labels": { "Dept": "Financial", "Status": "Normal" }},

If you run the command again, but this time with different labels, notice that the old labels disappear.

az tag create --resource-id $resource --tags Team=Compliance environment=Production
"properties": { "tags": { "Environment": "Production", "Equipment": "Compliance" }},

Use it to add tags to a resource that already has tagsthe member update. set those--Operationparameters tobind.

(Video) Azure Resource Group / Subscription / Tags Introduction

update az tag --resource-id $resource --operation Merge --tags Dept=Finance Status=Normal

Notice that the existing tags grow as the two new tags are added.

"properties": { "tags": { "Dept": "Finance", "Environment": "Production", "Status": "Normal", "Equipment": "Compliance" }},

Each tag name can only have one value. If you specify a new value for a tag, the new tag replaces the old value, even if you use the merge operation. The following example changes thestateday ofNormalAVerde.

az tag update --resource-id $resource --operation Merge --tags Status=Grün
"properties": { "tags": { "Dept": "Finance", "Environment": "Production", "State": "Green", "Equipment": "Compliance" }},

When configuring the--Operationparameters toSubstitute, the new set of tags replaces the existing tags.

update az tag --resource-id $resource --operation Replace --tags Project=ECommerce CostCenter=00123 Team=Web

Only new tags remain on the resource.

"properties": { "tags": { "CostCenter": "00123", "Project": "Ecommerce", "Team": "Web" }},

The same commands also work with resource groups or subscriptions. Pass them in the ID of the resource group or subscription you want to tag.

To add a new set of tags to a resource group, use:

group=$(az group show -n demoGroup --query id --output tsv)az tag create --resource-id $group --tags Dept=Finance Status=Normal

To update the tags for a resource group, use:

az tag update --resource-id $group --operation Merge --tags CostCenter=00123 Environment=Production

To add a new set of tags to a subscription, use:

sub=$(show account az --subscription "Demo Subscription" --query id --output tsv) create tag az --resource-id /subscriptions/$sub --tags CostCenter=00123 Environment=Dev

To update labels for a subscription usage:

az tag update --resource-id /subscriptions/$sub --operation Merge --tags Team="Web Apps"

list labels

To get the tags for a resource, resource group, or subscription, use theaz tag listcommand and pass the resource ID of the entity.

To view the labels for a resource usage:

resource=$(az resource show -g demoGroup -n demoStorage --resource-type Microsoft.Storage/storageAccounts --query "id" --output tsv)az tag list --resource-id $resource

To view the tags for a resource group, use:

group=$(az group show -n demoGroup --query id --output tsv)az tag list --resource-id $group

To view the labels for a subscription usage:

sub=$(show account az --subscription "Demo Subscription" --query id --output tsv)list tags az --resource-id /subscriptions/$sub

list per day

To get resources with a specific tag name and value, use:

resource list az --tag CostCenter=00123 --query [].name

To get resources that have a specific tag name with any tag value, use:

resource list az --tag Team --query [].name

To get resource groups with a specific tag name and value, use:

list of groups az --tag Dept=Finance

remove bookmarks

To remove specific tags, usethe member updateand adjust--OperationAextinguish. Pass the resource ID of the tags you want to remove.

update the az tag --resource-id $resource --operation Delete --tags Project=ECommerce Team=Web

You have removed the specified tags.

"properties": { "labels": { "CostCenter": "00123" }},

To remove all tags, use thedelete memberDomain.

az-Tag delete --resource-id $recurso

Dealing with spaces

If your tag names or values ​​contain spaces, enclose them in quotes.

update tag az --resource-id $group --Merge operation --tags "Cost Center"=Finance-1222 Location="West US"

plantillas ARM

You can tag resources, resource groups, and subscriptions with an ARM template during deployment.

use

Tags you apply via an ARM template or bicep file will overwrite existing tags.

apply values

The following example provisions a storage account with three tags. Two of the labels (aboutYSurroundings) are set to literal values. One day (last implemented) is set to a parameter that defaults to the current date.

  • JSON
  • biceps
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "utcShort ": { "type": "string", "default": "[utcNow('d')]" }, "location": { "type": "string", "default": "[group of resources(). location]" } }, "resources": [ { "type": "Microsoft.Storage/storageAccounts", "apiVersion": "2021-04-01", "name": "[concat('storage ', unique string( resourceGroup().id))]", "location": "[parameters('location')]", "sku": { "name": "Standard_LRS" }, "type": "Storage ", "tags" : { "Dept": "Finance", "Environment": "Production", "LastDeployed": "[parameters('utcShort')]" }, "properties": {} } ]}

apply an object

You can define an object parameter that stores multiple tags and apply that object to the tag element. This approach offers more flexibility than the previous example because the object can have different properties. Each property on the object becomes a separate tag for the resource. The following example has a parameter calledtag valuesapplied to the tag element.

(Video) AZ-900 Episode 30 | Azure Resource Tags

  • JSON
  • biceps
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "location ": { "type": "string", "defaultValue": "[resourceGroup().location]" }, "tagValues": { "type": "object", "defaultValue": { "Dept": "Finance ", "Environment": "Production" } } }, "resources": [ { "type": "Microsoft.Storage/storageAccounts", "apiVersion": "2021-04-01", "name": "[concat ('storage', uniquestring(resourceGroup().id))]", "location": "[params('location')]", "sku": { "name": "Standard_LRS" }, "type" : "Speech", "tags": "[parameters('tagValues')]", "properties": {} } ]}

Apply a JSON string

To store many values ​​in a single tag, apply a JSON string that represents the values. The entire JSON string is stored as a tag that cannot exceed 256 characters. The following example has a single tag namedcost centercontaining multiple values ​​of a JSON string:

  • JSON
  • biceps
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "location ": { "type": "string", "defaultValue": "[resourceGroup().location]" } }, "resources": [ { "type": "Microsoft.Storage/storageAccounts", "apiVersion": " 2021-04-01", "name": "[concat('storage', uniqueString(resourceGroup().id))]", "location": "[params('location')]", "sku": { "name": "Standard_LRS" }, "kind": "Storage", "tags": { "CostCenter": "{\"Dept\":\"Finance\",\"Environment\":\" Production \"}" }, "Eigenschaften": {} } ]}

Apply resource group tags

To apply tags from a resource group to a resource, use theresourcegroup()Function. If you get the value from the tag, use theTags[Tag-Name]syntax instead oftags.tag-nameSyntax because some characters in dot notation are not parsed correctly.

  • JSON
  • biceps
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "location ": { "type": "string", "defaultValue": "[resourceGroup().location]" } }, "resources": [ { "type": "Microsoft.Storage/storageAccounts", "apiVersion": " 2021-04-01", "name": "[concat('storage', uniqueString(resourceGroup().id))]", "location": "[params('location')]", "sku": { "name": "Standard_LRS" }, "kind": "Storage", "tags": { "Dept": "[resourceGroup().tags['Dept']]", "Environment": "[resourceGroup( ).tags['Umgebung']]" }, "Eigenschaften": {} } ]}

Apply tags to resource groups or subscriptions

You can add tags to a resource group or subscription by providing theMicrosoft.Resources/Tagsresource type. You can apply the tags to the target resource group or subscription that you want to deploy. Every time you deploy the template, replace the old tags.

  • JSON
  • biceps
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parámetros": { "tagName ": { "type": "string", "defaultValue": "TeamName" }, "tagValue": { "type": "string", "defaultValue": "AppTeam1" } }, "resources": [ { " type": "Microsoft.Resources/tags", "name": "predeterminado", "apiVersion": "2021-04-01", "properties": { "tags": { "[parámetros('tagName')] ": "[parámetro('tagValue')]" } } } ]}

To apply the tags to a resource group, use Azure PowerShell or the Azure CLI. Post them to the resource group you want to tag.

New-AzResourceGroupDeployment -ResourceGroupName exampleGroup -TemplateFile https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/tags.json
az deployment group create --resource-group exampleGroup --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/tags.json

To apply the tags to a subscription, use PowerShell or the Azure CLI. Provide the subscription you want to tag.

New-AzSubscriptionDeployment -name tagresourcegroup -Ubicación westus2 -TemplateUri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/tags.json
az deployment sub create --name tagresourcegroup --location westus2 --template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/tags.json

For more information on subscription implementations, seeCreate resource groups and subscription-level resources.

The following template adds the tags for an object to a resource group or subscription.

  • JSON
  • biceps
{ "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "tags ": { "type": "object", "defaultValue": { "TeamName": "AppTeam1", "Dept": "Finance", "Environment": "Production" } } }, "resources": [ { " type": "Microsoft.Resources/tags", "apiVersion": "2021-04-01", "name": "default", "properties": { "tags": "[parameters('tags')]" } } ]}

Portal

If a user does not have the necessary access to apply tags, they can assign themlabel contributorrole for the user. For more information, seeTutorial: Give a user access to Azure resources using RBAC and the Azure portal.

  1. To view the tags for a resource or resource group, look for existing tags in the overview. If you have not previously applied any tags, the list will be empty.

    Tag resources, resource groups, and subscriptions for logical organization - Azure Resource Manager (1)

  2. To add a tag, selectClick here to add tags.

  3. Provide a name and a value.

    Tag resources, resource groups, and subscriptions for logical organization - Azure Resource Manager (2)

  4. Add more tags as needed. When you are done, selectsave to computer.

    Tag resources, resource groups, and subscriptions for logical organization - Azure Resource Manager (3)

  5. Tags are now displayed in the overview.

    Tag resources, resource groups, and subscriptions for logical organization - Azure Resource Manager (4)

  6. To add or remove a tag, selectchange.

  7. To delete a tag, select the trash can icon. then selectsave to computer.

    Tag resources, resource groups, and subscriptions for logical organization - Azure Resource Manager (5)

To assign tags to multiple resources at once:

  1. In any list of resources, select the check box of the resources you want to assign the tag to. then selectassign tags.

    (Video) Managing Azure Subscriptions and Resource Groups - Azure Training

    Tag resources, resource groups, and subscriptions for logical organization - Azure Resource Manager (6)

  2. Add names and values. When you are done, selectsave to computer.

    Tag resources, resource groups, and subscriptions for logical organization - Azure Resource Manager (7)

To see all resources with a tag:

  1. Search in the Azure portal menukeywords. Select it from the available options.

    Tag resources, resource groups, and subscriptions for logical organization - Azure Resource Manager (8)

  2. Select the tag to view the resources.

    Tag resources, resource groups, and subscriptions for logical organization - Azure Resource Manager (9)

  3. All resources with that tag are displayed.

    Tag resources, resource groups, and subscriptions for logical organization - Azure Resource Manager (10)

REST-API

To work with tags via the Azure REST API, use:

  • Tags: create or update in scope(PUT operation)
  • Tags: scope update(PATCH operation)
  • Tags - Get Reach(GET operation)
  • Tags - Delete in scope(Operation DELETE)

SDK

For examples of how to apply tags using the SDK, see:

Resources don't inherit tags that you assign to a resource group or subscription. For information about how to apply subscription or resource group tags to resources, seeAzure Policies - Tags.

Labels and Billing

You can use tags to group your billing data. For example, if you run multiple virtual machines for different organizations, use tags to group usage by cost center. You can also use tags to categorize costs by runtime environment, such as B. Billing usage for virtual machines running in the production environment.

You can get information about the tags by downloading the usage file available in the Azure portal. For more information, seeDownload or view your Azure bill and daily usage data. For services that support billing labels, the labels appear on thekeywordsTo divide.

For information on REST API operations, seeAzure Billing REST API Reference.

restrictions

The following restrictions apply to tags:

  • Not all resource types support tags. See if you can apply a tag to a resource typeTag support for Azure resources.

  • Each resource, resource group, and subscription can have a maximum of 50 tag name/value pairs. If you need to apply more tags than the maximum allowed, use a JSON string for the tag value. The JSON string can contain many of the values ​​that you apply to a single tag name. A resource group or subscription can contain many resources, each with 50 tag name value pairs.

  • The tag name is limited to 512 characters and the tag value is limited to 256 characters. For storage accounts, the tag name is limited to 128 characters and the tag value is limited to 256 characters.

  • Classic resources, such as cloud services, don't support tags.

  • Azure IP groups and Azure Firewall policies do not support PATCH operations. Therefore, PATCH API method operations cannot update tags through the portal. Instead, you can use update commands for these resources. With you can e.g. B. Update labels for an IP poolupdated az network ip groupDomain.

  • Label names cannot contain these characters:<,>,%,&,\,?,/

    use

    • Azure Domain Name System (DNS) zones do not support the use of spaces in the label or a label that begins with a number. Azure DNS label names don't support special characters or Unicode. The value can contain any character.

    • Traffic Manager does not support the use of spaces,#o:in the tag name. The tag name cannot start with a number.

      (Video) Benefits and Usage of Resource Groups - AZ-900 Certification Course

    • Azure Front Door does not support the use of#o:in the tag name.

    • The following Azure resources only support 15 tags:

      • blue automation
      • Azure Content Delivery Network (CDN)
      • Azure DNS (zone and A records)
      • Azure Log Analytics Saved Search

Next steps

  • Not all resource types support tags. See if you can apply a tag to a resource typeTag support for Azure resources.
  • For recommendations on how to implement a tagging strategy, seeDecision support for resource naming and labeling.

Videos

1. Working with Resources Groups in Microsoft Azure
(Shane Young)
2. What are Azure Management Groups, Subscriptions, Resource Groups, Resources?
(LearnTerest)
3. Azure resource groups
(kudvenkat)
4. Resource Groups in Azure || How to create Resource group || Resource Group Lab || AZ-900 || AZ-104
(Tech Cloud Solutions)
5. Learn Azure Resource Manager (ARM) Overview Explained
(Paddy Maddy)
6. AZ-140 ep01 | Mgmt grp Subs Resource grp
(Azure Academy)
Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated: 12/20/2022

Views: 6424

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.