Skip to main content

AZ-104 - Governance and Compliance - LAB Add Remove Tags

Add, Remove and Update Tags for Resources in Azure

Introduction

In the scenario for this hands-on lab, the finance department has reached out to you. They are requesting additional taxonomy information on a recent Azure bill, including who created the resources, which department budget should be used for the resources, and if the resources are necessary for running business critical systems.

If there are any non-essential business systems, they ask that you signify that in some way.

  • Add Tags to resource group
  • Modify tags for VM and Mark for Deletion
  • Modify tags for Virtual Network

Launch a powershell instance inside azure portal

Add Tags to the resource group

Run az group list and copy the name (395-5d062b4a-add-remove-and-update-tags-for-resou)

PS /home/cloud> az group list                                                                                                                                                      
[
  {
    "id": "/subscriptions/9734ed68-621d-47ed-babd-269110dbacb1/resourceGroups/395-5d062b4a-add-remove-and-update-tags-for-resou",
    "location": "westus",
    "managedBy": null,
    "name": "395-5d062b4a-add-remove-and-update-tags-for-resou",
    "properties": {
      "provisioningState": "Succeeded"
    },
    "tags": null,
    "type": "Microsoft.Resources/resourceGroups"
  }
]
Update the user group tags: 

- az group update --resource-group "<RESOURCE_GROUP_NAME>" --tags "Environment=Production" "Dept=IT" "CreatedBy=<YourName>"

