Azure Resource Manager (ARM) provides a collection of resource functions that can be used to reference your resource’s configuration and state in an ARM deployment template. In this blog post, I have collated some common use cases for these functions and provided some snippets for your reference.
If you haven’t created an ARM template before, I recommend taking a look at Microsoft’s guide to Create your first Azure Resource Manager template.
For quick reference, here’s a list of functions used by the snippets below:
- listKeys - returns the values for any resource type that supports the list operation.
- reference - returns an object representing a resource’s runtime state.
- resourceGroup - returns an object that represents the current resource group.
- resourceId - returns the unique identifier of a resource.
- subscription - returns details about the subscription for the current deployment.
Reference
The reference function gives you access to a resource’s runtime state and implicitly declares that one resource depends on another.
To see the property names and values for a resource type, Microsoft recommends creating a template that returns the object in the outputs section1. I also find it useful to browse existing resources in the Azure Resource Explorer and reference the JSON in the “data” tab. While this isn’t a perfect representation of the return type, in my experience the data shown is often the same as that returned by reference.
You can also output JSON with the Azure CLI.
Snippets
Get the location for the current resource group with resourceGroup:
"[resourceGroup().location]"Get the subscription ID for the current deployment with subscription:
"[subscription().subscriptionId]"Get the tenant ID for the current deployment with subscription:
"[subscription().tenantId]"App Service
Get an App Service host name with reference and resourceId:
"[reference(resourceId('Microsoft.Web/sites', parameters('app_name'))).hostNames[0]]"Generate a valid URL for an App Service with reference and resourceId:
"[concat('https://', reference(resourceId('Microsoft.Web/sites', parameters('app_name'))).hostNames[0], '/')]"Get the outbound public IP address for an App Service with reference and resourceId:
"[reference(resourceId('Microsoft.Web/sites', parameters('app_name'))).outboundIpAddresses]"Application Insights
Get the Instrumentation Key for Application Insights with reference and resourceId:
"[reference(resourceId('Microsoft.Insights/components', parameters('app_insight_name'))).InstrumentationKey]"Azure SQL DB
Generate an Azure SQL DB connection string with reference and resourceId:
"[concat('Data Source=tcp:', reference(resourceId('Microsoft.Sql/servers', parameters('server_name'))).fullyQualifiedDomainName, ',1433;Initial Catalog=', parameters('db_name'), ';User Id=', parameters('sql_username'), '@', parameters('server_name'), ';Password=', parameters('sql_Password'), ';')]"Storage Account
Generate an Azure Storage connection string with reference, resourceId and listKeys:
"[concat('DefaultEndpointsProtocol=https;AccountName=', parameters('storage_name'), ';AccountKey=', listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storage_name')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value)]"Azure Service Bus
Get the connection string for an Azure Service Bus namespace with listKeys and resourceId:
"[listKeys(resourceId('Microsoft.ServiceBus/namespaces/authorizationRules', parameters('namespaces_name'), 'RootManageSharedAccessKey'), providers('Microsoft.ServiceBus', 'namespaces').apiVersions[0]).primaryConnectionString]"