In this article, I’m going to show you how to create and manage Azure deployment slots using Azure CLI.
Creating an Azure Web App Resource
First, you need to log in to your Azure account. To do so, execute the following command:
Then you can proceed with the creation of the resource group, app service plan, and only then the web ap resource.
# Login to your Azure account az login # Create a new resource group az group create -n training-rg -l westus # Create a new app service plan az appservice plan create -g training-rg -n training-plan --sku S1 # Create a new Web App resource az webapp create -g training-rg -p training-plan -n origintechnologiestraining
Add a deployment slot
To create a new slot, use the following command:
az webapp deployment slot create -n origintechnologiestraining -g training-rg --slot staging
Create and deploy a .NET 5 application
Now that your Azure resources are ready, it’s time to create your web application. In this demo, I’m going to use the dotnet CLI to create a new API project.
dotnet new webapi -o Training -f net5.0
To run your application locally run the following command
dotnet run -p Training\Training.csproj Building... info: Microsoft.Hosting.Lifetime Now listening on: https://localhost:5001 info: Microsoft.Hosting.Lifetime Now listening on: http://localhost:5000 info: Microsoft.Hosting.Lifetime Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime Hosting environment: Development info: Microsoft.Hosting.Lifetime Content root path: C:\Users\ivanp\Desktop\Training
Deploy your application
It’s time to deploy your application. To do so, you will have to compile and create a release package of your application and its dependencies, compress the package and then use kudu to push it to Azure. This might sound not very easy by Its just a matter of few commands.
# Compile and publish the application and its dependencies dotnet publish -f net5.0 -o Release -c Release Training.csproj # Create a ZIP archive of the output folder of the dotnet publish command Compress-Archive -Path Release/* -DestinationPath release.zip # Deploy the ZIP file to your web app using the kudu zip push deployment az webapp deployment source config-zip -g training-rg -n origintechnologiestraining --src release.zip
The previous instructions are the same for a deployment slot except the last one, which will also specify the slot name with the -s parameter.
az webapp deployment source config-zip -g training-rg -n origintechnologiestraining --src release.zip -s staging
Congratulation! You have successfully deployed your new API on Azure.
The routing traffic configure between slots is pretty straightforward with Azure CLI. In fact, you can do it by simply executing the following command:
az webapp traffic-routing set --distribution staging=50 --name origintechnologiestraining -g training-rg
Swap deployment slot
To swap two slots, execute this command in your command prompt:
az webapp deployment slot swap -g training-rg -n origintechnologiestraining --slot staging --target-slot production