PS /home/cloud> az group update --resource-group "395-5d062b4a-add-remove-and-update-tags-for-resou" --tags "Environment=Production" "Dept=IT" "CreatedBy=Cesar"                   
{
  "id": "/subscriptions/9734ed68-621d-47ed-babd-269110dbacb1/resourceGroups/395-5d062b4a-add-remove-and-update-tags-for-resou",
  "location": "westus",
  "managedBy": null,
  "name": "395-5d062b4a-add-remove-and-update-tags-for-resou",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": {
    "CreatedBy": "Cesar",
    "Dept": "IT",
    "Environment": "Production"
  },
  "type": "Microsoft.Resources/resourceGroups"
}
PS /home/cloud> 
Remove Tags for VM and Mark for Deletion
  1. In the Cloud Shell, list the existing virtual machines:
    PS /home/cloud> az vm list --query '[].{name:name, resourceGroup:resourceGroup, tags:tags}' -o json                                                                                
    [
      {
        "name": "webvm1",
        "resourceGroup": "395-5D062B4A-ADD-REMOVE-AND-UPDATE-TAGS-FOR-RESOU",
        "tags": {
          "defaultExperience": "Yes"
        }
      }
    ]
    PS /home/cloud> 
  2. Remove the existing tags from the VM:
    PS /home/cloud> az vm update -g "395-5d062b4a-add-remove-and-update-tags-for-resou" -n webvm1 --remove tags.defaultExperience                                                      
    
    {
      "additionalCapabilities": null,
      "applicationProfile": null,
      "availabilitySet": null,
      "billingProfile": null,
      "capacityReservation": null,
      "diagnosticsProfile": {
        "bootDiagnostics": {
          "enabled": true,
          "storageUri": "https://azurelalabi4q7bqaknwjqy.blob.core.windows.net/"
        }
      },
      "etag": null,
      "evictionPolicy": null,
      "extendedLocation": null,
      "extensionsTimeBudget": null,
      "hardwareProfile": {
        "vmSize": "Standard_B1ms",
        "vmSizeProperties": null
      },
      "host": null,
      "hostGroup": null,
      "id": "/subscriptions/9734ed68-621d-47ed-babd-269110dbacb1/resourceGroups/395-5d062b4a-add-remove-and-update-tags-for-resou/providers/Microsoft.Compute/virtualMachines/webvm1",
      "identity": null,
      "instanceView": null,
      "licenseType": null,
      "location": "westus",
      "managedBy": null,
      "name": "webvm1",
      "networkProfile": {
        "networkApiVersion": null,
        "networkInterfaceConfigurations": null,
        "networkInterfaces": [
          {
            "deleteOption": null,
            "id": "/subscriptions/9734ed68-621d-47ed-babd-269110dbacb1/resourceGroups/395-5d062b4a-add-remove-and-update-tags-for-resou/providers/Microsoft.Network/networkInterfaces/webvm1-nic1",
            "primary": null,
            "resourceGroup": "395-5d062b4a-add-remove-and-update-tags-for-resou"
          }
        ]
      },
      "osProfile": {
        "adminPassword": null,
        "adminUsername": "cloud_user",
        "allowExtensionOperations": null,
        "computerName": "webvm1",
        "customData": null,
        "linuxConfiguration": null,
        "requireGuestProvisionSignal": null,
        "secrets": [],
        "windowsConfiguration": {
          "additionalUnattendContent": null,
          "enableAutomaticUpdates": true,
          "enableVmAgentPlatformUpdates": false,
          "patchSettings": {
            "assessmentMode": "ImageDefault",
            "automaticByPlatformSettings": null,
            "enableHotpatching": null,
            "patchMode": "AutomaticByOS"
          },
          "provisionVmAgent": true,
          "timeZone": null,
          "winRm": null
        }
      },
      "plan": null,
      "platformFaultDomain": null,
      "priority": null,
      "provisioningState": "Succeeded",
      "proximityPlacementGroup": null,
      "resourceGroup": "395-5d062b4a-add-remove-and-update-tags-for-resou",
      "resources": null,
      "scheduledEventsProfile": null,
      "securityProfile": null,
      "storageProfile": {
        "dataDisks": [],
        "diskControllerType": null,
        "imageReference": {
          "communityGalleryImageId": null,
          "exactVersion": "14393.6709.240206",
          "id": null,
          "offer": "WindowsServer",
          "publisher": "MicrosoftWindowsServer",
          "sharedGalleryImageId": null,
          "sku": "2016-Datacenter",
          "version": "latest"
        },
        "osDisk": {
          "caching": "ReadWrite",
          "createOption": "FromImage",
          "deleteOption": "Detach",
          "diffDiskSettings": null,
          "diskSizeGb": 127,
          "encryptionSettings": null,
          "image": null,
          "managedDisk": {
            "diskEncryptionSet": null,
            "id": "/subscriptions/9734ed68-621d-47ed-babd-269110dbacb1/resourceGroups/395-5d062b4a-add-remove-and-update-tags-for-resou/providers/Microsoft.Compute/disks/webvm1_disk1_8db0168337f74a62a7160d01554e753f",
            "resourceGroup": "395-5d062b4a-add-remove-and-update-tags-for-resou",
            "securityProfile": null,
            "storageAccountType": "Premium_LRS"
          },
          "name": "webvm1_disk1_8db0168337f74a62a7160d01554e753f",
          "osType": "Windows",
          "vhd": null,
          "writeAcceleratorEnabled": null
        }
      },
      "tags": {},
      "timeCreated": "2024-02-13T20:59:23.426680+00:00",
      "type": "Microsoft.Compute/virtualMachines",
      "userData": null,
      "virtualMachineScaleSet": null,
      "vmId": "2292ae3f-ab87-42a0-b3ac-58ec941a77bc",
      "zones": null
    }
    PS /home/cloud> 
  3. Mark the VM for deletion
    PS /home/cloud> az vm update -g "395-5d062b4a-add-remove-and-update-tags-for-resou" -n webvm1 --set tags.MarkForDeletion=Yes                                                       
    
    {
      "additionalCapabilities": null,
      "applicationProfile": null,
      "availabilitySet": null,
      "billingProfile": null,
      "capacityReservation": null,
      "diagnosticsProfile": {
        "bootDiagnostics": {
          "enabled": true,
          "storageUri": "https://azurelalabi4q7bqaknwjqy.blob.core.windows.net/"
        }
      },
      "etag": null,
      "evictionPolicy": null,
      "extendedLocation": null,
      "extensionsTimeBudget": null,
      "hardwareProfile": {
        "vmSize": "Standard_B1ms",
        "vmSizeProperties": null
      },
      "host": null,
      "hostGroup": null,
      "id": "/subscriptions/9734ed68-621d-47ed-babd-269110dbacb1/resourceGroups/395-5d062b4a-add-remove-and-update-tags-for-resou/providers/Microsoft.Compute/virtualMachines/webvm1",
      "identity": null,
      "instanceView": null,
      "licenseType": null,
      "location": "westus",
      "managedBy": null,
      "name": "webvm1",
      "networkProfile": {
        "networkApiVersion": null,
        "networkInterfaceConfigurations": null,
        "networkInterfaces": [
          {
            "deleteOption": null,
            "id": "/subscriptions/9734ed68-621d-47ed-babd-269110dbacb1/resourceGroups/395-5d062b4a-add-remove-and-update-tags-for-resou/providers/Microsoft.Network/networkInterfaces/webvm1-nic1",
            "primary": null,
            "resourceGroup": "395-5d062b4a-add-remove-and-update-tags-for-resou"
          }
        ]
      },
      "osProfile": {
        "adminPassword": null,
        "adminUsername": "cloud_user",
        "allowExtensionOperations": null,
        "computerName": "webvm1",
        "customData": null,
        "linuxConfiguration": null,
        "requireGuestProvisionSignal": null,
        "secrets": [],
        "windowsConfiguration": {
          "additionalUnattendContent": null,
          "enableAutomaticUpdates": true,
          "enableVmAgentPlatformUpdates": false,
          "patchSettings": {
            "assessmentMode": "ImageDefault",
            "automaticByPlatformSettings": null,
            "enableHotpatching": null,
            "patchMode": "AutomaticByOS"
          },
          "provisionVmAgent": true,
          "timeZone": null,
          "winRm": null
        }
      },
      "plan": null,
      "platformFaultDomain": null,
      "priority": null,
      "provisioningState": "Succeeded",
      "proximityPlacementGroup": null,
      "resourceGroup": "395-5d062b4a-add-remove-and-update-tags-for-resou",
      "resources": null,
      "scheduledEventsProfile": null,
      "securityProfile": null,
      "storageProfile": {
        "dataDisks": [],
        "diskControllerType": null,
        "imageReference": {
          "communityGalleryImageId": null,
          "exactVersion": "14393.6709.240206",
          "id": null,
          "offer": "WindowsServer",
          "publisher": "MicrosoftWindowsServer",
          "sharedGalleryImageId": null,
          "sku": "2016-Datacenter",
          "version": "latest"
        },
        "osDisk": {
          "caching": "ReadWrite",
          "createOption": "FromImage",
          "deleteOption": "Detach",
          "diffDiskSettings": null,
          "diskSizeGb": 127,
          "encryptionSettings": null,
          "image": null,
          "managedDisk": {
            "diskEncryptionSet": null,
            "id": "/subscriptions/9734ed68-621d-47ed-babd-269110dbacb1/resourceGroups/395-5d062b4a-add-remove-and-update-tags-for-resou/providers/Microsoft.Compute/disks/webvm1_disk1_8db0168337f74a62a7160d01554e753f",
            "resourceGroup": "395-5d062b4a-add-remove-and-update-tags-for-resou",
            "securityProfile": null,
            "storageAccountType": "Premium_LRS"
          },
          "name": "webvm1_disk1_8db0168337f74a62a7160d01554e753f",
          "osType": "Windows",
          "vhd": null,
          "writeAcceleratorEnabled": null
        }
      },
      "tags": {
        "MarkForDeletion": "Yes"
      },
      "timeCreated": "2024-02-13T20:59:23.426680+00:00",
      "type": "Microsoft.Compute/virtualMachines",
      "userData": null,
      "virtualMachineScaleSet": null,
      "vmId": "2292ae3f-ab87-42a0-b3ac-58ec941a77bc",
      "zones": null
    }
    PS /home/cloud>
