Skip to main content

Use Azure CLI to Create a Virtual Machine

Hands-On Lab

 

Photo of

Training Architect

Length

02:00:00

Difficulty

Beginner

In this lab we walk through how to use the Azure CLI (az cli) to create a Virtual Machine within a Virtual Network in Azure. This beginner level hands-on lab covers several concepts, including: Azure CLI syntax Azure CLI help * Azure CLI default and assumed parameters This hands-on lab uses the Azure Cloud Shell, so you don't have to install any software and can follow along from work or at home using the web browser.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Use Azure CLI to Create a Virtual Machine

Introduction

In this lab we walk through how to use the Azure CLI (az cli) to create a Virtual Machine within a Virtual Network in Azure. This beginner level hands-on lab covers several concepts, including:

  • Azure CLI syntax
  • Azure CLI help
  • Azure CLI default and assumed parameters

This hands-on lab uses the Azure Cloud Shell, so you don't have to install any software and can follow along from work or at home using the web browser.

Solution

  1. Log in to the Azure Portal using the credentials provided on the lab instructions page.

Initialize the Azure CLI

  1. Click on the Cloud Shell icon at the top of the window in the Microsoft Azure portal.

  2. Click Bash on the weclome page at the bottom of the screen.

  3. Click Show advanced settings.

  4. Under Cloud Shell region, select South Central US.

  5. Under Storage account, choose the Use existing option.

  6. Under File share, type "shell" in the box provided.

  7. Click Create storage.

  8. Click All resource in the left-hand menu of the Azure portal to verify the existence of the pre-created resources.

Create a Virtual Network

  1. If you are using your own installation of Azure CLI, then login. This is not required if you are using the Cloud Shell provided through the Azure portal.

    az login
  2. View the Azure CLI help for virtual networks.

    az network vnet --help
  3. View the help for the create command.

    az network vnet create --help
  4. Get a list of all the available resource groups.

    az group list
  5. Copy the name field of the provided resource group.

  6. Create the virtual network and give it a name of "test1vnet". Remember to use the name of the group copied from the previous step.

    az network vnet create -n test1vnet -g COPIED_GROUP_NAME
  7. Navigate back to the Resources window in Azure portal and refresh your view.

  8. Click test1vnet.

  9. Click Subnets from the menu bar and see the lack of any subnets.

  1. Navigate to the Cloud Shell and create a subnet, giving it a name of "subnet1".

    az network vnet subnet create -n subnet1 --vnet-name test1vnet --address-prefixes 10.0.1.0/24 -g COPIED_GROUP_NAME
  2. In the Azure portal, refresh the Subnets window and verify the existence of the subnet.

Create a Virtual Machine

  1. In the Cloud Shell, create the virtual machine and give it a name of "test1vm".

    az vm create -n test1vm --image UbuntuLTS --admin-username azureuser --generate-ssh-keys -g COPIED_GROUP_NAME
  2. Once the command is finished, navigate back to the Azure portal and refresh the All resources view. Verify the existence of several virtual machine elements.

  3. Copy the publicIpAddress of the created virtual machine.

  4. Connect to the virtual machine using ssh.

    ssh azureuser@COPIED_PUBLIC_UP_ADDRESS -i -/.ssh/id_rsa
  5. Verify the connection to the virtual machine and then exit.

    exit
  6. Pull up the help for creating a virtual machine.

    az vm create -h
  7. Use the multi-line character to create a virtual machine with multiple options.

    az vm create 
    --resource-group COPIED_GROUP_NAME 
    --name test2vm 
    --image UbuntuLTS 
    --admin-username azureuser 
    --generate-ssh-keys 
    --size Standard_B2ms 
    --public-ip-address test1vm-pip 
    --nsg lab-test1-nsg 
    --vnet-name test1vnet 
    --subnet subnet1
  8. Once the command is finished, navigate back to the Azure portal and refresh the All resources view. Click test2vm.

  9. Click Networking and verify this machine uses the provided networking information.

Conclusion

Congratulations — you've completed this hands-on lab!