Farrukh Normuradov
Farrukh Normuradov

Reputation: 1184

Setting up tags on resource creation stage through Azure CLI

One can create a resource in Azure through Azure CLI commands:

az functionapp create                               \
    --name                "function-name"           \
    --resource-group      "resource-group-name"     \
    --storage-account     "gl0unique0storage0name"

and later - as the next step - in a separate command set tags:

az resource tag --tags "environment=development"   \
    --name               "function-name"           \
    --resource-group     "resource-group-name"

this will set tag "environment" with the value "development".

If one enforces a policy to subscription to have a certain tag on every resource.
One must set tags on creation.
Documentation says that it is possible through ARM templates.

Q1 (complex): How can I set tags on any resource type creation step through Azure CLI?
Q2: Can I do this at least for Azure Function because documentation says that I can?

When I tried to followed docs - linked in Q2, with this command:

az functionapp create                               \
    --name                "function-name"           \
    --resource-group      "resource-group-name"     \
    --storage-account     "gl0unique0storage0name"  \
    --tags                "environment=development"

I got this error:
az: error: unrecognized arguments: --tags environment=development

Upvotes: 4

Views: 4096

Answers (1)

Joy Wang
Joy Wang

Reputation: 42043

Q1 (complex): How can I set tags on any resource type creation step through Azure CLI?

AFAIK, you can use the Azure Policy to do that, follow this doc: Apply tag and its default value.

Sample for CLI:

# Create the Policy Definition (Subscription scope)
definition=$(az policy definition create --name 'apply-default-tag-value' --display-name 'Apply tag and its default value' --description 'Applies a required tag and its default value if it is not specified by the user' --rules 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/built-in-policy/apply-default-tag-value/azurepolicy.rules.json' --params 'https://raw.githubusercontent.com/Azure/azure-policy/master/samples/built-in-policy/apply-default-tag-value/azurepolicy.parameters.json' --mode All)

# Set the scope to a resource group; may also be a subscription or management group
scope=$(az group show --name 'YourResourceGroup')

# Set the Policy Parameter (JSON format)
policyparam='{ "tagName": { "value": "costCenter" }, "tagValue": { "value": "headquarter" } }'

# Create the Policy Assignment
assignment=$(az policy assignment create --name 'apply-default-tag-value' --display-name 'Apply tag and its default value Assignment' --scope `echo $scope | jq '.id' -r` --policy `echo $definition | jq '.name' -r` --params "$policyparam")

Q2: Can I do this at least for Azure Function because documentation says that I can?

Yes, you can, the command looks fine, make sure you use the latest version CLI, or you can try the command below az functionapp create -g joywebapp -p joyplan -n joytest1111 -s joystoragev2 --tags 'environment=development', it works fine on my side.

Upvotes: 3

Related Questions