Sergey
Sergey

Reputation: 391

How to get directory tenant name in Azure ARM template?

I need to use a tenant (directory tenant) name in my ARM templates (especially when creating Web Apps).
enter image description here It is possible to get subscription name using subscription().displayName however, how can I get my associated directory tenant name?
The expressions like [subscription().tenantId.displayName] or [subscription().tenantId.Name] aren't working and also I'm unable to find any presence of this property on the web.
The way I won't hardcode it is that it can be easily changed by subscription owner or account admin that's why I'm looking for some existing variable\parameter\etc

Upvotes: 3

Views: 12073

Answers (3)

twichy1983
twichy1983

Reputation: 1

You could run a deployment script as your first resource. Then, in powershell write the tenant name as a tag to the resource group your arm template is deploying to. Youll need to create a managed identity in the deployment too.

https://learn.microsoft.com/en-us/azure/templates/microsoft.managedidentity/userassignedidentities?pivots=deployment-language-arm-template

https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/deployment-script-template

Connect-AzAccount -identity
$TN = (Get-AzTenant).Name
$Tags += @{"TenantName"=$TN}
Set-AzResourceGroup -Name <your-resource-group-name> -Tag $Tags

Then in you arm template, just reference

"[resourceGroup().tags.TenantName]"

Upvotes: 0

daviesdoesit
daviesdoesit

Reputation: 843

The tenantId is now available with template functions. Use the subscription function: subscription().

Try the following output examples for reference:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [],
    "outputs": {
        "subscriptionOutput": {
            "value": "[subscription()]",
            "type" : "object"
        }
    }
}

For the tenant id:

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [],
    "outputs": {
        "subscriptionOutput": {
            "value": "[subscription().tenantId]",
            "type" : "object"
        }
    }
}

Reference:

https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-resource#subscription

Upvotes: 14

4c74356b41
4c74356b41

Reputation: 72191

There is no way to do this. you can only get these values back:

{
    "id": "/subscriptions/{subscription-id}",
    "subscriptionId": "{subscription-id}",
    "tenantId": "{tenant-id}",
    "displayName": "{name-of-subscription}"
}

reference: https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-functions-resource#subscription

Upvotes: 2

Related Questions