Getting Started with Azure CLI

karnatisrinivas

Srinivas karnati

Posted on March 4, 2022

Getting Started with Azure CLI

What is Azure CLI?

The Azure Command-Line Interface (CLI) is a command-line tool which connect to Azure and execute administrative commands on Azure resources. It allows the execution of commands through a terminal using interactive command-line prompts or a script.

How to install the Azure CLI on Macos?

Azure CLI is available to install in Windows, macOS and Linux environments. It can also be run in a Docker container and Azure Cloud Shell.

We can install Azure CLI on MacOS using Homebrew package manager. Homebrew makes it easy to keep your installation of the CLI update to date. It is always a best practice to update the homebrew first and install the packages.

brew update 
brew install azure-cli
Enter fullscreen mode Exit fullscreen mode

Once the dependencies are installed, you can check the version of Azure CLI using

az --version
Enter fullscreen mode Exit fullscreen mode

Sign into Azure CLI

  • To sign into the Azure, Run the login command
az login
Enter fullscreen mode Exit fullscreen mode

If the CLI can open your default browser, it will do so and load an Azure sign-in page.

Otherwise, open https://aka.ms/devicelogin on your browser and enter the authorization code displayed in your terminal.

  • Sign in with your account credentials in the browser.

Azure CLI Examples

To see subscriptions in your Azure account, we can use account as follows:

az account list 

\\ We can also use account show

az account show
Enter fullscreen mode Exit fullscreen mode

To see resources that are created in your azure account, we can use resource command:

az resource list
Enter fullscreen mode Exit fullscreen mode

find and --help

To search for commands, use az find. For example, to search for command names containing network, use the following command:

az find network
Enter fullscreen mode Exit fullscreen mode

Use the --help argument to get a complete list of commands and subgroups of a group. For example, to find the CLI commands for working with account we use:

az account --help
Enter fullscreen mode Exit fullscreen mode

Create a Virtual Network using Azure CLI

Virtual networks (VNets) is something that allows Azure resources to communicate securely with each other. VNets can also be connected to other VNets if their address ranges don't overlap.

Use the az network vnet create command to create a virtual network named VNet1 with address prefix of 10.1.0.0/16 and a subnet named Subnet1 with address prefix of 10.1.1.0/24.

az network vnet create \
 --name "vnet1" \
--resource-group "my_learning" \
--address-prefixes 10.1.0.0/16 
--subnet-name "subnet1" \
--subnet-prefixes 10.1.1.0/24

Enter fullscreen mode Exit fullscreen mode

You can also use shell variables to pass the values like as follows:


vnetName=vnet1
subnetName=subnet1
vnetAddressPrefix=10.1.0.0/16
subnetAddressPrefix=10.1.1.0/24

az network vnet create \
  --name $vnetName \
  --resource-group $resourceGroup \
  --address-prefixes $vnetAddress \
  --subnet-name $subnetName \
  --subnet-prefixes $subnetAddress

Enter fullscreen mode Exit fullscreen mode

Once you run the commands mentioned earlier, upon successful execution you will be able to see the vnet created in the resources list.

In the Terminal :

Screenshot 2022-03-04 at 7.21.22 PM.png

In the Portal:

Screenshot 2022-03-04 at 7.23.20 PM.png

Create a Virtual Machine (VM) using CLI

We use the az vm create command to create a new virtual machine running Ubuntu, which uses SSH authentication for login, and is connected to the subnet and VNet we created just now.

az vm create --name "myVM1" --resource-group "my_learning" 
--image UbuntuLTS --vnet-name "vnet1" 
--subnet "subnet1" --generate-ssh-keys
Enter fullscreen mode Exit fullscreen mode

Upon successfull execution, you should be able to see the following output which displays all information you need to access the VM.

Screenshot 2022-03-04 at 7.34.06 PM.png

Confirm that the VM is running by connecting over SSH.

ssh <public IP address>
Enter fullscreen mode Exit fullscreen mode

Go ahead and log out from the VM by typing exit.

Screenshot 2022-03-04 at 7.36.52 PM.png

Note: Always Clean up the resources which are not in use to save billing costs.

**Connect with me on: **





💖 💪 🙅 🚩
karnatisrinivas
Srinivas karnati

Posted on March 4, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Getting Started with Azure CLI
azure Getting Started with Azure CLI

March 4, 2022

Create a Virtual Network on Azure
azure Create a Virtual Network on Azure

February 20, 2022