0%

0/1 Lessons

Course Introduction

• 12min

0 / 1 lessons complete

Introduction to Cloud Computing

• 1hr 10min

0 / 6 lessons complete

The Benefits of using Cloud Services

• 44min

0 / 6 lessons complete

Azure Cloud Service Types

• 38min

0 / 5 lessons complete

Core architectural components of Azure

• 2hr 20min

0 / 8 lessons complete

Compute and Networking Services

• 3hr 14min

0 / 13 lessons complete

Azure Storage Services

• 1hr 48min

0 / 8 lessons complete

Azure Identity, Access and Security

• 1hr 54min

0 / 10 lessons complete

Azure Cost Management

• 1hr 30min

0 / 7 lessons complete

Azure Features and Tools for Governance and Compliance

• 1hr 17min

0 / 7 lessons complete

Features and Tools for Managing and Deploying Azure Resources

• 54min

0 / 5 lessons complete

Monitoring Tools in Azure

• 24min

0 / 5 lessons complete

AZ-900 Practice Exams

• 55min

0 / 2 lessons complete

Course Conclusion

• 5min

0 / 1 lessons complete

Create an Azure Virtual Machine via CLI

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.

Create note

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.

0 0 votes
Lesson Rating
Subscribe
Notify of
profile avatar
0 Comments
Inline Feedbacks
View all comments