Azure Resource Manager - Part 6 - Move Azure Resources from one Resource Group to another
This article is part of a series. Here's a list of all available parts.
- Part 0: Introduction to the article series
- Part 1: Create an AzureRm Active Directory (AAD) Application using PowerShell
- Part 2: Getting started with the AzureRm PowerShell cmdlets
- Part 3: Build an application using C# which is using the Azure Resource Manager API's
- Part 4: Tip: Azure Resource Explorer Tool
- Part 5: Tip: Get all available api-version alternatives for the ARM endpoints
- Part 6: Move Azure Resources from one Resource Group to another
- Part 7: Download an Azure Publishing Profile (xml) programmatically using REST
- Part 8: Programmatically export Resource Group template using the REST API
In the past recent months I've been turning inside out on various parts of Azure, including the Azure Resource Manager (ARM) and what it offers.
If you're looking for the basics of getting started, please check out the other posts in this article series. Start here.
Background
I love how we can work with resources in Resource Groups, and keep them grouped for easy findability, configuration and manageability.
One of the things I've been looking for is an automatic way to move resources between my resource groups.
In this post we'll quickly check out how to do just that, using the ARM REST API.
Use the Azure Resource Manager REST Api to move resources from one Resource Group to another
I dug into the REST API's for Azure Resource Manager and found that there's now an endpoint for doing this, and it's very straight forward.
The request should be done to the /resourceGroups/<groupName>/moveResources
endpoint, which could look like this (replace subscription Id and resource group name with your own:
The POST request
This is a sample of what the POST request endpoint would look like. Replace the SUBSCRIPTIONGUID
and RESOURCEGROUPNAME
with your own.
POST https://management.azure.com/subscriptions/SUBSCRIPTIONGUID/resourceGroups/Resource-Group-A/moveResources?api-version=2014-04-01
JSON Payload
The payload needs to look like this in your POST
request, where you specify the targetResourceGroup
for where to move the resources, and any resources
to move:
{
"targetResourceGroup": "/subscriptions/SUBSCRIPTIONGUID/resourceGroups/ZimmersNewResourceGroup",
"resources": [
"/subscriptions/SubscriptionGUID/resourceGroups/Bots/providers/Microsoft.Web/sites/slackcodebot"
]
}
Example
Pose that we have Resource-Group-A
and Resource-Group-B
. Resource Group B is empty at the moment, but Resource Group A contains our resources that we want to move:
We have two Azure Storage Accounts: zimmerstorage123
and zimmerstorage234
. Here's what that exact request would look like in order to move from A to B:
POST https://management.azure.com/subscriptions/c6f21dec-a9b3-4ed9-88b7-946549cb2300/resourceGroups/Resource-Group-A/moveResources?api-version=2014-04-01
{
"targetResourceGroup": "/subscriptions/c6f21dec-a9b3-4ed9-88b7-946549cb2300/resourceGroups/Resource-Group-B",
"resources": [
"/subscriptions/c6f21dec-a9b3-4ed9-88b7-946549cb2300/resourceGroups/resource-group-a/providers/Microsoft.Storage/storageAccounts/zimmerstorage123",
"/subscriptions/c6f21dec-a9b3-4ed9-88b7-946549cb2300/resourceGroups/resource-group-a/providers/Microsoft.Storage/storageAccounts/zimmerstorage234"
]
}
The POST request is targeting Resource-Group-A
and in the payload we select the Resource-Group-B
as a targetResourceGroup
for the resources.
The status of the destination resource group is changed to Moving Resources
as expected. After a short while (or a long while, should you have a lot of resources), they now live in your new resource group:
The result is as expected, that our resources now exist in Resource-Group-B
instead of A. Voila, it's that easy.
Feel free to drop any comments or questions in the comments section.
Enjoy.
Recent comments