Post

Azure | Azure CLI Introduction and Usage

Azure CLI (Command-Line Interface) is a powerful tool for managing Azure resources from the command line. It provides a set of commands for interacting with Azure services and resources, allowing users to automate tasks, deploy resources, and manage infrastructure efficiently. In this guide, we’ll explore the basics of Azure CLI and how to use it with detailed examples.

Installation

Before using Azure CLI, you need to install it on your system. Azure CLI is available for various operating systems, including Windows, macOS, and Linux. You can follow the instructions provided in the official documentation for installation.

Authentication

After installing Azure CLI, you need to authenticate with your Azure account to access Azure resources. You can log in using the az login command, which will open a browser window for authentication.

1
az login

Once authenticated, you can start using Azure CLI to manage your resources.

Most Useful Azure CLI Commands with Detailed Examples

Here are 30 of the most useful Azure CLI commands along with detailed examples:

1. az login

  • Description: Authenticates to Azure and allows you to access Azure resources.
  • Example:
    1
    
    az login
    

2. az account list

  • Description: Lists all Azure subscriptions associated with the logged-in account.
  • Example:
    1
    
    az account list --output table
    

3. az account set –subscription <subscription_id>

  • Description: Sets the active subscription for the current session.
  • Example:
    1
    
    az account set --subscription <subscription_id>
    

4. az group create –name <resource_group_name> –location <location>

  • Description: Creates a new resource group.
  • Example:
    1
    
    az group create --name MyResourceGroup --location eastus
    

5. az group list

  • Description: Lists all resource groups in the current subscription.
  • Example:
    1
    
    az group list --output table
    

6. az group delete –name <resource_group_name> –yes –no-wait

  • Description: Deletes a resource group and all resources within it.
  • Example:
    1
    
    az group delete --name MyResourceGroup --yes --no-wait
    

7. az vm create –resource-group <resource_group_name> –name <vm_name> –image <image> –admin-username <username> –generate-ssh-keys

  • Description: Creates a virtual machine.
  • Example:
    1
    
    az vm create --resource-group MyResourceGroup --name MyVM --image UbuntuLTS --admin-username azureuser --generate-ssh-keys
    

8. az vm list –resource-group <resource_group_name>

  • Description: Lists all virtual machines in a resource group.
  • Example:
    1
    
    az vm list --resource-group MyResourceGroup --output table
    

9. az vm start –resource-group <resource_group_name> –name <vm_name>

  • Description: Starts a virtual machine.
  • Example:
    1
    
    az vm start --resource-group MyResourceGroup --name MyVM
    

10. az vm stop –resource-group <resource_group_name> –name <vm_name>

1
2
3
4
5
- Description: Stops a virtual machine.
- Example:
  ```bash
  az vm stop --resource-group MyResourceGroup --name MyVM
  ```

11. az storage account create –name <storage_account_name> –resource-group <resource_group_name> –location <location> –sku <sku>

1
2
3
4
5
- Description: Creates a storage account.
- Example:
  ```bash
  az storage account create --name MyStorageAccount --resource-group MyResourceGroup --location eastus --sku Standard_LRS
  ```

12. az storage account list

1
2
3
4
5
- Description: Lists all storage accounts in the current subscription.
- Example:
  ```bash
  az storage account list --output table
  ```

13. az storage account show-connection-string –name <storage_account_name> –resource-group <resource_group_name>

1
2
3
4
5
- Description: Shows the connection string for a storage account.
- Example:
  ```bash
  az storage account show-connection-string --name MyStorageAccount --resource-group MyResourceGroup
  ```

14. az storage blob upload –container-name <container_name> –file <local_file_path> –name <blob_name> –account-name <storage_account_name>

1
2
3
4
5
- Description: Uploads a file to a storage blob.
- Example:
  ```bash
  az storage blob upload --container-name MyContainer --file myfile.txt --name myblob.txt --account-name MyStorageAccount
  ```

15. az storage blob list –container-name <container_name> –account-name <storage_account_name>

1
2
3
4
5
- Description: Lists all blobs in a storage container.
- Example:
  ```bash
  az storage blob list --container-name MyContainer --account-name MyStorageAccount --output table
  ```

16. az network vnet create –name <vnet_name> –resource-group <resource_group_name> –subnet-name <subnet_name> –address-prefix <address_prefix>

1
2
3
4
5
- Description: Creates a virtual network.
- Example:
  ```bash
  az network vnet create --name MyVnet --resource-group MyResourceGroup --subnet-name MySubnet --address-prefix 10.0.0.0/16
  ```

17. az network vnet list –resource-group <resource_group_name>

1
2
3
4
5
- Description: Lists all virtual networks in a resource group.
- Example:
  ```bash
  az network vnet list --resource-group MyResourceGroup --output table
  ```

18. az network public-ip create –name <public_ip_name> –resource-group <resource_group_name> –allocation-method <allocation_method>

1
2
3
4
5
- Description: Creates a public IP address.
- Example:
  ```bash
  az network public-ip create --name MyPublicIP --resource-group MyResourceGroup --allocation-method Static
  ```

19. az network public-ip list –resource-group <resource_group_name>

1
2
3
4
5
- Description: Lists all public IP addresses in a resource group.
- Example:
  ```bash
  az network public-ip list --resource-group MyResourceGroup --output table
  ```

20. az network nsg create –name <nsg_name> –resource-group <resource_group_name>

1
2
3
4
5
- Description: Creates a network security group.
- Example:
  ```bash
  az network nsg create --name MyNSG --resource-group MyResourceGroup
  ```

21. az network nsg list –resource-group <resource_group_name>

1
2
3
4
5
- Description: Lists all network security groups in a resource group.
- Example:
  ```bash
  az network nsg list --resource-group MyResourceGroup --output table
  ```

22. az network nic create –name <nic_name> –resource-group <resource_group_name> –subnet <subnet_name> –vnet-name <vnet_name> –public-ip-address <public_ip_name>

1
2
3
4
5
- Description: Creates a network interface.
- Example:
  ```bash
  az network nic create --name MyNIC --resource-group MyResourceGroup --subnet MySubnet --vnet-name MyVnet --public-ip-address MyPublicIP
  ```

23. az network nic list –resource-group <resource_group_name>

1
2
3
4
5
- Description: Lists all network interfaces in a resource group.
- Example:
  ```bash
  az network nic list --resource-group MyResourceGroup --output table
  ```

24. az webapp create –name <app_name> –resource-group <resource_group_name> –plan <app_service_plan_name> –runtime <runtime>

1
2
3
4
5
- Description: Creates a web app.
- Example:
  ```bash
  az webapp create --name MyWebApp --resource-group MyResourceGroup --plan My
  ```

What Next?

Azure CLI provides a convenient way to manage Azure resources from the command line, enabling automation and efficient resource management. In this guide, we covered the basics of Azure CLI installation, authentication, and usage with detailed examples. Explore more commands and options in the official documentation.

This post is licensed under CC BY 4.0 by the author.