'Clone a resource group

So, I've setup a lab environment in a azure resource group with a domain controller, sql-server and a web server. I would like to clone that whole resource group to a new one when needed and by stat I mean with everything intact. Is that possible?



Solution 1:[1]

I don't know of anyway to do a direct copy, but you can export a JSON template from the portal or PowerShell that will come close to giving you something you can automate. I say "close" because the feature is still in preview and some resources can't be exported.

In portal.azure.com go to: Resource Groups > [your rg] > Settings > Export...

In PowerShell see:

Export-AzureRMResourceGroup

Solution 2:[2]

So, the solution I came up with was to simply creating a new Resource group, copy the template (not sysprepped) vhds to that new Resource group and creating new VMs with the vhd's attached.

I do wish that there was a "Clone resource group" button somewhere :)

Solution 3:[3]

To clone a resource group 1. Go to: Resource groups > [resource group to copy] > Settings (sidebar grouping) > Export template > Deploy

My resource group: enter image description here

Issues on my way with solutions:

  1. Parameter osDisk.managedDisk.id is not allowed

  2. Disk [disk subscription] cannot be attached as the disk is already owned by VM [VM subscription]

    • Make the data disk snapshot
    • Create a new disk based on the snapshot (important: the disk name cannot be changed)
    • Change proper id parameter value in settings or edit parameters (under 'Edit parameters' button)
  3. Changing property 'dataDisk.name' is not allowed.

    • As mentioned above. Do not change the disk name when coping the disk.
  4. Required parameter 'adminPassword' is missing (null)."

    • 'Edit Parameters'
    • Add:

      "adminPassword": {
          "value": null
      }
      
    • 'Edit Template'

    • Add:

      "adminPassword": {
          "defaultValue": null,
          "type": "SecureString"
      }
      
    • Set the adminPassword in settings

Solution 4:[4]

Check out Jeff Bow's scripts to copy / clone a resource group:

Copies Azure V2 (ARM) resources from one Azure Subscription to another.

Unlike the Move-AzureRMresource cmdlet, this script allows you to move between subscriptions in different Tenants and different Azure Environments. Requires AzureRM module version 6.7 or later.

Clones Azure V2 (ARM) resources from one resource group into a new resource group in the same Azure Subscriptions.

Requires AzureRM module version 6.7 or later.

This is intended mostly for Azure V2 virtual machines and will include copying virtual disks, virtual network, load balancers, Public IPs and other associated storage accounts, blob files and now managed disks.

PS suggest you upvote this Azure feedback to provide an Azure-native command, Copy resource groups:

Solution 5:[5]

While it doesn't look to be specifically designed for this use case, I have successfully moved RGs using Azure Resource Mover, released in 2021. Since it isn't mandatory to remove any resources from the source RG, this has worked better for me than exporting the RG JSON and redeploying, etc.

Solution 6:[6]

I believe Ive worked this out using a simple channel. Numerous clients issue operations (flush/retry) and theyre processed sequentially in the launch block. This passes my tests, hopefully it can help someone else out there. If I got something wrong, feel free to correct me in the comments!

sealed interface Operation {

    object Flush : Operation
}

fun interface Retry : Operation {

    suspend operator fun invoke()
}

private val channel = Channel<Operation>(UNLIMITED)

init {
    scope.launch {
        val pending = mutableListOf<Retry>()

        for (operation in channel) {
            when (operation) {
                is Retry -> {
                    pending += operation
                }
                is Flush -> {
                    if (pending.isNotEmpty()) {
                        pending.forEach { retry ->
                            retry()
                        }
                        pending.clear()
                    }
                }
            }
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bmoore-msft
Solution 2 pezmannen
Solution 3 Jay Allen
Solution 4 Community
Solution 5 piersonn
Solution 6 zoltish