Creating Virtual Machines and Exporting the ARM Template using Azure CLI

Mary AjayiMary Ajayi
2 min read

Microsoft Azure offers a versatile set of tools for managing cloud resources, and Azure Command-Line Interface (Azure CLI) stands out as a powerful option. In this guide, we'll walk through the process of creating a Virtual Machine (VM) using Azure CLI and exporting the ARM template representing the VM configuration.

Prerequisites

Before diving into the steps, ensure you have the following prerequisites:

  1. Azure Subscription: You need an active Azure subscription. If you don't have one, sign up here.

  2. Azure CLI: Install Azure CLI on your local machine. Follow the installation instructions found here.

Step 1: Create a Resource Group

Open your terminal and execute the following commands to create a resource group and VM. Replace the placeholder values with your desired configuration.

# Set variables
resourceGroupName=<YourResourceGroupName>
vmName=<YourVMName>
location=<YourLocation>

# Create a resource group
az group create --name $resourceGroupName --location $location

This creates a Resource Group, but feel free to adjust the parameters based on your requirements.

You can login to Azure Portal to check if it was actually created.

Step 2: Create a Virtual machine

Create a Linux VM by using the following command:


# Create a VM
az vm create --resource-group $resourceGroupName --name $vmName --image Ubuntu2204 --admin-username azureuser --admin-password <YourPassword> --size Standard_DS1_v2

See the terminal output below.

Check the Deploy resource group to confirm it is created.

Step 3: Export the ARM Template

Azure Resource Manager (ARM) templates define the resources you deploy to Azure. To export the template of your VM, use the following command:

# Export the ARM template
az resource show --resource-group Deploy --name vmdeploy --resource-type "Microsoft.Compute/virtualMachines" --api-version "2022-03-01" --out json > vmtemplate.json

This command retrieves the VM details and saves them to a JSON file named vmtemplate.json.

Congratulations! You've successfully created a VM and exported its ARM template. This process is crucial for Infrastructure as Code (IaC) scenarios, enabling version control, sharing, and consistent reproduction of Azure resources. Explore additional customization options and features of Azure CLI to further enhance your cloud infrastructure management experience.

0
Subscribe to my newsletter

Read articles from Mary Ajayi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mary Ajayi
Mary Ajayi

I am passionate about all things tech, and I'm always learning new skills and technologies to stay on top of the latest trends. I'm also a team player who loves collaborating with others to create innovative solutions. In my free time, I enjoy coding, creating tech blogs, and seeing movies. I'm always up for a challenge and I'm excited to see what the future holds.