To round off things nicely, I thought I would follow on from two previous posts about creating Azure Virtual Machines. First we went through how to create an Azure Virtual Machine using PowerShell, then we went through with ARM templates. Now we’re going to use the Azure CLI for the third try.
Create an Azure Virtual Machine with PowerShell
Create an Azure Virtual Machine with an ARM template
Create an Azure Virtual Machine with Terraform
The plan
- Create a new Resource Group
- Create Virtual Network and Subnet
- Create Virtual Machine
- Open required ports
- Connect to the Virtual Machine
Prerequisites
Before you begin, ensure you have:
- The latest version of the Azure CLI installed
- An active Azure subscription
- Basic understanding of Azure networking concepts
If you haven’t installed Azure CLI yet, follow the instructions on the official documentation.
1. Create a new Resource Group
First, set the variables we’ll use throughout this exercise:
|
|
For the admin password, it’s best to create it securely:
|
|
Now create the resource group:
|
|
This command creates a new resource group in the North Europe region where we’ll deploy all our resources.
2. Create Virtual Network and Subnet
|
|
This creates a virtual network with address space 10.0.0.0/16 and a subnet within it with address space 10.0.1.0/24. The virtual network will provide network isolation for your VM.
3. Create a Network Security Group
Let’s create a network security group with rules to allow RDP (port 3389) traffic:
|
|
4. Create a Public IP Address
|
|
5. Create a Network Interface
|
|
This creates a network interface with the public IP we created and associates it with our subnet and security group.
6. Create the Virtual Machine
|
|
This command creates a Windows Server 2019 VM with the specified size and credentials.
7. Connect to Your Virtual Machine
After your VM is created, you can get its public IP address:
|
|
Use the returned IP address to connect to your VM via RDP.
8. Clean Up Resources (Optional)
When you’re finished testing, you can remove all the resources created in this exercise to avoid incurring additional costs:
|
|
Conclusion
In this article, we’ve seen how to use the Azure CLI to create a Windows virtual machine in Azure. The Azure CLI provides a straightforward, script-friendly way to manage Azure resources from the command line.
Unlike the PowerShell and ARM template methods we explored in previous articles, the Azure CLI is cross-platform, making it a great choice if you work across different operating systems. The commands are also generally shorter and more intuitive than their PowerShell counterparts.
Whether you prefer PowerShell, ARM templates, Terraform, or the Azure CLI, Azure provides multiple options to help you automate your infrastructure deployment according to your preferences and requirements.