Deploying resources across subscriptions and resource groups

In the last section, resource groups were created as part of ARM templates and this is a relatively new Azure feature. Another new Azure feature is provisioning of resources into multiple subscriptions simultaneously from a single deployment using a single arm template.  In this section, we will provision a new storage account into two different subscriptions and resource groups. The person deploying the ARM template would select one of the subscriptions as base subscription using which they would initiate the deployment and provision the storage account into the current and another subscription. The prerequisite for deploying this template is that the person doing the deployment should have access to at least two subscriptions and that they have contributor rights on these subscriptions. The code listing is shown here and available in CrossSubscriptionStorageAccount.json file within the accompanied code.

{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storagePrefix1": {
"type": "string",
"defaultValue": "st01"
...
"type": "string",
"defaultValue": "rg01"
},
"remoteSub": {
"type": "string",
"defaultValue": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
...
}
}
],
"outputs": {}
}
}
}
],
"outputs": {}
}

It is important to note that the names of the resource group used within code should already be available in respective subscriptions. The code will throw error in case the resource groups are not available. Moreover, the names of the resource group should exactly match in arm template.

The code for deploying this template is shown next. In this case, we use New-AzureRmResourceGroupDeployment because the scope of deployment is a resource group. The deployment script is available in CrossSubscriptionStorageAccount.ps1 file:

New-AzureRmResourceGroupDeployment  -TemplateFile "<< path to your CrossSubscriptionStorageAccount.json file >>" -ResourceGroupName "<<provide your base subscription resource group name>>" -storagePrefix1 <<provide prefix for first storage account>> -storagePrefix2 <<provide prefix for first storage account>> -verbose