Last Update: Sep 04, 2024 | Published: Jan 20, 2017
This “how to” post will show you how to deploy a JSON template in Azure using PowerShell and a parameters JSON file.
The JSON template that I will use has been built up using a series of previously published posts:
The JSON template that was built in the above posts, MultipleResources.JSON, includes several parameters that are used to customize the deployment of:
Some of these parameters require completion and some include default values that you might want to override:
We will use the New-AzureRmResourceGroupDeployment cmdlet to send the JSON file to Azure and start the deployment. There are two ways that we can manipulate the parameters of the JSON using this cmdlet:
I’m going to use a JSON parameter file, which I’ve shared below, to customize the deployment of the JSON template. I created this file by copying the parameters section from the JSON template that we want to deploy into a new JSON file in VS Code. Each parameter has a single setting, called value. I simply renamed the original defaultValue setting to value, edited the parameter value that was being passed to override the default settings, and deleted the other unnecessary settings from each parameter.
{ "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "Storage Account Name": { "value": "sapetri2abcd" }, "Storage Account Resiliency": { "value": "Standard_LRS" }, "Network Name": { "value": "nw-petri2" }, "Network Address": { "value": "10.10.0.0/16" }, "Subnet-1 Name": { "value": "sn-petri2-01" }, "Subnet-1 Address": { "value": "10.10.0.0/24" }, "Subnet-2 Name": { "value": "sn-petri2-02" }, "Subnet-2 Address": { "value": "10.10.1.0/24" }, "Availability Set Name": { "value": "as-petri2-01" } } }
The above parameters file was saved as Parameters.JSON, which must not be larger than 64KB.
All deployments of a JSON template are done to a resource group; this can be an existing or a new resource group. If you are using a new resource group, then you must create that resource group. But before we do that, we need to sign into Azure and select the correct subscription.
We are working with ARM, so we’ll be using the ARM PowerShell cmdlets, identifiable by the “RM” in the noun part of the cmdlets. We will log into Azure as follows:
Login-AzureRmAccount
A window will appear; sign into this window with your assigned credentials for the desired Azure subscription. Then we will retrieve the list of subscriptions that this account has access to:
Get-AzureRmSubscription
Copy the SubscriptionID of the desired subscription and use it in the below cmdlet to select the subscription:
Select-AzureRmSubscription -SubscriptionId <Your subscription ID>
Now we can create a resource group if a new one is required, and store details of the resource group in a variable called $rg:
$rg = New-AzureRmResourceGroup -Name "rg-petri2" -Location "East US"
Alternatively, if we are deploying the template to an existing resource group, we can store the details of that resource group in a variable called $rg:
$rg = Get-AzureRmResourceGroup -Name "rg-petri2"
Now I can execute the deployment and use the parameters JSON file to configure the deployment:
New-AzureRmResourceGroupDeployment -Name FirstDeployment -ResourceGroupName $rg.ResourceGroupName -TemplateFile C:TempMultipleResources.json -TemplateParameterFile C:Tempparameters.json
Note that the deployment is given a name; this is because you can do multiple deployments into a resource group using the same JSON template or a combination of JSON templates, and you’ll need the ability to track your deployments.