|
| 1 | +--- |
| 2 | +title: Tag resources, resource groups, and subscriptions with Azure CLI |
| 3 | +description: Shows how to use Azure CLI to apply tags to Azure resources. |
| 4 | +ms.topic: conceptual |
| 5 | +ms.date: 04/11/2023 |
| 6 | +--- |
| 7 | + |
| 8 | +# Apply tags with Azure CLI |
| 9 | + |
| 10 | +This article describes how to use Azure CLI to tag resources, resource groups, and subscriptions. For tag recommendations and limitations, see [Use tags to organize your Azure resources and management hierarchy](tag-resources.md). |
| 11 | + |
| 12 | +## Apply tags |
| 13 | + |
| 14 | +Azure CLI offers two commands to apply tags: [az tag create](/cli/azure/tag#az-tag-create) and [az tag update](/cli/azure/tag#az-tag-update). You need to have the Azure CLI 2.10.0 version or later. You can check your version with `az version`. To update or install it, see [Install the Azure CLI](/cli/azure/install-azure-cli). |
| 15 | + |
| 16 | +The `az tag create` replaces all tags on the resource, resource group, or subscription. When you call the command, pass the resource ID of the entity you want to tag. |
| 17 | + |
| 18 | +The following example applies a set of tags to a storage account: |
| 19 | + |
| 20 | +```azurecli-interactive |
| 21 | +resource=$(az resource show -g demoGroup -n demostorage --resource-type Microsoft.Storage/storageAccounts --query "id" --output tsv) |
| 22 | +az tag create --resource-id $resource --tags Dept=Finance Status=Normal |
| 23 | +``` |
| 24 | + |
| 25 | +When the command completes, notice that the resource has two tags. |
| 26 | + |
| 27 | +```output |
| 28 | +"properties": { |
| 29 | + "tags": { |
| 30 | + "Dept": "Finance", |
| 31 | + "Status": "Normal" |
| 32 | + } |
| 33 | +}, |
| 34 | +``` |
| 35 | + |
| 36 | +If you run the command again, but this time with different tags, notice that the earlier tags disappear. |
| 37 | + |
| 38 | +```azurecli-interactive |
| 39 | +az tag create --resource-id $resource --tags Team=Compliance Environment=Production |
| 40 | +``` |
| 41 | + |
| 42 | +```output |
| 43 | +"properties": { |
| 44 | + "tags": { |
| 45 | + "Environment": "Production", |
| 46 | + "Team": "Compliance" |
| 47 | + } |
| 48 | +}, |
| 49 | +``` |
| 50 | + |
| 51 | +To add tags to a resource that already has tags, use `az tag update`. Set the `--operation` parameter to `Merge`. |
| 52 | + |
| 53 | +```azurecli-interactive |
| 54 | +az tag update --resource-id $resource --operation Merge --tags Dept=Finance Status=Normal |
| 55 | +``` |
| 56 | + |
| 57 | +Notice that the existing tags grow with the addition of the two new tags. |
| 58 | + |
| 59 | +```output |
| 60 | +"properties": { |
| 61 | + "tags": { |
| 62 | + "Dept": "Finance", |
| 63 | + "Environment": "Production", |
| 64 | + "Status": "Normal", |
| 65 | + "Team": "Compliance" |
| 66 | + } |
| 67 | +}, |
| 68 | +``` |
| 69 | + |
| 70 | +Each tag name can have only one value. If you provide a new value for a tag, the new tag replaces the old value, even if you use the merge operation. The following example changes the `Status` tag from _Normal_ to _Green_. |
| 71 | + |
| 72 | +```azurecli-interactive |
| 73 | +az tag update --resource-id $resource --operation Merge --tags Status=Green |
| 74 | +``` |
| 75 | + |
| 76 | +```output |
| 77 | +"properties": { |
| 78 | + "tags": { |
| 79 | + "Dept": "Finance", |
| 80 | + "Environment": "Production", |
| 81 | + "Status": "Green", |
| 82 | + "Team": "Compliance" |
| 83 | + } |
| 84 | +}, |
| 85 | +``` |
| 86 | + |
| 87 | +When you set the `--operation` parameter to `Replace`, the new set of tags replaces the existing tags. |
| 88 | + |
| 89 | +```azurecli-interactive |
| 90 | +az tag update --resource-id $resource --operation Replace --tags Project=ECommerce CostCenter=00123 Team=Web |
| 91 | +``` |
| 92 | + |
| 93 | +Only the new tags remain on the resource. |
| 94 | + |
| 95 | +```output |
| 96 | +"properties": { |
| 97 | + "tags": { |
| 98 | + "CostCenter": "00123", |
| 99 | + "Project": "ECommerce", |
| 100 | + "Team": "Web" |
| 101 | + } |
| 102 | +}, |
| 103 | +``` |
| 104 | + |
| 105 | +The same commands also work with resource groups or subscriptions. Pass them in the identifier of the resource group or subscription you want to tag. |
| 106 | + |
| 107 | +To add a new set of tags to a resource group, use: |
| 108 | + |
| 109 | +```azurecli-interactive |
| 110 | +group=$(az group show -n demoGroup --query id --output tsv) |
| 111 | +az tag create --resource-id $group --tags Dept=Finance Status=Normal |
| 112 | +``` |
| 113 | + |
| 114 | +To update the tags for a resource group, use: |
| 115 | + |
| 116 | +```azurecli-interactive |
| 117 | +az tag update --resource-id $group --operation Merge --tags CostCenter=00123 Environment=Production |
| 118 | +``` |
| 119 | + |
| 120 | +To add a new set of tags to a subscription, use: |
| 121 | + |
| 122 | +```azurecli-interactive |
| 123 | +sub=$(az account show --subscription "Demo Subscription" --query id --output tsv) |
| 124 | +az tag create --resource-id /subscriptions/$sub --tags CostCenter=00123 Environment=Dev |
| 125 | +``` |
| 126 | + |
| 127 | +To update the tags for a subscription, use: |
| 128 | + |
| 129 | +```azurecli-interactive |
| 130 | +az tag update --resource-id /subscriptions/$sub --operation Merge --tags Team="Web Apps" |
| 131 | +``` |
| 132 | + |
| 133 | +## List tags |
| 134 | + |
| 135 | +To get the tags for a resource, resource group, or subscription, use the [az tag list](/cli/azure/tag#az-tag-list) command and pass the resource ID of the entity. |
| 136 | + |
| 137 | +To see the tags for a resource, use: |
| 138 | + |
| 139 | +```azurecli-interactive |
| 140 | +resource=$(az resource show -g demoGroup -n demostorage --resource-type Microsoft.Storage/storageAccounts --query "id" --output tsv) |
| 141 | +az tag list --resource-id $resource |
| 142 | +``` |
| 143 | + |
| 144 | +To see the tags for a resource group, use: |
| 145 | + |
| 146 | +```azurecli-interactive |
| 147 | +group=$(az group show -n demoGroup --query id --output tsv) |
| 148 | +az tag list --resource-id $group |
| 149 | +``` |
| 150 | + |
| 151 | +To see the tags for a subscription, use: |
| 152 | + |
| 153 | +```azurecli-interactive |
| 154 | +sub=$(az account show --subscription "Demo Subscription" --query id --output tsv) |
| 155 | +az tag list --resource-id /subscriptions/$sub |
| 156 | +``` |
| 157 | + |
| 158 | +## List by tag |
| 159 | + |
| 160 | +To get resources that have a specific tag name and value, use: |
| 161 | + |
| 162 | +```azurecli-interactive |
| 163 | +az resource list --tag CostCenter=00123 --query [].name |
| 164 | +``` |
| 165 | + |
| 166 | +To get resources that have a specific tag name with any tag value, use: |
| 167 | + |
| 168 | +```azurecli-interactive |
| 169 | +az resource list --tag Team --query [].name |
| 170 | +``` |
| 171 | + |
| 172 | +To get resource groups that have a specific tag name and value, use: |
| 173 | + |
| 174 | +```azurecli-interactive |
| 175 | +az group list --tag Dept=Finance |
| 176 | +``` |
| 177 | + |
| 178 | +## Remove tags |
| 179 | + |
| 180 | +To remove specific tags, use `az tag update` and set `--operation` to `Delete`. Pass the resource ID of the tags you want to delete. |
| 181 | + |
| 182 | +```azurecli-interactive |
| 183 | +az tag update --resource-id $resource --operation Delete --tags Project=ECommerce Team=Web |
| 184 | +``` |
| 185 | + |
| 186 | +You've removed the specified tags. |
| 187 | + |
| 188 | +```output |
| 189 | +"properties": { |
| 190 | + "tags": { |
| 191 | + "CostCenter": "00123" |
| 192 | + } |
| 193 | +}, |
| 194 | +``` |
| 195 | + |
| 196 | +To remove all tags, use the [az tag delete](/cli/azure/tag#az-tag-delete) command. |
| 197 | + |
| 198 | +```azurecli-interactive |
| 199 | +az tag delete --resource-id $resource |
| 200 | +``` |
| 201 | + |
| 202 | +## Handling spaces |
| 203 | + |
| 204 | +If your tag names or values include spaces, enclose them in quotation marks. |
| 205 | + |
| 206 | +```azurecli-interactive |
| 207 | +az tag update --resource-id $group --operation Merge --tags "Cost Center"=Finance-1222 Location="West US" |
| 208 | +``` |
| 209 | + |
| 210 | +## Next steps |
| 211 | + |
| 212 | +* Not all resource types support tags. To determine if you can apply a tag to a resource type, see [Tag support for Azure resources](tag-support.md). |
| 213 | +* For recommendations on how to implement a tagging strategy, see [Resource naming and tagging decision guide](/azure/cloud-adoption-framework/decision-guides/resource-tagging/?toc=/azure/azure-resource-manager/management/toc.json). |
| 214 | +* For tag recommendations and limitations, see [Use tags to organize your Azure resources and management hierarchy](tag-resources.md). |
0 commit comments