When working with Terraform on Azure to manage resources, there are times when it’s necessary to get a reference to an Azure Subscription. This could be to reference the name or id attributes of the subscription, or even to get the tenant id for the subscription. Luckily, the azurerm Terraform Provider provides the azurerm_subscription data source that can be used for just this purpose.
Current Azure Subscription Reference
The most common time when an attribute of an Azure Subscription is needed to be used when setting the configuration on other resources using the azurerm Terraform Provider to manage Microsoft Azure resources. To do this, a data block referencing the Current Azure Subscription being targeted by the Terraform Project can be used.
The following is the simple data block to reference the Current Azure Subscription using the default azurerm Provider configured on the Terraform Project:
data azurerm_subscription "current" { }
Notice the data azurerm_subscription block doesn’t define any attributes to specify the Azure Subscription to reference. This is because the azurerm_subscription type will automatically grab the Azure Subscription ID set by the default azurerm Terraform Provider on the project. This makes the block extremely simple to define.
The previous example also names the resource current. This offers easier to read usage of calling the Azure Subscription reference to get attribute values for the Current Azure Subscription the Terraform Project is targeting.
The following are example Terraform expressions to grab the display_name and tenant_id of the Azure Subscription:
# Get Display Name of the Azure Subscription
data.azurerm_subscription.current.display_name
# Get subscription tenant ID / aka Azure AD Directory ID
data.azurerm_subscription.current.tenant_id
Azure Subscription Reference by ID
There are times when a single Terraform Project might be targeting multiple Azure Subscriptions, or if the project needs to grab attributes for a different Azure Subscription than the current Azure Subscription defined in the default azurerm Terraform Provider for the project. In these cases, the subscription_id attribute can be set in the data block to tell the azurerm Terraform Provider which Azure Subscription to get a reference for.
The following is an example on defining a data azurerm_subscirption block that points to a specific Azure Subscription by its ID (guid):
data azurerm_subscription "my_subscription" {
subscription_id = "00000000-0000-0000-0000-000000000000"
}
Be sure to replace the 00000000-0000-0000-0000-000000000000 placeholder with the actual Azure Subscription ID guid for the Azure Subscription to be referenced. Alternatively, a local. variable or a var. Input Variable could be used to set the Azure Subscription ID more dynamically within the project.
Happy Terraforming in Azure!
Original Article Source: Terraform: Get Azure Subscription Reference written by Chris Pietschmann (If you're reading this somewhere other than Build5Nines.com, it was republished without permission.)
Microsoft Azure Regions: Interactive Map of Global Datacenters
Create Azure Architecture Diagrams with Microsoft Visio
Unlock GitHub Copilot’s Full Potential: Why Every Repo Needs an AGENTS.md File
Azure Network Security Best Practices to Protect Your Cloud Infrastructure
New Book: Build and Deploy Apps using Azure Developer CLI by Chris Pietschmann



