Do more or less with default values in the Azure CLI

I've been spending quite a lot of time using Azure over the last many years. In fact, the portal was written in Silverlight. Now the the portal has a great modern UI and complimenting that is a CLI (command line interface) allowing for great automation, grep, awk, jamespath and much more.

Checkout Getting Started with Azure CLI 2.0

When using the CLI to create resources there are occasions, usually when putting together a project or demo, when there are parameters that should be assumed or defaulted.

In this scenario, the following resources must be created for the application architecture.

  • Resource Group
  • AppService Plan
  • AppService Web Application
  • DocumentDB

Creating a Resource Group using the CLI is simple using the following command:

az group create --name vaderappgroup --location "East US"  

However, each part of the architecture requires --location and typing that each time seems a waste of keystrokes.

Beyond creating the group, the other items also require passing the group name hence more unneeded typing.

Azure CLI Defaults.

Looking at the az configure -h information, setting the defaults are key/value pairs.

$ az configure -h

Command  
    az configure: Configure Azure CLI 2.0 Preview or view your configuration. The command is
    interactive, so just type `az configure` and respond to the prompts.

Arguments  
    --defaults : Space separated 'name=value' pairs for common arguments defaults, e.g. '--defaults
                 group=myRG web=myweb vm=myvm'. Use '' to clear the defaults, e.g. --defaults vm=''
                 web=''.

Let's set the defaults for this scenario.

az configure --defaults location="East US" group="vaderappgroup"  

To check the defaults you have configured, run az configure and look at the section [defaults]

[defaults]
location = East US  
group = vaderappgroup  

When creating any resource using the CLI, if the option is not specified it will use the default if available. To clear any default run az configure --defaults <key>=''

Creating our scenario

Now that the default location and resource group is configured, we can create the application with a little less typing.

Create the resource group

uses --location "East US" from defaults

az group create -n vaderappgroup  

Create the app service plan

Uses --location "East US" and --resource-group vaderappgroup from defaults

az appservice plan create -n vaderserviceplan --sku FREE  

Create web application

Uses --location "East US" and --resource-group vaderappgroup from defaults

-p|--plan : App Service Plan is required

az appservice web create -n vaderwebapp -p vaderappplan  

Creating the DocumentDB

Uses --resource-group vaderappgroup from defaults, location for DocumentDB defaults to region of the resource group unless specified using --locations. Use az documentdb create -h for more information.

Note: If you want to use MongoDB add --kind MongoDB.

az documentdb create -vaderappdb  

Resources

Checkout the video below on Azure Command Line Interface with Jason Shaver

Tweet Post Share Update Email RSS

Hi, I'm Shayne Boyer, I write this site, work on ASP.NET Core content and Open Source, speak at national and community events while helping teams architect web and cloud applications.

Tags:
azure azure-cli azuredev