Create an Azure Virtual Machine via CLI
Full-Access Members Only
Sorry, this lesson is only available to Server Academy Full-Access members. Become a Full-Access member now and get instant access to this and many more premium courses. Click the button below and get instant access now.
Instructions
Q&A (0)
Notes (0)
Resources (0)
Saving Progress...
Resources
There are no resources for this lesson.
Notes can be saved and accessed anywhere in the course. They also double as bookmarks so you can quickly review important lesson material.
In this lesson, you will use the CLI to create an Azure virtual machine (VM), install Nginx (a popular web server), and then delete the VM along with all associated resources. You will use the Azure CLI for this task and work within a resource group named FreeResourcesRG
.
Task 1: Create a Linux Virtual Machine and Install Nginx
Use the following Azure CLI commands to create a Linux VM and install Nginx. The Custom Script Extension will be used to install Nginx on your VM after it is created.
Open Cloud Shell
Access the Azure Portal.
Click on the Cloud Shell icon in the top menu bar.
Create a Linux Virtual Machine
Run the following command to create a Linux VM in the FreeResourcesRG
resource group:
az vm create \
--resource-group FreeResourcesRG \
--name my-vm \
--public-ip-sku Standard \
--image Ubuntu2204 \
--admin-username azureuser \
--generate-ssh-keys
This command will create a VM named my-vm
with Ubuntu 22.04, using SSH keys for authentication. The VM creation will take a few minutes.
Install Nginx on the VM
After the VM is created, run the following command to install Nginx using the Custom Script Extension:
az vm extension set \
--resource-group FreeResourcesRG \
--vm-name my-vm \
--name customScript \
--publisher Microsoft.Azure.Extensions \
--version 2.1 \
--settings '{"fileUris":["https://raw.githubusercontent.com/MicrosoftDocs/mslearn-welcome-to-azure/master/configure-nginx.sh"]}' \
--protected-settings '{"commandToExecute": "./configure-nginx.sh"}'
This command will:
Task 2: Verify Nginx Installation
Get the Public IP Address
Run the following command to get the public IP address of your VM:
az vm show \
--resource-group FreeResourcesRG \
--name my-vm \
--show-details \
--query [publicIps] \
--output tsv
Copy the public IP address from the output.
Verify Nginx is Running
Open a web browser and enter the public IP address you copied.
You should see a welcome message from Nginx, indicating that it is installed and running on your VM.
Task 3: Delete the VM and Associated Resources
Once you have verified that Nginx is running, you can delete the VM and all associated resources to avoid incurring costs.
In just a few lessons (Configuring Network Access), you will need an Azure VM. If you plan to complete that lesson soon, you can skip deleting the VM so you won't have to create a new one again in the "Configuring Network Access" lesson. If you are not sure when you'll complete that lesson, go ahead and delete the VM now.
Delete the Virtual Machine
Run the following command to delete the VM:
az vm delete \
--resource-group FreeResourcesRG \
--name my-vm \
--yes
Delete the Network Interface
Identify the network interface associated with the VM by running:
az vm show \
--resource-group FreeResourcesRG \
--name my-vm \
--query "networkProfile.networkInterfaces[0].id" \
--output tsv
Use the returned network interface ID to delete the network interface:
az network nic delete \
--ids [networkInterfaceId]
Delete the Public IP Address
Identify the public IP address associated with the VM by running:
az vm show \
--resource-group FreeResourcesRG \
--name my-vm \
--query "networkProfile.networkInterfaces[0].id" \
--output tsv | xargs -I {} az network nic show --ids {} --query "ipConfigurations[0].publicIpAddress.id" --output tsv
Use the returned public IP address ID to delete the public IP address:
az network public-ip delete \
--ids [publicIpAddressId]
Delete the OS Disk
Identify the OS disk associated with the VM by running:
az vm show \
--resource-group FreeResourcesRG \
--name my-vm \
--query "storageProfile.osDisk.managedDisk.id" \
--output tsv
Use the returned disk ID to delete the OS disk:
az disk delete \
--ids [osDiskId] \
--yes
By following these steps, you have created a Linux VM, installed Nginx, verified its operation, and cleaned up all associated resources. This lesson provides a comprehensive understanding of managing Azure VMs using the Azure CLI.
Server Academy Members Only
Sorry, this lesson is only available to Server Academy Full Access members. Become a Full-Access Member now and you’ll get instant access to all of our courses.