Author – Shefali Kamble, Associate Cloud Engineer
Microsoft has introduced the preview of ARM Template what-if feature which was a private preview initially but lately, this new option for PowerShell has been made available as a public preview which is a great option and can help far most with the template deployment.
What is the What-if feature?
This feature holds the capability of letting you know what your deployment would look like but doesn’t attempt to change anything unless confirmed. It is like getting an overview of the deployment. When a lot of resources are deployed through a template, it is easy to overlook something or have a typo somewhere. Especially with templates running in complete mode, this can cause you to accidentally remove resources you wanted to keep or deploy the wrong resources.
The what-if option is still in preview, which means the output is not perfect yet. There can be noise in the output, which means the output says something has changed when it didn’t. This is something to keep in mind when using it.
Pre-requisites:
- To use what-if in PowerShell, you must have version 2 or later of the Az module.
- To use what-if in Azure CLI, you must have Azure CLI 2.5.0 or later.
Note: PowerShell Core (6.x or 7.x) is required. You can’t install the required module on PowerShell 5.x or earlier.
What-if commands:
Using Azure PowerShell:
In order to use the what-if parameter in PowerShell and preview the changes that will take place before the actual deployment, run the following commands and append the what-if parameter to it
New-AzResourceGroupDeployment for resource level deployment
New-AzSubscriptionDeployment for subscription level deployment
- New-AzResourceGroupDeployment -Whatif
- New-AzSubscriptionDeployment -Whatif and New-AzDeployment -Whatif
You can -Confirm the changes after the preview by appending the -confirm parameter to continue with the deployment.
- New-AzResourceGroupDeployment -Confirm for resource group deployments
- New-AzSubscriptionDeployment -Confirm and New-AzDeployment -Confirm for subscription level deployments
Using Azure CLI:
Similarly, to use the what-if parameter in Azure CLI and preview the changes that will take place before the actual deployment, run the following commands and append the what-if parameter to it
az deployment group for resource-level deployment
az deployment sub for subscription level deployment
- az deployment group what-if
- az deployment sub what-if
You can -Confirm the changes after the preview by appending the –confirm-with-what-if parameter to continue with the deployment. Add this switch to az deployment group create or az deployment sub create.
- az deployment group create –confirm-with-what-if or -c for resource group deployments
- az deployment sub create –confirm-with-what-if or -c for subscription level deployments
Understanding the format of what-if:
There are types of changes that take place during the template deployment. Let’s understand them one by one
- Create – the new resource that has to be deployed is mentioned in the template
- Delete – the resource that needs to be deleted. It isn’t defined in the template but works on only those resources that fall under the complete mode category
- Ignore – the resource that already exists won’t be deployed or modified again
- NoChange – the resource defined in the template already exists will be redeployed again but the properties will remain same
- Modify – the resource defined in the template already exists will be redeployed again and the properties will also change
- Deploy – the resource defined in the template already exists will be redeployed again but the properties may or may not change. The operation returns this change type when it doesn’t have enough information to determine if any properties will change
Let’s understand the working with a demo:
Open PowerShell 7 with “run as administrator” and login to azure with the command Connect-Azaccount.Copy the link and paste it in the browser, Insert the code and select your account to Login.
Your subscription will be displayed after the login.
Now let’s create a resource group in which all the deployment would take place so that the resources are placed together.To test the what-if parameter deploy a virtual network with 2 subnets using the following syntax.The bottom line shows that 3 resources are to be added and the words in green lists all the resources that will be deployed without actually deploying it unless confirmed.
In order to deploy these resources confirm the deployment with the following syntax. It will prompt for confirmation to proceed with the deployment, type Y for yes and hit enter.It will list all the resources that were deployed.To verify the deployments head towards the Azure Portal and check the Resource Group:
I hope this will help you to perform Dry runs with ARM templates. You can post your thoughts in the comment section.