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.

 

image

 

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”.

Azure Resource Group Template

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”.

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.

Related Posts

Listing Azure Services within a CSV file In some situations you will look into a current Azure Environment and the setup/governance of it and need to migrate or move resources around. The ...
Azure Event Grid with Custom Events As of yesterday (16-8-2017) the public preview of Azure Event Grid is live. Azure Event Grid is a fully managed event routing service. Azure Event Gri...
Removing the Classis Hybrid Connections from Azure (Azure BizTalk Service) As you know the classic hybrid connections that are build upon Azure BizTalk Services are deprecated. These connection will have to be replaced by the...
Restricting access to your Azure Web Application As you may know almost everything that is deployed to Azure is publicly available. As with Azure SQL Database you do not have a firewall available for...
Configure access to a private network for a Azure App Services On-Premise connections for Azure App Services can be created by using Hybrid Connections. Hybrid connections do not need any development or re-configu...
Part 2 – Azure API Application to query the Azure SQL Database This post is the second in a series of three posts and will help you with the creation of identity pass-through authentication from a client applicati...

Leave a Reply

Your email address will not be published. Required fields are marked *