Create Environment with Azure Deployment Slots
When deploying your application using a serverless solution, for example, Azure Functions or Azure App Service, you can use deployment slots to set up environments, such as a staging environment.
The deployment slots can be used for A/B testing and canary deployment, controlling the exposition of a specific version to a particular public or a slice of your requests. Then, promoting the tested version into your main slot is easy as clicking the swap button.
The Scenario
Recently I had to replicate my test environment, used for user acceptance test, to make it available to an external vendor, as a development environment.
The application is currently a colossal monolith application that, in the last couple of years, has been decoupled in separate components — or modules.
The monolith runs on a Virtual Machine hosted on an IIS, and it was decided just to duplicate the VM and configure it to be the development environment.
But the modularized components are already developed and planned to run in the cloud as AppService. For those components, it was decided to create the deployment slot.
Creating the Development Slot
On the Azure Portal, we need to locate the App Service component and access the Deployment Slots configuration on the side menu. For now, our App Service contains one single slot marked as “production”.
The creation of a new slot is an easy task. We just need to click the “Add Slot” button and then fill out the name of the development slot.
In this case, we defined the suffix “dev” and determined to copy the configurations from the original slot. By doing this, the configuration of the application would be just setting the values of the development environment in the application settings.
After the creation, the slot is listed and ready to use. Note that we will keep the traffic of the new slot as 0% because we want to access it directly by its name for testing purposes.
The next step of my development environment creation is to get the publishing profile, which contains the information to automatize the publishing of the application using a CI tool. In this case, our pipelines are managed by Jenkins.
After downloading the publishing profile file, we have access to the publishing URL, user name, and user password.
Note that this file can also be imported on Visual Studio for configuring the publishing from the IDE.
On Jenkins, my task was to create a new pipeline based on the existing one. On the new pipeline, I just changed the branch source of my code, the publishing URL, and the credentials for publishing.
Job’s done? Well, not yet. I mentioned before that apart from the monolith application, I have modules and components, each one with its Web App and Web API. In total, I had to do this procedure for 30 Azure Resources.
Automate With Azure CLI
The Azure Portal is an excellent tool and easy to use, but for repetitive tasks, nothing better than automate.
To be honest, it wouldn’t take too much time to do it manually on the Azure Portal, but that was an excellent opportunity to train CLI skills. Besides, I’m new in Mac world, so training bash script is also good at any chance.
By the way, if you wonder whether it is possible to run Azure CLI on Mac, it is! And I explain how in the following article:
To exemplify the case, I created the following App Services on a specific resource group:
The objective of our script is to loop the resources of type App Service and for every resource, create a deployment slot, copying the configuration of the default slot of every resource.
Visual Studio Code
The commands can run on the Terminal, but creating the script and editing on Visual Studio Code make things easier. Especially if you install the Azure CLI Extension for Visual Studio Code.
It will provide code snippets, intelliSense, integrated terminal, and more.
We go ahead and create a new file. Save it with the extension “azcli”, so the Visual Studio Azure CLI Extension will recognize that you are working on a CLI file.
Open the terminal (Shift+Control+`) and login into Azure with command az login.
When editing the file, you can select the lines you want to execute and use (Command+’) to run the selected code on the terminal.
So, let’s start our script. We will first set the resource group and list all the resources within this resource group with the following command:
rg=rg-training
az resource list — resource-group $rg — resource-type Microsoft.Web/Sites — query “[?kind == ‘app’].name” — output tsv
The result shows the list of current App Services on text output. Note the parameters "--resource-type" and "--query" to list only App Service resources. Also, the parameter "--output tsv" will format the output as text, instead of the default JSON.
Let’s change a little bit, and instead of listing the resources, we will put the result of the query into a variable list and print the name of each resource in this loop:
rg=rg-training resourceList=$(az resource list — resource-group $rg — resource-type Microsoft.web/sites — query “[?kind == ‘app’].name” — output tsv) for resource in $resourceList do echo $resource done
Note that the output, as expected, is the same:
Finally, we will remove the echo line and replace with the two following sentences:
az webapp deployment slot create — name $resource — resource-group $rg — slot dev — configuration-source $resource az webapp deployment list-publishing-credentials — name $resource — slot dev — resource-group $rg > $resource’-dev-publishing’.json
The first sentence will create the new deployment slot, using the configuration source from the current slot of the resource.
The second sentence will save a file with the publish profile, similar to the one we downloaded using Azure Portal.
The full final code is the following:
rg=rg-training resourceList=$(az resource list — resource-group $rg — resource-type Microsoft.web/sites — query “[?kind == ‘app’].name” — output tsv) for resource in $resourceList do az webapp deployment slot create — name $resource — resource-group $rg — slot dev — configuration-source $resource az webapp deployment list-publishing-credentials — name $resource — slot dev — resource-group $rg > $resource’-dev-publishing’.json done
Again, select the whole text and press (Command+’) to execute the script. Depending on the number of resources, it can take some time to run. The script will automate the task, but the commands will wait for the completion of each line to execute the next. For this exercise, with six App Services and creating six deployment slots, the execution took me no more than 3 minutes.
The terminal will list the output of the deployment slot create command for every resource created. Note that the terminal will not give any status on the publishing profile because we are directing the output to a file that will be saved on the disk. As I’m running those commands on the home of my current logged user, that’s where the files are saved:
Back to Azure Portal, we can check that all the dev Deployment Slots were successfully created:
Conclusion
As I already mentioned, the Azure Portal is a fantastic tool, but when you need to do a repetitive task, creating a script is an excellent way to achieve the results.
Visit the official reference guide for all the Azure CLI commands:
By using the Azure CLI and scripting, it will not only save you some time but will make sure that the task will be consistent with the logic applied, avoiding typing mistakes and skipping a step on the process.
To be honest, in this specific case, I spent some time doing the research, installing CLI tools, finding the commands, and running POCs to make sure everything was going to work as expected. But I’m sure it worth learning and next similar task; I will surely use the Azure CLI for scripting again.
I hope you learned something new today. And as usual, don’t forget to follow, comment, react, and check the other articles of DevOps, Cloud & IT Career Publication!