PowerShell provides additional comments to check the validity of the templates before actual deployment.
The Test-AzureRmResourceGroupDeployment cmdlet can be used to test the validity of a template before deployment. Please note that this cmdlet cannot catch runtime configuration, naming, and dependency issues between resources; however, it will catch and report issues if there are syntax issues or if the grammar does not follow the rules of ARM templates. It is recommended to use this cmdlet before actual deployment:
Test-AzureRmResourceGroupDeployment -ResourceGroupName "TestRG" -Mode Incremental -TemplateFile "C:\myARMTemple.json" -TemplateParameterFile "C:\myARMTemplateParameters.json" -Verbose
ResourceGroupName is the name of the target resource group for deployment. Mode determines Incremental or Complete deployment, TemplateFile refers to the location of the ARM template on the local machine, TemplateParameterFile refers to the location of the ARM template parameters file, and it is good to use the Verbose switch to get more meaningful information of testing the deployment artifacts. It is mandatory to provide values for the ResourceGroupName and TemplateFile cmdlet parameters; the rest of them are optional.
You can deploy a template using the New-AzureRmResourceGroupDeployment cmdlet. This cmdlet has multiple property sets, which means that it can be used to deploy templates that are stored on a local machine, stored remotely on Azure Storage Account, or stored at any other location that can be referenced using HTTP URL. It supports both Incremental as well as Complete deployment. It also accepts parameters by way of the parameters file or by way of explicit parameters. The code listing shown next shows the command for deploying a template in incremental mode by supplying both the template and its parameters file:
New-AzureRmResourceGroupDeployment -Name "TestDeploy1" -ResourceGroupName "TestRG" -Mode Incremental -TemplateFile "C:\myARMTemple.json" -TemplateParameterFile "C:\myARMTemplateParameters.json" -Verbose
The parameters for a new cmdlet is almost like the Test cmdlet. The only difference is the addition of a new Name parameter and deployments are identified using this parameter.
It is important to note that a resource group must exist before a template can be deployed. This is because a template needs a resource group to be available as a prerequisite. Template deployment will not create a resource group automatically.
ARM template parameters can be explicitly provided with cmdlet. This cmdlet generates dynamic parameters for accepting these templates that is defined in parameters.
The script for deploying a template without a parameters file is shown next:
- Create a new Resource group using the New-AzureRmResourceGroup cmdlet and providing the Name and Location of the Resource group. You should have been authenticated to Azure using the Login-AzureRmAccount cmdlet before running any of the following Azure PowerShell cmdlet:
- The template is tested for correctness using the Test-AzureRmResourceGroupDeployment cmdlet:
- The template is deployed using the New-AzureRmResourceGroupDeployment cmdlet:
An interesting aspect of this template deployment is that it outputs custom name-value pairs as defined within the Outputs section of the ARM template. The values of these are derived at runtime and returned to the caller.