How I Automate Azure Tasks with CLI: A Step-by-Step Guide for Cloud Admins

If you’ve ever spent hours doing repetitive tasks in Azure—like creating VMs, tagging resources, or scheduling backups—you know how quickly it can get tedious. I’ve been there. That’s why I started using Azure CLI to automate these tasks, and it’s been a game-changer. In this post, I’m going to walk you through a real-world workflow that I use, so you can do the same.


The Scenario I’m Solving

Here’s what we’ll automate together:

  1. Log in and pick the right Azure subscription.
  2. Create a resource group.
  3. Spin up a virtual machine.
  4. Add tags to resources automatically.
  5. Schedule daily backups.
  6. Log everything and handle errors.
  7. Make the script run automatically on a schedule.

By the end, you’ll have a workflow you can tweak for your own projects.


What You’ll Need

Before we dive in, make sure you have:

  • Azure CLI installed (official guide)
  • Access to an Azure subscription, or better yet, a free sandbox from Microsoft Learn
  • Some basic Bash or PowerShell familiarity
  • An SSH key for connecting to your VMs

Free resources I recommend:

  • Microsoft Learn Sandbox: Lets you try Azure hands-on without needing a paid subscription. Check it out here
  • Microsoft Learn Modules: Step-by-step tutorials with exercises, including Azure CLI labs. Great for practicing safely.

Step 1 – Log in and Pick Your Subscription

The first step is logging in and making sure you’re working in the right subscription:

bash

# Log in
az login

# Check your subscriptions
az account list --output table

# Set the subscription you want to work in
az account set --subscription "YourSubscriptionName"

When I first started, I messed up a few times by running commands in the wrong subscription—so trust me, this step is important.


Step 2 – Create a Resource Group

I always start by creating a resource group. Think of it like a folder for all your Azure resources:

bash

RESOURCE_GROUP="AutoRG"
LOCATION="EastUS"

az group create \
  --name $RESOURCE_GROUP \
  --location $LOCATION

Using variables here makes it way easier to reuse the script for different projects.


Step 3 – Spin Up a VM

Here’s how I create a virtual machine automatically:

bash

VM_NAME="AutoVM"
ADMIN_USER="azureuser"

az vm create \
  --resource-group $RESOURCE_GROUP \
  --name $VM_NAME \
  --image UbuntuLTS \
  --admin-username $ADMIN_USER \
  --generate-ssh-keys

I like using variables so I can tweak the VM name, OS image, or user without rewriting the whole script.


Step 4 – Tag Resources

Tags save you a ton of headaches when managing multiple resources. I usually tag my VMs like this:

bash

az resource tag \
  --tags Owner="Admin" Environment="Dev" \
  --name $VM_NAME \
  --resource-group $RESOURCE_GROUP \
  --resource-type "Microsoft.Compute/virtualMachines"

For me, tagging is a lifesaver when generating reports or tracking costs.


Step 5 – Schedule a Daily Backup

I like keeping backups automated so I don’t have to remember it every day. Here’s a command to enable backup for a VM:

bash

az backup protection enable-for-vm \
  --resource-group $RESOURCE_GROUP \
  --vault-name "BackupVault" \
  --vm $VM_NAME \
  --policy-name "DefaultPolicy"

And if you’re just practicing, the Microsoft Learn Sandbox is perfect for testing this safely.


Step 6 – Logging and Handling Errors

I always log my scripts to catch errors early:

bash

#!/bin/bash
{
  echo "Starting automation script: $(date)"
  
  if az group create --name $RESOURCE_GROUP --location $LOCATION; then
      echo "Resource group created successfully."
  else
      echo "Failed to create resource group."
  fi
  
  echo "VM creation in progress..."
  az vm create \
      --resource-group $RESOURCE_GROUP \
      --name $VM_NAME \
      --image UbuntuLTS \
      --admin-username $ADMIN_USER \
      --generate-ssh-keys
} >> automate.log 2>&1

Honestly, I can’t stress this enough—without logs, debugging failed scripts is a nightmare.


Step 7 – Automate Script Execution

Linux/macOS – Cron Jobs

I usually schedule scripts to run daily at 2 AM like this:

bash

0 2 * * * /home/user/azure_automation.sh >> /home/user/azure_automation.log 2>&1

Windows – Task Scheduler

  • Open Task Scheduler → Create Basic Task → Select your script
  • Set frequency and configure the “Start in” folder
  • Optional: redirect output to a log file

Advanced – Azure Automation Runbooks

For cloud-native automation:

  1. Create an Automation Account in Azure.
  2. Add your script as a Runbook.
  3. Schedule the Runbook to run automatically.

Pro tip: Microsoft Learn has free modules that walk through Runbooks in a sandbox environment—you can practice without touching your paid subscription.


Conclusion

Automating Azure CLI tasks has saved me hours every week. Now, I create resources, tag them, schedule backups, and log everything without lifting a finger once the script is set up.

Next steps: Try expanding this workflow to include networks, storage accounts, and multiple VMs. Combine loops and conditionals to handle large-scale automation.

Free Learning Resources:

Leave a Comment