Search
  • Nathan

Deploying Bicep Files Part 2 - AZ CLI

Updated: Aug 27

If you haven't already, please check out the previous parts of this series.


Deploying Bicep with AZ CLI

As mentioned in Part 1, the Az CLI is able to automatically download the Bicep binary for you. This means that the Az CLI can be used to deploy Bicep files. Let's go over this in detail.

 

az deployment group create

This command lets you deploy a Bicep file that has a targetScope set to resourceGroup. Remember, if your Bicep file does not have a targetScope line at the top, then by default, the value of resourceGroup is automatically used. Let's go over the options available with this command:

  • --resource-group <resourceGroupName>

  • Can be shortened to -g <resourceGroupName>

  • This specifies the Resource Group to deploy into. This must already exist

  • --subscription <subscriptionNameOrId>

  • This specifies the Subscription ID or Subscription Name that contains the Resource Group you're deploying to

  • This is optional. If omitted, the currently selected subscription is used. You can set your subscription using az account set -s <subscriptionNameOrId>

  • --mode <option>

  • <option> can be either Incremental or Complete

  • This is optional. If omitted, then Incremental is used

  • Incremental simply deploys the resources that are defined in your Bicep file. It doesn't care what other resources may reside in the Resource Group

  • Complete mode uses the Bicep file as the ultimate source of truth. Be careful with this option. If you have existing resources in the Resource Group, and those existing resources are not defined in your Bicep file, then those existing resources will be deleted. Likewise, if you remove a resource's code from your Bicep file, then that resource will be deleted from the Resource Group

  • --rollback-on-error --rollback-on-error <deploymentName>

  • When there's an error, roll back to the last successful deployment

  • Or, if you specify a deploymentName, you will roll back to that specific deployment on error

Note: Deployment data is automatically stored in the same Region as the Resource Group specified with the --resource-group parameter. There is no way to change this for the az deployment group create command.

 

az deployment sub create

This command lets you deploy a Bicep file that has a targetScope set to subscription. Let's go over the options available with this command:

  • --location <locationName>

  • Can be shortened to -l <locationName>

  • This is the region used to store data about your deployment

Wait, how do I specify which Subscription to deploy to? Well, the currently selected subscription will be used automatically. You can set your subscription using az account set -s <subscriptionNameOrId>

 

az deployment mg create

This command lets you deploy a Bicep file that has a targetScope set to managementGroup. Let's go over the options available with this command:

  • --location <locationName>

  • Can be shortened to -l <locationName>

  • This is the region used to store data about your deployment

  • --management-group-id <managementGroupID>

  • Can be shortened to -m <managementGroupID>

  • This specifies the Management Group to deploy to

 

az deployment tenant create

This command lets you deploy a Bicep file that has a targetScope set to tenant. Let's go over the options available with this command:

  • --location <locationName>

  • Can be shortened to -l <locationName>

  • This is the region used to store data about your deployment

Wait, how do I specify which Tenant to deploy to? Well, the currently selected tenant will be used automatically. You can set your tenant using az login --tenant <tenantID>

 

Options that are common to all 4 Az CLI commands

  • --name <deploymentName>

  • Can be shortened to -n <deploymentName>

  • This specifies the name for this deployment. You may need to reference this deployment in the future, so it can be beneficial to use good names here

  • This is optional. If omitted, then the name of the Bicep file is used as the name of the deployment

  • If you use the same deployment name over and over, then you will be restricted to viewing information about the latest deployment only. In other words, new deployments will overwrite the deployment data of old deployments if you use the same deployment name

  • --template-file <localFile.bicep>

  • Can be shortened to -f <localFile.bicep>

  • This specifies the local path to the bicep file you want to deploy

  • Either this or --template-uri is required

  • --template-uri <https://remote/file.bicep>

  • Can be shortened to -u <https://remote/file.bicep>

  • This specifies the URI to the bicep file you want to deploy

  • Either this or --template-file is required

  • --parameters <something>

  • Can be shortened to -p <something>

  • This let's you provide parameter values to your Bicep template.

  • There are multiple options for <something>:

  • Provide a local path to a parameters file

  • Provide a URI to a remote parameters file

  • Provide parameter key & value pairs directly on the command line

  • Multiple instances of --parameters can be used on the same command

  • Parameters are evaluated in order. If a value is assigned twice, then the last value wins

  • It is recommended to provide a parameters file first, and then override selectively using key & value pairs

  • --what-if

  • Can be shortened to -w

  • This will not deploy any resources. Instead, it will show you a summary of the changes that this template wants to make

  • This is similar to a terraform plan command

  • --confirm-with-what-if

  • Can be shortened to -c

  • This will first run a what-if (see above) and show you the results. Then, it will prompt you to accept the changes before continuing the deployment

  • --proceed-if-no-change

  • This is an optional parameter that can be used when you specify the --confirm-with-what-if parameter

  • This tells the deployment to automatically continue if the what-if result contains zero changes

 

Examples of minimal deployment commands

 

Well, that covers most of the basics of deploying Bicep files with AZ CLI. There are still a lot of details that I didn't expand upon in this article. For example, there are other ways to define your Bicep template, such as Template Specs and Template Objects. Make sure to read the Microsoft docs for more information.


The next part in this series will cover deploying Bicep files using the Az PowerShell Module.

 

My Bicep Deployment series:

61 views