Cloning Azure environments utilizing Azure Automation

One in all my clients requested for a process for cloning Azure environments. An surroundings is a useful resource group with a number of Home windows VM on it. Since that isn’t the primary time that I’ve seen comparable requests, I want to share a script utilizing Azure Automation that would assist cloud directors who might face the identical problem.

There are a few elegant methods to realize the purpose. The primary one which involves my thoughts is by adjusting their Azure DevOps pipeline, add some variables, and a few therapeutic massage of the ARM templates, and, voilà, we might have a shiny new surroundings. A second one could be utilizing Azure Site Recovery (ASR) to create take a look at failover eventualities.

After some dialogue, the fact was that every one environments in Azure have been a part of a lift-and-shift migration. These environments may have a number of servers, plenty of knowledge, and the purpose was to copy these servers as is. On high of that, some cloned environments would possibly keep for some time within the subscription.

One other vital level concerning the state of affairs was that the cloned Azure environments can occur fairly often and so they might keep for just a few days and even months. The frequency of cloning Azure environments was one other vital level. They could possibly be very frequent.

My determination was to create a script after which convert it to an Azure Automation the place the shopper offers the useful resource group and a clone identify. The script will retrieve the present digital community and subnet of the supply servers and mimic that community infrastructure within the cloned surroundings.

  • Create a brand new useful resource group for the cloned surroundings including the prefix _<CloneName>_Bubble.
  • Create a brand new digital community and subnet construction within the goal. We are going to copy all of the naming conventions and IP settings from the unique digital community.
  • Create a snapshot of all VM disks within the supply useful resource group and create managed disks within the goal useful resource group.
  • Create new VMs utilizing the data from the supply VMs. Connect the model new managed disks within the new VMs.

Cloning Azure environments

The script course of began with just a few features in PowerShell, a whole lot of validation and checks in various kinds of environments. When a great working situation was achieved, we realized that the surroundings (surroundings and useful resource teams will probably be used interchangeably on this article) may have something between just a few to as many as 50 VMs.

When working the primary model of the PowerShell script in Azure Automation, the time to clone a whole surroundings with 19 VMs took round 4 hours so a brand new requirement was launched: The method needs to be as quick as we may get.

Due to this new requirement, we launched PowerShell Workflows within the image, which is supported in Azure Automation and permits duties to run in parallel, with some caveats.

The whole script is accessible at my GitHub, and you’ll click on here. We’re going over the primary areas of the script that will help you to grasp the code and apply it to your surroundings.

The script: Setting validation

The script will validate just a few gadgets within the surroundings earlier than transferring ahead with the creation of a digital community and VMs. These following situations are going to be checked as a part of the method. In the event that they exist, then the script won’t execute, and guide cleanup will probably be required.

  • The goal useful resource group should not exist.
  • All VMs within the supply useful resource group have to be turned off.
  • All VMs (digital community interfaces) within the supply useful resource group should share the identical digital community.

Utilizing PowerShell Workflows

Cloning Azure environments

There are some benefits when utilizing PowerShell Workflow, resembling multithreading (parallel) and checkpoint functionality, which permits the Workflow to be resumed. However there are additionally some caveats when utilizing PowerShell Workflows, and we are going to tackle a few of them in a separate article right here at TechGenix.

You might be questioning how the Azure Automation is aware of the distinction between Workflows and common PowerShell scripts. The PowerShell Workflow has a unique construction, as listed beneath.

Workflow <Verb-Noum> 

All code in PowerShell Workflows run on the Home windows Workflow Basis. The one exception is after we use InlineScript, which executes the code in our conventional Home windows PowerShell.

The InlineScript exercise can retrieve data from the Workflow by referencing variables utilizing the next format $Utilizing:<VariableName> and that helps to deliver values between Workflow and common PowerShell.

When you will have a limitation utilizing Workflow, the InlineScript exercise may help to beat the barrier, and we had to make use of that in our present script. We used it to create the digital community and cargo all variables in a hashtable that will probably be used all through our script execution.

Right here is how the structure for the InlineScript exercise was utilized in our script earlier than we attain the stage to start out provisioning cloned VMs.

$tControl = InlineScript 
Features Space
Script Physique space
$tControl #--> final line of the block to return the worth to the variable within the workflow

Word: Features created within the Workflow space can’t be consumed inside the InlineScript. We should declare the perform within the InlineScript block, and that’s that you simply see the identical perform within the code twice.

We additionally configured the choice variable $PSRunInProcessPreference to $True at the start of the script, and by doing that, we pressure all actions to run in the identical course of. For extra details about choice variables, the next link can be utilized.

The features

The script goals to cut back linear code and tackle all repetitive duties utilizing features. Within the Workflow space, we depend on 4 features to ship the constant output to assist all phases of our cloning course of. Here’s a abstract of our features:

  • VMInventory($VMName): This perform will obtain a VM identify, and it’ll create an array with all the data associated to the VM, together with disks, VM measurement, and community data. Use that perform so as to add any extra data that you could be want from the supply VM.
  • VMSnapshot($vmInfo,$HashTable): It is going to obtain the array containing the VM data and an array with all of the surroundings data. It is going to create snapshots of the prevailing VMs within the goal useful resource group.
  • VMRestoreSnap($vminfo,$HashTable): It is going to obtain the identical data from the earlier perform, and it’ll create managed disks of all snapshots that have been created within the goal useful resource group.
  • CloneVM($VMInfo,$HashTable): The ultimate piece of the puzzle, it’ll seize all of the details about the VM, mix with the brand-new managed disks, and create a brand new VM connected to the cloned digital community and subnet.

The primary script part

Right here is the place we obtain the multithreading purpose and may run a number of cases of cloning VMs through the use of -Parallel swap within the ForEach assertion. Based mostly on my observations, when utilizing Azure Automation, the candy spot was round six cases on the similar time and to throttle the variety of cases accordingly, we used -ThrottleLimit swap.

ForEach -Parallel -ThrottleLimit 6 ($vm in $VMs) {

The ultimate motion on the script is to take away any current snapshots and supply how a lot time your entire execution befell.

Cloning Azure environments: Wrapping all of it up

Cloning Azure environments


The purpose of this text is to show how one can use Azure Automation with PowerShell Workflows to go about cloning Azure environments.

The script is just not a one-size-fits-all, and it was created to deal with some necessities and challenges of a selected buyer. Nonetheless, you should use it as a place to begin to deal with a number of the extra necessities that your surroundings might have if you wish to go about cloning Azure environments.

There are a few gadgets that you have to be conscious of when creating the present cloning script:

  • We aren’t making an allowance for different frequent assets that could possibly be simply a part of your utility/surroundings, resembling load balancers, Azure Software Gateways, Key Vaults, Storage Accounts, and so forth.
  • We aren’t taking all of the VM properties: boot diagnostics, IPs and extensions. We’re simply ensuring that now we have the identical VM measurement, similar disks, and OS (in our case solely Home windows).
  • The creation of Home windows VMs makes use of a parameter to make use of existent licenses. (It is probably not appropriate on your subscription.)
  • We haven’t examined the script in opposition to Linux VMs and encrypted disks.
  • In case your area controllers or any auxiliary system are required in that “bubble” community, your script ought to be capable of clone them as a part of the method.
  • There isn’t a connectivity on that new VNet, and my advice is to make use of the Azure Bastion service to permit entry to that surroundings.

Featured picture: Shutterstock

Publish Views:

Learn Subsequent

About the Author

Leave a Reply

Your email address will not be published. Required fields are marked *