Step by Step ARM Templates - Deploying Template Using Azure PowerShell

@20aman    Sep 11, 2016

Index of all blogs in this Step by Step ARM Templates series is located here: Step by Step Azure Resource Manager (ARM) Templates - Index

In the previous blog you learned How to build your first ARM Template . Now that you have a fully functional ARM template we want to deploy this template to Azure.

There are various options to deploy a template to Azure. We already saw in the last blog How to deploy template using Azure Portal. Now we will look at Azure PowerShell as more programmatic and automated way to deploy the template.


Things you should know before deployment

  1. Azure PowerShell - This should be installed on the machine from where the Steps will be followed. If you don't have this then use this link to get it: Get Azure PowerShell
  2. Azure Subscription - where you want to deploy your template
  3. Resource Group - This is the resource group in Azure where you will be deploying your template. You can create a new resource group (for the resources that will be deployed by the template) or use an existing one.
  4. Parameters - Value of the input parameters to the template should be known to you for the deployment. Follow all your naming conventions when defining the parameters for deployments of resources in Azure.
  5. Internet Connectivity - This should be present on the machine from where the Steps will be followed for connectivity to Azure

Steps for Deployment

  • First, launch a PowerShell window as an Administrator
  • Then, log into the Azure account.

Run the below cmdlet to log into Azure:

  • Select appropriate Azure Subscription

You have two choices here. You can either use below cmdlet to use Subscription ID

Set-AzureRmContext -SubscriptionID <YourSubscriptionId>

Or you can use the Subscription name with the below cmdlet:

Select-AzureRmSubscription -SubscriptionName "<Your Subscription Name>"
  • Next, if you already have a resource group to which you want to deploy the template then skip this step. Else create a new resource group. A resource in Azure ARM architecture can only exist in a resource group.

Use below cmdlet to create a new Resource Group:

New-AzureRmResourceGroup -Name TestResourceGroup01 -Location "Central US"
  • Before deploying the Resource Template to Azure, you should Test it. This step is optional but highly recommended.

Use the below cmdlet to test and validate your template:

Test-AzureRmResourceGroupDeployment -ResourceGroupName TestResourceGroup01 -TemplateFile <PathToJsonTemplate>
  • Now comes the last step i.e. to deploy the template. You have two options when deploying the template. You can either deploy a template without any parameters (if none are required) or you need to specify the parameters. Let's check both these options next.

Deploying Template which doesn't need Parameters

You can deploy such template using New-AzureRmResourceGroupDeployment cmdlet. If the template file is on a local directory then use the below cmdlet:

New-AzureRmResourceGroupDeployment -Name ExampleDeployment -ResourceGroupName TestResourceGroup01 -TemplateFile <PathToTemplate>

If the template file is uploaded to some hosted location and is accessible via a link, then use the below cmdlet to deploy the template:

New-AzureRmResourceGroupDeployment -Name ExampleDeployment -ResourceGroupName TestResourceGroup01 -TemplateUri <LinkToTemplate>

Deploying Template with Parameters

Deploying of the template is exactly similar as the previous section. You use the same cmdlet. To specify the parameter, you have 4 options. Use the below cmdlets for the option you want to use.

Option 1 - Using Inline Parameter

New-AzureRmResourceGroupDeployment -Name ExampleDeployment -ResourceGroupName TestResourceGroup01 -TemplateFile <PathToTemplate> -myParameterName "parameterValue" -secondParameterName "secondParameterValue"

Option 2 - Using Parameter Object

$parameters = @{"<ParameterName>"="<Parameter Value>"}
New-AzureRmResourceGroupDeployment -Name ExampleDeployment -ResourceGroupName TestResourceGroup01 -TemplateFile <PathToTemplate> -TemplateParameterObject $parameters

Option 3 - Using Parameter file which is in local environment

New-AzureRmResourceGroupDeployment -Name ExampleDeployment -ResourceGroupName TestResourceGroup01 -TemplateFile <PathToTemplate> -TemplateParameterFile <PathToParameterFile>

Option 4 - Using Parameter file which is located externally and can be referenced via Link

New-AzureRmResourceGroupDeployment -Name ExampleDeployment -ResourceGroupName TestResourceGroup01 -TemplateUri <LinkToTemplate> -TemplateParameterUri <LinkToParameterFile>

Key Gotchas

  1. If you provide values for a parameter in both the local parameter file and inline, the inline value takes precedence.
  2. You cannot use inline parameters with an external parameter file. All inline parameters are ignored when you specify "TemplateParameterUri" parameter.
  3. As a best practice, do not store sensitive infomation in the parameters file e.g. Local admin password. Instead either provide these dynamically using inline parameters. Or store them using the Azure Key vault and then reference the key vault in your parameters file.

You can find more details about these cmdlets here: Deploy resources with Resource Manager templates and Azure PowerShell

In the next blog, we will see how to create a Parameters File for providing parameters dynamically to the template.

Comments powered by Disqus