Azure CLI Tutorial with Examples.

In this tutorial, we will learn about Azure CLI with examples.  Azure CLI is a command-line tool to manage Azure cloud resources.

Azure CLI, Powershell, Portal, and SDK are the Management tools, all these will use Azure Resource Manager(ARM) API to manage the azure resources.

 

 

Azure CLI will run on Mac, Linux, and windows.

Azure CLI Installation.

Installation on Mac OS Mojave.

To install Azurecli on Mac execute below command on Mac terminal. The Azure CLI has a dependency on the Homebrew python3 package and will install it.

brew update && brew install azure-cli

 

Installation on Ubuntu 18.04

Execute the below script which is provided by Microsoft to install the Azure CLI package for Ubuntu.

techies:~$ curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash

Installation is completed.

Next, we will start learning about the commands. I hope you all have an Azure cloud account.

To log in to this account from using CMD execute below Azure CLI command.

az login

You will get a message like below.

“To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code GWQU4NYNW to authenticate.” (Please use the code that you received ).

Use your Azure cloud credentials to log in to the account.  Once you logged in, you can see all the subscription names in the terminal.

MacBook-Pro:~ $ az login
You have logged in. Now let us find all the subscriptions to which you have access...
[
  {
    "cloudName": "AzureCloud",
    "id": "c233fdf-444-33d-ccc4-3c222111000",
    "isDefault": false,
    "name": "DEVELOPMENT",
    "state": "Enabled",
    "tenantId": "a111a-b222b-c3333c-d444d-e5555e",
    "user": {
      "name": "techies",
      "type": "user"
    }
  }
]

To see all the subscriptions in a table execute below command. The argument “-o table” shows the output in a table format. If we haven’t mentioned it explicitly then it prints in JSON format.

az account list -o table

To list all the resource groups under the subscription execute the below command.

MacBook-Pro:~ $ az group list
[
  {
    "id": "/subscriptions/424derfdf343edsf-/resourceGroups/dashboards",
    "location": "eastus",
    "managedBy": null,
    "name": "dashboards",
    "properties": {
      "provisioningState": "Succeeded"
    },
    "tags": null,
    "type": "Microsoft.Resources/resourceGroups"
  }
]

To start working with Azure CLI we will use a subscription so as to set it, execute the below command.

$ az account set -s "Subscription name"  {Enter your subscription name}

Azure CLI Syntax

This is the syntax of Azure CLI. It will help to form complex queries.

az group subgroup command arguments

Group/ subgroup: The service or topic of concern (e.g. Network)

Command: The action to perform ( Create or delete)

Arguments: Imported and related values (e.g. name, size)

 

Resource creation with Azure CLI

Here we will create a few Azure cloud resources with the help of CLI. I recommend you to use the “help” argument if you stuck in the middle.

  • Create a resource group.
 az group create --name Devops-test  --location eastus
{
  "id": "/subscriptions/954c543-f666-44b6-aa98-1111/resourceGroups/Devops-test",
    "location": "eastus",
    "managedBy": null,
    "name": "Devops-test",
    "properties": {
      "provisioningState": "Succeeded"
    },
    "tags": null,
    "type": null
  }

Name and location arguments are mandatory, if you miss any of these then you will get an error like below. Also, location can be added with argument ‘-l’ but we have to add location name in quotes e.g az group create –name Devops-test  -l “Australia southeast”

“az group create error: the following arguments are required: –location/-l “

  • Create a storage account

az storage account create --name devopsteststorage1122 -g Devops-test

This will create a storage account named “devopsteststorage1122” under the resource group “Devops-test”. The command will generate output with all details like Location, Primary endpoints, Network rules, access tier, etc.

  •  Create a virtual network

az network vnet create -g Devops-test -n TC_vnet --address-prefix 10.4.0.0/16

This will create a virtual network named TC_vnet under the resource group “Devops-test” with the IP address space 10.4.0.0/16. Please use an IP address space that doesn’t exist.

Command formation with the help of the argument.

Remembering all the commands and its argument is difficult, so to form a complete command I have followed the below steps. Here I used the above VNET creation command.

Logged in to Azure with az login and set my subscription with account set -s "Subscription name" so all the resources created will be present in this subscription.

To create VNET I executed az -h command and then it is listed a lot of argument in alphabetical order. From this, I choose the “network” argument.

 

Next, execute az network -h to get details about the next argument and I continued the same.

az network vnet -h

 

From the az network vnet, we will get two arguments one is “create” and the other is “list”. The list argument will help to list all vnet and all the associated IP address present in the resource group. The -o will helps to print the output in table format.

MacBook-Pro:~ $ az network vnet list -o table
Name      ResourceGroup   Location    NumSubnets     Prefixes 
-------------------   --------------- ---------- ------------ -------
myVNet      Devops-test      eastus         2      10.0.0.0/16 
vnet_stg    Devops-test      eastus         1      10.3.0.0/16

 

From the output of  “az network vnet create -h” I found the name (-n) and resource group (-g) is required to create a VNET. Also, I followed the example they provided to form my query.

az network vnet create -g Devops-test -n TC_vnet --address-prefix 10.4.0.0/16

 

  • Create a subnet

We have created a VNET, now we can create subnet inside this VNET, execute the below command to create a subnet. To eliminate the complexity of the command we can break down the command in multiple lines with the help of backslash (\).

MacBook-Pro:~ $ az network vnet subnet create \

> -n public_subnet \

> --vnet-name TC_vnet \

> -g  Devops-test \

> --address-prefixes 10.4.1.0/24

Here we haven’t added backslash at the end so it means the command is completed. To confirm the subnet is created look at the output and you can see a message like below.

"provisioningState": "Succeeded",

Log in to the Azure portal to confirm the same Home -> Virtual networks -> Subnets.

We have now a VNET and subnet. Next, we will create a VM inside this subnet. Go to the next page.

 

VM creation with the az command

Execute the below command to create a virtual machine under the TC_vnet VNET and public_subnet.

MacBook-Pro:~ $ az vm create \
> --resource-group MDC_DEVOPS_EUS \
> --name MyVM  \
> --image UbuntuLTS \
> --admin-username adminusr \
> --admin-password 'TC@#$!@09&&ADDF' \
> --size Standard_B2ms \
> --public-ip-address MyVMIP \
> --nsg MyVMNSG \
> --vnet-name TC_vnet \
> --subnet public_subnet
{
  "fqdns": "",
  "id": "/subscriptions/11197-5b2-94ce-501d20/resourceGroups/Devops-test/providers/Microsoft.Compute/virtualMachines/MyVM",
  "location": "eastus",
  "macAddress": "00-0D-3A-1A-0A-6E",
  "powerState": "VM running",
  "privateIpAddress": "10.4.1.4",
  "publicIpAddress": "xx.xx.xx.xx",
  "resourceGroup": "Devops-test",
  "zones": ""
}
MacBook-Pro:~ $

The command will create a virtual machine named MyVM. If we haven’t mentioned any location the VM will be created in the Resource group default location.

 

 

Leave a Reply

Your email address will not be published.