Change the Tags for the Virtual Network

In the cloud shell list the virtual networks

PS /home/cloud> az network vnet list --query '[].{name:name, resourceGroup:resourceGroup, tags:tags}' -o json                                                                      
[
  {
    "name": "vnet1",
    "resourceGroup": "395-5d062b4a-add-remove-and-update-tags-for-resou",
    "tags": {
      "Application": "MyApp",
      "Created By": "MyName",
      "Department": "MyDepartment"
    }
  }
]

Overwrite the existing tags:

PS /home/cloud> az resource tag --tags "Dept=IT" "Environment=Production" "CreatedBy=Cesar" --resource-group "395-5d062b4a-add-remove-and-update-tags-for-resou" -n "vnet1" --resource-type "Microsoft.Network/virtualNetworks"
{
  "etag": "W/\"c51873be-773d-4b96-ab24-fa37389401fe\"",
  "extendedLocation": null,
  "id": "/subscriptions/9734ed68-621d-47ed-babd-269110dbacb1/resourceGroups/395-5d062b4a-add-remove-and-update-tags-for-resou/providers/Microsoft.Network/virtualNetworks/vnet1",
  "identity": null,
  "kind": null,
  "location": "westus",
  "managedBy": null,
  "name": "vnet1",
  "plan": null,
  "properties": {
    "addressSpace": {
      "addressPrefixes": [
        "10.1.0.0/16"
      ]
    },
    "enableDdosProtection": false,
    "provisioningState": "Succeeded",
    "resourceGuid": "48583c22-680d-4ddf-97d8-1e25046de802",
    "subnets": [
      {
        "etag": "W/\"c51873be-773d-4b96-ab24-fa37389401fe\"",
        "id": "/subscriptions/9734ed68-621d-47ed-babd-269110dbacb1/resourceGroups/395-5d062b4a-add-remove-and-update-tags-for-resou/providers/Microsoft.Network/virtualNetworks/vnet1/subnets/subnet1",
        "name": "subnet1",
        "properties": {
          "addressPrefix": "10.1.1.0/24",
          "delegations": [],
          "ipConfigurations": [
            {
              "id": "/subscriptions/9734ed68-621d-47ed-babd-269110dbacb1/resourceGroups/395-5D062B4A-ADD-REMOVE-AND-UPDATE-TAGS-FOR-RESOU/providers/Microsoft.Network/networkInterfaces/WEBVM1-NIC1/ipConfigurations/IPCONFIG1",
              "resourceGroup": "395-5D062B4A-ADD-REMOVE-AND-UPDATE-TAGS-FOR-RESOU"
            }
          ],
          "networkSecurityGroup": {
            "id": "/subscriptions/9734ed68-621d-47ed-babd-269110dbacb1/resourceGroups/395-5d062b4a-add-remove-and-update-tags-for-resou/providers/Microsoft.Network/networkSecurityGroups/shared-nsg",
            "resourceGroup": "395-5d062b4a-add-remove-and-update-tags-for-resou"
          },
          "privateEndpointNetworkPolicies": "Disabled",
          "privateLinkServiceNetworkPolicies": "Enabled",
          "provisioningState": "Succeeded"
        },
        "resourceGroup": "395-5d062b4a-add-remove-and-update-tags-for-resou",
        "type": "Microsoft.Network/virtualNetworks/subnets"
      }
    ],
    "virtualNetworkPeerings": []
  },
  "resourceGroup": "395-5d062b4a-add-remove-and-update-tags-for-resou",
  "sku": null,
  "tags": {
    "CreatedBy": "MyName",
    "Dept": "IT",
    "Environment": "Production"
  },
  "type": "Microsoft.Network/virtualNetworks"
}
PS /home/cloud>