Skip to content

Commit 963a313

Browse files
committed
split tags content
1 parent a4a2dba commit 963a313

File tree

7 files changed

+879
-806
lines changed

7 files changed

+879
-806
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
---
2+
title: Tag resources, resource groups, and subscriptions with Bicep
3+
description: Shows how to use Bicep to apply tags to Azure resources.
4+
ms.topic: conceptual
5+
ms.date: 04/11/2023
6+
---
7+
8+
# Apply tags with Bicep
9+
10+
This article describes how to use Bicep to tag resources, resource groups, and subscriptions during deployment. For tag recommendations and limitations, see [Use tags to organize your Azure resources and management hierarchy](tag-resources.md).
11+
12+
> [!NOTE]
13+
> The tags you apply through a Bicep file overwrite any existing tags.
14+
15+
## Apply values
16+
17+
The following example deploys a storage account with three tags. Two of the tags (`Dept` and `Environment`) are set to literal values. One tag (`LastDeployed`) is set to a parameter that defaults to the current date.
18+
19+
```Bicep
20+
param location string = resourceGroup().location
21+
param utcShort string = utcNow('d')
22+
23+
resource stgAccount 'Microsoft.Storage/storageAccounts@2021-04-01' = {
24+
name: 'storage${uniqueString(resourceGroup().id)}'
25+
location: location
26+
sku: {
27+
name: 'Standard_LRS'
28+
}
29+
kind: 'Storage'
30+
tags: {
31+
Dept: 'Finance'
32+
Environment: 'Production'
33+
LastDeployed: utcShort
34+
}
35+
}
36+
```
37+
38+
## Apply an object
39+
40+
You can define an object parameter that stores several tags and apply that object to the tag element. This approach provides more flexibility than the previous example because the object can have different properties. Each property in the object becomes a separate tag for the resource. The following example has a parameter named `tagValues` that's applied to the tag element.
41+
42+
```Bicep
43+
param location string = resourceGroup().location
44+
param tagValues object = {
45+
Dept: 'Finance'
46+
Environment: 'Production'
47+
}
48+
49+
resource stgAccount 'Microsoft.Storage/storageAccounts@2021-04-01' = {
50+
name: 'storage${uniqueString(resourceGroup().id)}'
51+
location: location
52+
sku: {
53+
name: 'Standard_LRS'
54+
}
55+
kind: 'Storage'
56+
tags: tagValues
57+
}
58+
```
59+
60+
## Apply a JSON string
61+
62+
To store many values in a single tag, apply a JSON string that represents the values. The entire JSON string is stored as one tag that can't exceed 256 characters. The following example has a single tag named `CostCenter` that contains several values from a JSON string:
63+
64+
```Bicep
65+
param location string = resourceGroup().location
66+
67+
resource stgAccount 'Microsoft.Storage/storageAccounts@2021-04-01' = {
68+
name: 'storage${uniqueString(resourceGroup().id)}'
69+
location: location
70+
sku: {
71+
name: 'Standard_LRS'
72+
}
73+
kind: 'Storage'
74+
tags: {
75+
CostCenter: '{"Dept":"Finance","Environment":"Production"}'
76+
}
77+
}
78+
```
79+
80+
## Apply tags from resource group
81+
82+
To apply tags from a resource group to a resource, use the [resourceGroup()](../templates/template-functions-resource.md#resourcegroup) function. When you get the tag value, use the `tags[tag-name]` syntax instead of the `tags.tag-name` syntax, because some characters aren't parsed correctly in the dot notation.
83+
84+
```Bicep
85+
param location string = resourceGroup().location
86+
87+
resource stgAccount 'Microsoft.Storage/storageAccounts@2021-04-01' = {
88+
name: 'storage${uniqueString(resourceGroup().id)}'
89+
location: location
90+
sku: {
91+
name: 'Standard_LRS'
92+
}
93+
kind: 'Storage'
94+
tags: {
95+
Dept: resourceGroup().tags['Dept']
96+
Environment: resourceGroup().tags['Environment']
97+
}
98+
}
99+
```
100+
101+
## Apply tags to resource groups or subscriptions
102+
103+
You can add tags to a resource group or subscription by deploying the `Microsoft.Resources/tags` resource type. You can apply the tags to the target resource group or subscription you want to deploy. Each time you deploy the template you replace any previous tags.
104+
105+
```Bicep
106+
param tagName string = 'TeamName'
107+
param tagValue string = 'AppTeam1'
108+
109+
resource applyTags 'Microsoft.Resources/tags@2021-04-01' = {
110+
name: 'default'
111+
properties: {
112+
tags: {
113+
'${tagName}': tagValue
114+
}
115+
}
116+
}
117+
```
118+
119+
The following Bicep adds the tags from an object to the subscription it's deployed to. For more information about subscription deployments, see [Create resource groups and resources at the subscription level](../bicep/deploy-to-subscription.md).
120+
121+
```Bicep
122+
targetScope = 'subscription'
123+
124+
param tagObject object = {
125+
TeamName: 'AppTeam1'
126+
Dept: 'Finance'
127+
Environment: 'Production'
128+
}
129+
130+
resource applyTags 'Microsoft.Resources/tags@2021-04-01' = {
131+
name: 'default'
132+
properties: {
133+
tags: tagObject
134+
}
135+
}
136+
```
137+
138+
## Next steps
139+
140+
* 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).
141+
* 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).
142+
* For tag recommendations and limitations, see [Use tags to organize your Azure resources and management hierarchy](tag-resources.md).
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
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).
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
title: Tag resources, resource groups, and subscriptions with Azure portal
3+
description: Shows how to use Azure portal to apply tags to Azure resources.
4+
ms.topic: conceptual
5+
ms.date: 04/11/2023
6+
---
7+
8+
# Apply tags with Azure portal
9+
10+
This article describes how to use the Azure portal to tag resources. For tag recommendations and limitations, see [Use tags to organize your Azure resources and management hierarchy](tag-resources.md).
11+
12+
[!INCLUDE [resource-manager-tag-resource](../../../includes/resource-manager-tag-resources.md)]
13+
14+
## Next steps
15+
16+
* 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).
17+
* 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).
18+
* For tag recommendations and limitations, see [Use tags to organize your Azure resources and management hierarchy](tag-resources.md).

0 commit comments

Comments
 (0)