Engaging Azure CLI: Virtual Machine and Resource group creation

Engaging Azure CLI: Virtual Machine and Resource group creation

Apart from creating resources with the Azure portal, we can also create resources with the Command Line Interface (CLI) using Powershell.

The Powershell can be downloaded from the Microsoft Cloud website and must be installed on your personal computer.

In this article, we will through the process of creating resources like Virtual Machines, resource groups etc. using the CLI on the Powershell.

As explained above, you must have installed a Powershell on your computer. You can download the Azure CLI Here.

The following steps explain in detail the creation of a Virtual Machine and Resource group using the Azure CLI:

  1. Open the Powershell on your computer login using the command - "az login". As the command is typed, press the "enter" key. A web browser will be automatically opened for you to log in with your Azure username and password.

  2. After you have successfully signed in, you can now start creating your resources. The next step is to create a Virtual Machine but before the creation of the VM, the Resource group has to be created first. So let's create the resource group. Type in the command "az group create --name Myresourcegroup --location Uksouth", and press the "enter" key. Please note that the areas highlighted in yellow are to be changed to your preferred name and region of your choice. The resource group is created afterwards.

  3. The next resource to be created as the resource group has been created. Type in the command " az vm create --resource-group Myresourcegroup --name MyVMname --image UbuntuLTS --admin-username Myusername \--admin-password Mypassword --authentication-type password --size Standard_DS2_v2 ". Please note that the areas highlighted in yellow are to be changed to your preferred name. However, Image and Size must be specifically picked as they have types.

    The picture above confirms that our virtual machine has been created. This can also be confirmed from the Azure portal as shown in the pictures below:

    The Virtual Machine can also be managed in so many ways with the CLI. VM can be managed by stopping, starting, restarting, deleting and in so many other ways.

    Kindly find below some of the CLI commands:

    | Task | Azure CLI commands | | --- | --- | | Start a VM | az vm start --resource-group myResourceGroup --name myVM | | Stop a VM | az vm stop --resource-group myResourceGroup --name myVM | | Deallocate a VM | az vm deallocate --resource-group myResourceGroup --name myVM | | Restart a VM | az vm restart --resource-group myResourceGroup --name myVM | | Redeploy a VM | az vm redeploy --resource-group myResourceGroup --name myVM | | Delete a VM | az vm delete --resource-group myResourceGroup --name myVM |

    We are going to be looking at one of those managing commands.

    Stopping a Virtual Machine (VM): Type the command "az vm stop --resource-group myResourceGroup --name myVM" and press the ënter" key. The myResourceGroup and myVM should be the name given to them during the creation of the VM.

    1. The VM will be stopped from running, this can also be seen on the portal to confirm that the VM is already stopped.

      In the same manner and as seen in the table for Azure CLI commands, each task can be performed to manage the Virtual Machine.

      The CLI commands are not exhaustive as there are quite several commands to administer.

      I hope you found this article interesting.