Extensions and Tips for deploying with Azure Resource Templates
Working with Azure Services in different subscriptions means that the Azure Services need to run in different subscriptions. This often occurs when we talk about DTAP environments (Development, Test, Acceptance and Production). Making sure the resources run on different subscriptions can be done by hand but that is very time-consuming task, this is where the Azure Resource Manager comes in.
Azure Resource Manager
The Azure Resource Manager is a technology within Azure that makes deployments and actions within Azure tools consistent. It is the layer that the tooling around Azure uses (Portal, Command Line and Visual Studio) to do tasks within, the “New” Azure Portal. This portal is fully build on top of the Azure Resource Manager.
Deployments of Azure services that have associations and the same lifetime are mostly put in the same Resource Group. A Resource Group is a logical container of Azure Services that you would like to manage together, deploying a complete Resource Group with multiple services needs to be done using the Resource Manager and Resource Templates.
Visual Studio
In this blog post we will not get into creating complete resource templates but we will mention how to get started together with Visual Studio. Within Visual Studio you get started by creating a project with the template called “Azure Resource Group”.
Creating a project with this template will give you three files:
- ResourceTemplate.parameters.json: Parameters file for the resource template.
- ResourceTemplate.json : The resource template.
- Deploy-AzureResourceGroup.ps1: PowerShell script that will get you started with deploying the resource template.
As mentioned above the “ResourceTemplate.json” file is the real resource template. In this file all Azure resources are defined but also the configurations of those Azure resources. Visual Studio offers a viewer for the resource template file called the “JSON Outline”.
As you can see in the above screenshot the “JSON Outline” displays the structure of the resource template.
Tip
Add a tag called “displayName” to the resource file in the Azure resource as in the below snip-it. With this tag the “JSON Outline” will use this name to display the resource in the viewer instead of the “name” property that is usually a non saying reference to a specific parameter.
{ "apiVersion": "[variables('defaultApiVersion')]", "name": "[parameters('api_Name')]", "kind": "api", "type": "Microsoft.Web/sites", "location": "[variables('location')]", "tags": { "displayName": "InternalAPI" }, "dependsOn": [ "[concat('Microsoft.Web/serverfarms/', parameters('hosting_Plan_Name'))]" ], "properties": { "name": "[parameters('api_Name')]", "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('hosting_Plan_Name'))]", "siteConfig": { "AlwaysOn": true } } }
PowerShell
The PowerShell added to the project is a great starting point to start with the deployment. But as mentioned above accounts are mostly associated with multiple Azure Subscriptions. The script does not support this situation and will always deploy to the default subscription that is associated with your account.
To support this scenario add a line of PowerShell to make sure that the user needs to login.
#Login to the Azure Resource Manager Login-AzureRmAccount
This line will open the login window to the Azure Resource Manager. Next we will retrieve all subscriptions the user is associated to.
Write-Host "---------------------------------------------------------------------" Write-Host "Your current subscriptions: " -ForegroundColor Yellow Get-AzureRMSubscription
The “Get-AzureRMSubscription” will display specific information about all the subscriptions the user is associated with. Information that is shown is the subscription id and subscription name. To set the context of the Azure Resource Manager to the right subscription the following lines need to be added.
Write-Host "Enter the Subscription ID to deploy to: " -ForegroundColor Green $sub = Read-Host Set-AzureRmContext -SubscriptionId $sub clear
The above lines will ask the user to fill in the subscription id of the subscription were they would like to deploy the Azure Resource Template. The function “Set-AzureRMContext” will then make sure that the context of the resource manager is set to the correct subscription.