Manage Virtual Machines in Azure (with examples)

Manage Virtual Machines in Azure (with examples)

In this article, we will explore different use cases of the az vm command, which is part of the Azure Command-Line Interface (CLI). The az vm command allows you to manage virtual machines in Azure, such as creating, starting, stopping, restarting, and querying details about them. We will provide code examples for each use case, along with motivations, explanations for each argument, and example outputs.

Use Case 1: List details of available Virtual Machines

To list the details of available virtual machines in Azure, you can use the following command:

az vm list

Motivation: This use case is helpful when you want to retrieve a list of all virtual machines in a given Azure subscription or resource group. It provides information such as VM names, resource group, power state, location, and other details.

Explanation for arguments:

  • None

Example output:

[
  {
    "name": "vm1",
    "resourceGroup": "rg",
    "powerState": "VM running",
    "location": "westus",
    ...
  },
  {
    "name": "vm2",
    "resourceGroup": "rg",
    "powerState": "VM deallocated",
    "location": "westus",
    ...
  },
  ...
]

Use Case 2: Create a virtual machine using the default Ubuntu image and generate SSH keys

To create a virtual machine with the default Ubuntu image and generate SSH keys for authentication, you can use the following command:

az vm create --resource-group rg --name vm_name --image UbuntuLTS --admin-user azureuser --generate-ssh-keys

Motivation: This use case is useful when you want to create a new virtual machine in Azure using the default Ubuntu image. It automatically generates SSH keys for you, which simplifies the authentication process.

Explanation for arguments:

  • --resource-group: The name of the resource group where the virtual machine will be created.
  • --name: The name of the virtual machine.
  • --image: The name of the image to be used for the virtual machine. In this case, we are using the default Ubuntu image.
  • --admin-user: The username for the administrator account on the virtual machine. In this case, we are using “azureuser”.
  • --generate-ssh-keys: Generates SSH key pairs for authentication.

Example output:

{
  "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg/providers/Microsoft.Compute/virtualMachines/vm_name",
  "name": "vm_name",
  "location": "westus",
  "powerState": "VM running",
  ...
}

Use Case 3: Stop a Virtual Machine

To stop a virtual machine in Azure, you can use the following command:

az vm stop --resource-group rg --name vm_name

Motivation: This use case is helpful when you want to stop a running virtual machine to save costs or perform maintenance tasks. Stopping a virtual machine deallocates the underlying compute resources, but retains the details and configuration.

Explanation for arguments:

  • --resource-group: The name of the resource group where the virtual machine is located.
  • --name: The name of the virtual machine.

Example output:

'''
The operation was successful.
'''

Use Case 4: Deallocate a Virtual Machine

To deallocate a virtual machine in Azure, you can use the following command:

az vm deallocate --resource-group rg --name vm_name

Motivation: This use case is useful when you want to deallocate a virtual machine, which releases the compute resources associated with it. Deallocating a virtual machine stops the billing of the compute resources but retains the VM configuration for later use or scaling.

Explanation for arguments:

  • --resource-group: The name of the resource group where the virtual machine is located.
  • --name: The name of the virtual machine.

Example output:

'''
The operation was successful.
'''

Use Case 5: Start a Virtual Machine

To start a stopped or deallocated virtual machine in Azure, you can use the following command:

az vm start --resource-group rg --name vm_name

Motivation: This use case is helpful when you want to start a stopped or deallocated virtual machine. Starting a virtual machine re-allocates the compute resources associated with it, making it accessible again.

Explanation for arguments:

  • --resource-group: The name of the resource group where the virtual machine is located.
  • --name: The name of the virtual machine.

Example output:

'''
The operation was successful.
'''

Use Case 6: Restart a Virtual Machine

To restart a running virtual machine in Azure, you can use the following command:

az vm restart --resource-group rg --name vm_name

Motivation: This use case is useful when you want to restart a running virtual machine for maintenance or troubleshooting purposes. Restarting a virtual machine gracefully shuts it down and then starts it up again.

Explanation for arguments:

  • --resource-group: The name of the resource group where the virtual machine is located.
  • --name: The name of the virtual machine.

Example output:

'''
The operation was successful.
'''

Use Case 7: List VM images available in the Azure Marketplace

To list the virtual machine images available in the Azure Marketplace, you can use the following command:

az vm image list

Motivation: This use case is helpful when you want to discover the available virtual machine images in the Azure Marketplace. It allows you to find the appropriate image for creating new virtual machines.

Explanation for arguments:

  • None

Example output:

[
  {
    "name": "Canonical:UbuntuServer:18.04-LTS:latest",
    "offer": "Canonical",
    "sku": "18.04-LTS",
    ...
  },
  {
    "name": "MicrosoftWindowsServer:WindowsServer:2019-Datacenter:latest",
    "offer": "MicrosoftWindowsServer",
    "sku": "2019-Datacenter",
    ...
  },
  ...
]

Conclusion

In this article, we explored different use cases of the az vm command, which allows you to manage virtual machines in Azure. We provided code examples, motivations, explanations, and example outputs for each use case. By understanding these use cases, you can effectively manage your virtual machines in Azure using the Azure CLI.

Related Posts

How to use the command pbmtoybm (with examples)

How to use the command pbmtoybm (with examples)

The command pbmtoybm is used to convert a PBM file to a Bennet Yee “face” file.

Read More
How to use the command collectd (with examples)

How to use the command collectd (with examples)

Collectd is a system statistics collection daemon that gathers information about the system’s usage and performance.

Read More
How to use the command base64 (with examples)

How to use the command base64 (with examples)

Base64 is a command-line tool that allows encoding or decoding files or standard input using the Base64 algorithm.

Read More