Creating Virtual Machines Exporting & Deployment of ARM Template Using Azure CLI
Automating resource deployment is a critical skill for any Cloud or DevOps Engineer when working in Azure. This step-by-step guide will walk you through creating a Virtual Machine (VM) using Azure CLI and then exporting the Azure Resource Manager (ARM) template. This process simplifies VM creation and enables infrastructure-as-code (IaC) practices for repeatable deployments.
Why This Is Important
Consistency: Exporting ARM templates ensures you can effortlessly redeploy identical infrastructure configurations.
Automation: Using the CLI for resource creation reduces manual effort and minimizes errors.
Scalability: ARM templates allow you to manage and scale resources across environments with ease.
Prerequisites
Before proceeding, ensure you have:
An active Azure subscription.
The Azure CLI is installed on your local machine link on how to install CLI LINK
Logged into Azure CLI using the command:
az login
Step 1: Create a Resource Group
Start by creating a resource group to hold your VM and associated resource
az group create --name Luxembourg-RG --location eastus
You can login to Azure Portal to check if it was actually created.
Step 2: Create a Virtual Machine
Use the following command to create a VM
az vm create `
--resource-group Luxembourg-RG `
--name polandVM `
--image Ubuntu2204 `
--admin-username azureuser `
--generate-ssh-keys `
Resource Group: The group you created earlier.
VM Name: A unique name for the virtual machine.
Image: The VM operating system (e.g., UbuntuLTS).
Admin Username: Specify a username for VM access.
SSH Keys: Automatically generates keys for secure access.
Check the Luxembourg-RG resource group to confirm it is created.
Step 3: Verify the Virtual Machine
Confirm the VM is up and running:
az vm show --resource-group MyResourceGroup --name MyVM --show-details --query "{Name:name, Status:powerState, IP:publicIps}" --output table
This command retrieves the VM’s name, power state, and public IP address.
Step 4: Export the ARM Template
To create an ARM template from the VM, use the following command
az group export --name MyResourceGroup --output json --query properties.template > MyVMTemplate.json
This command saves the ARM template to a JSON file named VMTemplate.json
.
Step 5: Review the ARM Template
Open the MyVMTemplate.json
file in a code editor like Visual Studio Code to examine its contents. The template includes all the resource configurations, allowing you to redeploy the same infrastructure.
Step 6: Deploy the ARM Template
To test the ARM template, redeploy it into a different resource group:
Create a new resource group:
az group create --name NewDeploy --location australiaeast
Deploy the template:
az deployment group create --resource-group NewDeploy --template-file MyVMTemplate.json
Best Practices
Parameterizations: Modify the ARM template to include parameters for dynamic deployments.
Version Control: Save your templates in a Git repository for collaboration and version tracking.
Testing: Validate your templates in a staging environment before deploying to production.
Conclusion
Creating VMs and exporting ARM templates using Azure CLI is an essential workflow for anyone managing Azure resources. This approach fosters automation, repeatability, and compliance with IaC principles, making it an invaluable tool in modern cloud environments.