Click here to Skip to main content
13,700,326 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

3K views
Posted 3 Jan 2018
Licenced MIT

Moving Application Insights Resources Between Subscriptions in Azure

, 3 Jan 2018
Rate this:
Please Sign up or sign in to vote.
How to move application insights resources between subscriptions in Azure

Editorial Note

This article is an entry in our Microsoft Azure IoT Contest. Articles in this section are not required to be full articles so care should be taken when voting.

There is documentation available for Move resources to new resource group or subscription, but things are easier to do with exact sample code, so here it goes Smile. The steps below will work for moving between Resource Groups as well, you'll just skip some steps that would be obvious.

Limitations

There are a couple of limitations which make sense like resources needing to be in the same directory and not every service is supported to move which is all detailed on the link mentioned above where all the limitations are documented.

Step by Step

First off, we need to login:

Login-AzureRmAccount

Then, let's list all our subscriptions that we have access to:

$Subscriptions = Get-AzureRmSubscription
$Subscriptions | Format-Table

You should see a result similar to the below:

image

Now, we need to set the source and destination subscription id from the list above and then check if the resource type is registered in the destination subscription.

$SourceSubscriptionId = "<source sub id>"
$DestinationSubscriptionId = "<dest sub id>"

Once done, we can check the resource providers to make sure the insights resource provider is enabled, usually this would not be enabled if you haven't created the resource in the destination subscription before. We'll first change the context to the subscription to our destination subscription id and then list the resource providers.

Set-AzureRmContext -Subscription $DestinationSubscriptionId
$ResourceProviders = Get-AzureRmResourceProvider -ListAvailable
$ResourceProviders | Where-Object { $_.ProviderNamespace -eq 'microsoft.insights' } | 
 Select-Object ProviderNamespace, RegistrationState

If like with me, it's not registered...

image

...you can use the script below to enable the provider:

Register-AzureRmResourceProvider -ProviderNamespace microsoft.insights

You should see a message like below saying it's Registering:

image

This takes a little while to run sometimes, you can run the last part of the script above every now and then to see when it's registered:

$ResourceProviders = Get-AzureRmResourceProvider -ListAvailable
$ResourceProviders | Where-Object { $_.ProviderNamespace -eq 'microsoft.insights' } | 
 Select-Object ProviderNamespace, RegistrationState

You should see it saying Registered as below when done:

image

Before doing the switch, we need to make sure we have a resource group in the target subscription to move the resource to, you can create a new resource group like below:

New-AzureRmResourceGroup -Name "BeemingBlog" -Location "West Europe"

We can now get our source resource Id and perform the move. For this, we are going to switch context to the source subscription, find the resource we want, get it's resource id and then finally call Move-AzureRmResource to move the resource.

Set-AzureRmContext -Subscription $SourceSubscriptionId
$SourceResource = Get-AzureRmResource -ResourceGroupName "BeemingBlog" -ResourceName "beemingblog"
$SourceResource
$ResourceId = $SourceResource | Where-Object { $_.ResourceType -eq "microsoft.insights/components" } | 
 Select-Object -ExpandProperty ResourceId { $_ }
$ResourceId
Move-AzureRmResource -ResourceId $ResourceId -DestinationSubscriptionId $DestinationSubscriptionId 
-DestinationResourceGroupName "BeemingBlog" -Force

This would take a little while to complete usually, when done, your resource should be in the subscription you want it in... if yes, there's a good chance you are going to be creating a support ticket soon Smile with tongue out. This is where you would leave off the subscription id if you are moving between resource groups instead of subscriptions.

Last Steps

All that's left now is to go through any dashboards and existing scripts that reference this resource and update them. Because the resource is no longer at its old location, your dashboards will become less 'metricky'. Smile

image

Full Script

The full script for the above is below including some variables to make the replacements easier:

# Set some variables that are going to be used later
$SourceResourceGroupName = "BeemingBlog"
$SourceResourceName = "beemingblog"
$DestinationResourceGroupName = "BeemingBlog"
$DestinationResourceGroupLocation = "West Europe"

#------------------------

# Login
Login-AzureRmAccount

# The source and destination subscriptions must exist within the same Azure Active Directory tenant
$Subscriptions = Get-AzureRmSubscription
$Subscriptions | Format-Table

# Set selected source and destination subscription ids
$SourceSubscriptionId = "b69cbba1-9087-4d8a-8e9f-861651ed3362"
$DestinationSubscriptionId = "3259386d-3d64-40f1-bad4-1a65f8daea4d"

# The service must enable the ability to move resources. This article lists which services 
# enable moving resources and which services do not enable moving resources.
Set-AzureRmContext -Subscription $DestinationSubscriptionId
$ResourceProviders = Get-AzureRmResourceProvider -ListAvailable
$ResourceProviders | Where-Object { $_.ProviderNamespace -eq 'microsoft.insights' } | 
 Select-Object ProviderNamespace, RegistrationState

# Register the provider
Register-AzureRmResourceProvider -ProviderNamespace microsoft.insights

# Create target resource group
New-AzureRmResourceGroup -Name $DestinationResourceGroupName -Location $DestinationResourceGroupLocation

# Get Source resource
Set-AzureRmContext -Subscription $SourceSubscriptionId
$SourceResource = Get-AzureRmResource -ResourceGroupName $SourceResourceGroupName 
 -ResourceName $SourceResourceName
$SourceResource
$ResourceId = $SourceResource | Where-Object { $_.ResourceType -eq "microsoft.insights/components" } | 
 Select-Object -ExpandProperty ResourceId { $_ }
$ResourceId
Move-AzureRmResource -ResourceId $ResourceId -DestinationSubscriptionId $DestinationSubscriptionId 
-DestinationResourceGroupName $DestinationResourceGroupName -Force

Conclusion

There is quite a lot of functionality that is available to us using the sdks like PowerShell that we can't do in the Azure Portal UI. Sometimes, you just need to figure it out, in this case there was a good doc to follow.

Happy migrating! Open-mouthed smile

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Gordon Beeming (RSA)
Software Developer Nologo Studios
South Africa South Africa
Gordon Beeming works at Nologo Studios in the sunny city of Durban, South Africa. He is the Lead for the Data and Services Team and has a strong focus on Developer Efficiencies and R&D. When he's not hacking away at a keyboard in Visual Studio he'll generally be relaxing with his family or hitting the black top getting in some mileage. He is a Visual Studio ALM Rangers, Visual Studio ALM MVP.

http://beeming.net

You may also be interested in...

Pro
Pro

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180910.1 | Last Updated 3 Jan 2018
Article Copyright 2018 by Gordon Beeming (RSA)
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid