Azure Use Automation to Snapshot and Create Disk Periodically

Yst@IT
5 min readMay 23, 2021

--

Azure Use Automation to Snapshot and Create Disk Periodically

Image:https://docs.microsoft.com/en-us/azure/automation/start-runbooks

What is Azure automation

Azure Automation delivers a cloud-based automation and configuration service that supports consistent management across your Azure and non-Azure environments. It comprises process automation, configuration management, update management, shared capabilities, and heterogeneous features. Automation gives you complete control during deployment, operations, and decommissioning of workloads and resources.

Azure Use Automation to Snapshot and Create Disk Periodically

As you can see, Automation can actually achieve different job requirements. In this post, I will write about how to use automation to snapshot and create disk out from it regularly.

Before we start, let’s talk about some terminology first.

  • Automation is the service that will execute the jobs we want it to run for us.
  • Runbook is the script that we tell Automation how and what we want it to run for us.

Therefore, let’s get started by creating an Automation Account. Pay attention to the Run As account option, in simple words, Automation and Disk are two individual services so we need to assign Automation necessary privilege so that it can snapshot and create disk out from Disk.

Besides Run As account, there is another option called managed identity, I will demonstrate both way in this post.

Azure Use Automation to Snapshot and Create Disk Periodically

Runbook only supports these languages and I use PowerShell for my Runbook. Some commands used in my Runbook are not available in Automation by default, so I need to install them manually.

Azure Use Automation to Snapshot and Create Disk Periodically

I need to install Az.Accounts and Az.Compute in order, steps are show below. Repeat the steps for Az.Compute.

Azure Use Automation to Snapshot and Create Disk Periodically
Azure Use Automation to Snapshot and Create Disk Periodically
Azure Use Automation to Snapshot and Create Disk Periodically

Once done, you can verify them from Modules section.

Azure Use Automation to Snapshot and Create Disk Periodically

Next, let’s prepare out Runbook. This script is for my own scenario usage, please modify according to your needs. Please read the # comments for more detail.

Script for Run as accounts

Create the Runbook.

Azure Use Automation to Snapshot and Create Disk Periodically

Next, paste script into the editor, save it and do a test run to make sure the script is functional.

Azure Use Automation to Snapshot and Create Disk Periodically

Once started, it will Queue the job first then start running the job in a second.

Azure Use Automation to Snapshot and Create Disk Periodically

If the script ran without any issues, it will show complete.

Azure Use Automation to Snapshot and Create Disk Periodically

By checking Snapshots and Disks, we can confirm that Automation is working as the way we expected.

Azure Use Automation to Snapshot and Create Disk Periodically
Azure Use Automation to Snapshot and Create Disk Periodically

Next we publish the script to make it online official.

Azure Use Automation to Snapshot and Create Disk Periodically

Once published, we can manually trigger, schedule or use webhook to execute the script. Of course you can modify the script as well.

Azure Use Automation to Snapshot and Create Disk Periodically

Since I want the script to run periodically, let's create a schedule for it. Pay attention that for Starts, you can only set 5 minutes after your current time. As for recurring, the minimum frequency is 1 hour.

For example, you can only set 2:05PM if your current time is 2:00PM

Azure Use Automation to Snapshot and Create Disk Periodically

Let’s go back to Runbook and associate it with the schedule just created.

Azure Use Automation to Snapshot and Create Disk Periodically
Azure Use Automation to Snapshot and Create Disk Periodically
Azure Use Automation to Snapshot and Create Disk Periodically
Azure Use Automation to Snapshot and Create Disk Periodically

Once done, you can verify it at the Schedules section.

Azure Use Automation to Snapshot and Create Disk Periodically

Once Runbook is executed, you can check the job list from Jobs and in addition, we can check Schedules for the next scheduled run time.

Azure Use Automation to Snapshot and Create Disk Periodically
Azure Use Automation to Snapshot and Create Disk Periodically

Script for managed identity

From script point of view, the only difference is the way how we assign privilege to Automation Runbook to perform jobs we want it to execute. For Run as accounts, you need codes from line 1 to 23 from the previous script where as Managed identity, all you need is one line.

In order to use managed identity feature, we simply enable it from Identity seciton.

Azure Use Automation to Snapshot and Create Disk Periodically

Once done, use the new script to perform run Runbook to verify result.

Azure Use Automation to Snapshot and Create Disk Periodically
Azure Use Automation to Snapshot and Create Disk Periodically

Everything is working as expected! That’s it, hope you enjoy the show ;)

--

--

Yst@IT

Cloud Solution Architect, focusing on Oracle Cloud Infrastructure currently.