

Adam Storr - Harness Azure CLI to Keep Your App Service Name
source link: https://adamstorr.azurewebsites.net/blog/harness-azure-cli-to-keep-your-app-service-name
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Harness Azure CLI to Keep Your App Service Name
In this post we walk through the steps I used to delete and recreate an Azure App Service under a different App Service Plan using the Azure CLI so that I could keep the *.azurewebsites.net name for consistency.
Published on 23 March 2021
Warning
This worked for me.
It may not work for you.
Your mileage may vary.
You've been warned.
You run this at your own risk!
Introduction
I had the requirement recently to move an app service from one app service plan to another. This doesn't feel like an edge use case however the Azure portal does not allow for you to initiate a move especially if you are moving from a Windows based app service plan to a Linux based one or at least that I could find. One option was to create a new app service under the new app service plan but it would have a new name (as they have to be unique across Azure) and I initially wanted the same name so it would continue to work before I setup a custom domain on it.
The next idea was if I get all the wizard steps filling in ready in one browser window and then delete the original in another window, then click create in the original window, I would be able to keep the name without anyone sneaking in and taking it. This felt very risky. Now I don't think anyone would be able to register the same named app service at the exact same time I was deleting mine but I didn't want to risk it.
This left me with investigating the Azure CLI and scripting the change. I was aware that you can perform the actions I required (delete and create of a web app) through the Azure CLI so this felt like the right approach to take.
Requirements
I had a couple of base requirements I wanted to achieve with the app service move.
Keep the name
Move from a Windows based plan to a Linux based plan to keep costs down
Move to a paid for plan to allow for custom domains.
Prerequisites
The script I will show below assumes that the target App Service Plan has already been provisioned and you have the name. You will need your source/original resource group name and the target resource group name. For me they were different however I would expect this to work fine with the same named resource group.
It is also assumed that the Azure CLI is intalled on your computer which the scripts are being run.
Backup
Make a backup of your items you are moving. The script defined below WILL delete everything the current app service has deployed to it.
This is NOT a script which will move the app service, it will destroy it and create another with the same name in a different place.
Scripts
Login
First you need to log into your Azure account with the Azure CLI
az login
This will launch a browser window for you to authenticate with Azure.
Subscription Selection
The next step is you need to select your subscription. If you have only one then this will be selected automatically otherwise you will need to select the required subscription.
Note: I have not tried this across subscriptions. I'd imagine there will be more steps to do if going across subscriptions as well. Good luck!
az account set --subscription "Pay-As-You-Go"
In my case the subscription name was "Pay-As-You-Go".
Variables
It is now time to setup some variables for the script to use.
# original resource group
$Original_RG = "source-rg"
# Target rg
$Target_RG = "target-rg"
# Target app service plan
$Target_App_Service_Plan = "target-app-service-plan"
# name of app which is being re-created
$APP_NAME = "my-awesome-app"
The names of the variables should be self explainatory and these will be used throughout the rest of the script actions.
Delete Original App Service
The only way I could find to move an app service between plans was to delete and re-create it on the new app service plan. I couldn't find a way to move it through the portal but there maybe a way through the CLI. However I did not go for this route, deleting and re-creating was fine for my needs.
# DELETE the old one
az webapp delete `
--name $APP_NAME `
--resource-group $Original_RG
First you delete the app from the original resource group. This is using the az webapp delete
command. To delete an app I found you only needed the name and the resource group which it was defined in.
Create New App Service
Next is to create the app service on the target plan with the same name as before.
# Create the new one
az webapp create `
--resource-group $Target_RG `
--name $APP_NAME `
--plan $Target_App_Service_Plan `
--runtime '"DOTNET|5.0"'
This is done through the az webapp create
command. It requires the target resource group, the name (which you wanted to keep in the first place!), the target app service plan and the target runtime.
The target runtime is important as this defines what framework your application is going to be running under. In the Azure portal this is driven by the "Runtime Stack" dropdwn in the creation wizard. The dropdown values relate to specific "enum" type values which you will need to determine for your specific scenario.
And that's it. I found in less than 20 seconds I had deleted my application and re-created it in the new plan and kept the name!
Deploy
Once you have created the target app service in it's new home there will not be anything deployed to it as it is essentially "brand new". If you deploy your code through AzureDevOps, Github or another CI/CD pipeline you will need to update the variables, targets, resource groups, connections etc. as required and re-deploy your application.
If you manually deploy your website or application then you may want to investigate automation. It might sound scary but it makes life so much easier.
Conclusion
In this post we have run through the steps I used to move an app service from one app service plan to another in Azure while keeping the same name. It is something I will be looking to do for some other apps in the future which is why I am primarily documenting it here for personal use however I hope it helps others in the future as well.
Any questions/comments then please contact me on Twitter @WestDiscGolf
Recommend
-
12
Becoming a Remote Worker - Month 9+ 9 months of remote working, Christmas and how Covid has affected me. Published on 27 December 2020 I've not written about the past 3 mon...
-
5
Beginning to Make Testing with HttpClient Easier Introduction to the basics of setting up the HttpClient Interception library from Just Eat Tech to allow for testing items which rely on HttpClient. Publ...
-
8
Catching all the Requests while Testing with HttpClient Making sure tests fail if not all request matches are defined using the HttpClient Interception library from Just Eat Tech. Published on 11 May 20...
-
9
Default Your FirstOrDefault in .NET 6.0 New overrides coming for commonly used linq extension methods, will you be using them? Published on 22 June 2021 Intr...
-
6
Thoughts and Musings of a Standing Desk 1 Month On Thoughts and review of the first month of being a standing desk user Published on 06 July 2021 Introductio...
-
10
Becoming a Remote Worker - Month 18+ 18 months on with no end in sight ... Published on 17 September 2021 So this week clocks over the 18 month mark of remote...
-
8
Is Accessing Querystring Values in Azure Functions Hard? Accessing querystring values on web requests is an important concept for any modern APIs. But how can it be done in Azure Functions? Published on...
-
8
Dev Tips - Stand Up for Stand Up Are you still sitting down for your daily "stand up"? Then why are you not standing? Published on 13 October 2021
-
11
Dev Tips - Why Soduku can improve your Debugging Can playing number games improve code debugging? Let's find out! Published on 21 October 2021
-
6
Using AutoFac with .NET 6 Isolated Azure Functions Having the ability to use the Inversion of Control container of choice in your application is important. But can you use AutoFac with Azure Functions? Let's find out!...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK