To create a Virtual Machine (VM) in Azure using the Azure Command-Line Interface (Azure CLI), you need to follow these general steps:
Prerequisites
- Azure Subscription: Ensure you have an active Azure subscription. If you don't have one, you can sign up for a free Azure account.
- Install Azure CLI: Install the Azure CLI on your local machine. You can download and install it from here.
Steps to create a VM
Login to Azure
Open a command prompt or terminal and run the following command to log in to your Azure account. The az login command is used in Azure CLI to authenticate and log in to your Azure account. It initiates the login process, allowing you to access and manage Azure resources from the command line. When you run az login, it opens a web page in your default web browser, prompting you to enter your Azure credentials (username and password).
Alternatively, you can use the --service-principal, --identity, or other authentication methods for non-interactive logins or automation scenarios. More about the az login command. After a successful login, you can use the az account show command to display information about the currently logged-in account.
az login
Create a Resource Group
Create a resource group to logically group and manage your Azure resources. The az group command in the Azure Command-Line Interface (Azure CLI) is used to manage Azure Resource Groups. Resource Groups are logical containers that hold related Azure resources and are used to manage and organize resources in a more structured way. The az group command provides various subcommands to create, update, delete, and manage resource groups. For our example, we will use az group create:
az group create --name "demo-rg" --location "centralus"
Create a VM
Use the az vm create command to create a VM. Customize the command with your desired settings, such as VM name, VM size, operating system, username, and password.
Providing a unique name for a Virtual Machine (VM) within a specified resource group is important for several reasons:
- Resource Identification: names within a resource group must be unique to identify and distinguish each resource. This uniqueness ensures that there are no naming conflicts or ambiguity when referring to resources within the same scope.
- Azure DNS and FQDN: Azure automatically assigns a fully qualified domain name (FQDN) to a VM based on its name. This FQDN is used for accessing the VM over the internet. A unique name ensures that the FQDN is also unique, avoiding naming conflicts in the Azure DNS.
- Azure Portal and CLI Commands: a unique name makes it easier to manage and interact with resources through the Azure Portal, Azure Command-Line Interface (CLI), or other Azure management tools. It simplifies command-line operations by avoiding ambiguity.
- Automation and Scripting: when automating resource deployment using scripts or templates, a unique name is crucial to avoid errors and conflicts during the provisioning process. Automation tools rely on unique identifiers to manage and deploy resources correctly. More about the [az vm] various subcommands: (https://learn.microsoft.com/en-us/cli/azure/vm?view=azure-cli-latest).
az vm create --resource-group "demo-rg" --name "demo-vm-win" --image "win2019datacenter" --admin-username "admin" --admin-password "strongpassword123qwe$"
Open Ports (Optional)
If your application requires specific ports to be open, you can configure the Network Security Group (NSG) associated with the VM to allow incoming traffic on those ports.
az vm open-port --resource-group "demo-rg" --name "demo-vm-win" --port 3389
Get VM IP (Optional)
az vm list-ip-addresses --resource-group "demo-rg" --name "demo-vm-win" --output table
Other things you might want to take into consideration when creating a VM:
- *Size *(select an appropriate VM size based on your workload requirements. Sizes vary in terms of CPU, memory, and disk space.)
-- size <vm-size>
- OS Disk Type (choose between Standard HDD, Standard SSD, or Premium SSD for the operating system disk)
--os-disk-type <os-disk-type>
- Data Disks (attach additional data disks to the VM if required)
--data-disk-sizes-gb <disk-sizes>
- Availability Set (if high availability is a concern, consider placing the VM in an availability set)
--availability-set <availability-set-name>
- Tags (you can tag resources with metadata for better organization.)
--tags <key1=value1 key2=value2>
Before running the Azure CLI command to create a VM, make sure you have authenticated using az login. Additionally, be aware of your Azure subscription, and ensure that you have the necessary permissions to create resources in the specified resource group.