MeghP
MeghP

Reputation: 711

Create/Update tags in Azure using API

How do we create a new tag or update an already existing tag on a azure resource(VM, DB, function app). AWS has create_tags for instances, image ..Is there any such way in azure ?

Thanks

Upvotes: 0

Views: 3739

Answers (3)

user13798665
user13798665

Reputation: 11

Was trying to figure out how to update tags for a specific Azure resource for a few days, documentation was unclear and could not understand how to do this using solutions in this thread.

Figured it out eventually by running the powershell command to do the same operation(has better documentation with examples here and captured the API call using fiddler to figure out the format being used.

Updating the thread with my solution so that it is useful for others.

REST API documentation in the below link. I reached out to Azure support for help and they did not get back in the last 1 week.

https://learn.microsoft.com/en-us/rest/api/resources/tags/updateatscope

Example to add/edit a tag for a Virtual machine. Basically need to use the PATCH operation. the PUT operations replaces all existing tags.

Patch https://management.azure.com/subscriptions/>subscriptionId</resourceGroups/>resourcegrp</providers/Microsoft.Compute/virtualMachines/>VM Name</providers/Microsoft.Resources/tags/default?api-version=2019-10-01

$Body = 

{ 
  "operation": "merge", 
  "properties": { 
    "tags": { 
      "testtag": "testvalue", 
      "testtag2": "", 
      "existingtag": "updatedtagvalue" 
    } 
  } 
} 

Note: the operation "merge" is important for merging or editing tags. "Delete" for deleting specific existing tags and "replace" for replacing existing tags with new ones.

Upvotes: 1

Levi Lu-MSFT
Levi Lu-MSFT

Reputation: 30313

You can use Azure rest Api to create tags for azure resources as mentioned by 4c74356b41.

PUT https://management.azure.com/subscriptions/{subscriptionId}/resourcegroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{parentResourcePath}/{resourceType}/{resourceName}?api-version=2019-08-01

For below example I create a tag for my azure sql datebase using az rest cli.

az login --service-principal --username "<clientid>" --password "<clientpassword>" --tenant "<tenantid>"

$subscriptionId ="<subscriptionId >"
$resourceGroupName="<resourceGroupName>"

$uri = "/subscriptions/$subscriptionId/resourcegroups/$resourceGroupName/providers/Microsoft.Sql/servers/levi-sql-server/databases/levi_sql_database?api-version=2019-06-01-preview"

$body = '{\"tags\": {\"name\":\"firstdatabasetag\"},\"location\":\"westcentralus\"}'

az rest --method put --uri $uri --body $body

After tested I found some resource api may not support the latest api-version=2019-08-01. Above api to update the database tag only support api-version=2019-06-01-preview and earlier. But no need to wrong, you will get warning with the supported api versions if you used an unsupported version.

For other ways to make calls to azure api, you can refer to this blog.

There are other methods like azure powershell command(as mentioned by Sajeetharan) and az cli that allow you to use its update command to set the tags property.

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222552

Yes, you can do it in two ways.

(i) Using Powershell Cmdlet below to add a new tag or update a existed tag with new value.

Set-AzureRmResource -Tag @( @{ Name="tag_name"; Value="tag_value" }) -ResourceId <resource_id>

(ii) Use C#

using Microsoft.Azure.Management.Resources;
using Microsoft.Azure.Management.Resources.Models;       

//MyResourceOperation implemented interface IResourcesOperations 
MyResourceOperation resourceOpertion = new MyResourceOperation();

//Get a resource belonging to a resource group
Resource myResource = resourceOpertion.Get("resourceGroupName", "resourceProviderNamespace", "parentResourcePath", "resourceType", "resourceName", "apiVersion");

//update the assigned tag with a new value
myResource.Tags.Add("tagName", "updatedValue");

Upvotes: 2

Related Questions