Toggle Deployment Slots on an Azure Function App Using AZ CLI

45 minutes
  • 11 Learning Objectives

About this Hands-on Lab

In this lab, we use Azure CLI to create multiple slots in an Azure Function App Service, then deploy two different versions of an app to each slot. Once that is finished, we run the app in each slot, swap the two slots, and then run the apps again. Finally we verify that the output has swapped.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Log In to the Azure Portal

Log in to the Azure Portal using the username and password supplied by the lab.

  1. Open a browser.
  2. Navigate to the provided Azure Portal URL.
  3. Use the supplied username and password to authenticate.
Remote into the Windows VM

Starting in the Azure dashboard, perform the following tasks:

  1. Open the navigation menu in the upper-left of the Portal.
  2. Click on All resources.

Wait for all the resources to appear.

In the list, click on the lab-VM resource.

Wait for the VM overview page to open. On that page, make note of the public IP address.

Using a remote desktop client, connect to the IP address of the VM. Log in using the credentials provided on the lab page.

Accept the certificate if asked to do so.

Wait for the desktop to load and stabilize. If the server manager opens, click ‘No’ for network sharing and close it.

Install Azure Functions Core Tools

Open PowerShell ISE as an administrator. Open a new script window. Paste the following into the editor window (not the blue pane).

choco install nodejs-lts -y --force
npm i -g azure-functions-core-tools@3 --unsafe-perm true
code --install-extension ms-vscode.csharp
code --install-extension ms-vscode.azurecli
code --install-extension ms-azuretools.vscode-azurefunctions
code --install-extension ms-vscode.azure-account
code --install-extension ms-azuretools.vscode-azurestorage
code --install-extension ms-vscode.powershell

Run the script by pressing F5 or the play button.

Wait a few minutes for it to complete.

Close the PowerShell ISE window.

Open the Project in Visual Studio Code

Open an administrative command prompt. Change the directory using the following command:

cd codeslots

Now open the project in visual studio code with the following command:

code .

Wait for Visual Studio Code to open the solution.

On the toast message about detecting an Azure Functions project being found in the folder, press the Yes button.

On the toast message about "There are unresolved dependencies…", Click the Restore button.

Log In to Azure with AZ CLI

In Visual Studio Code, open the file scripts.ps1.

Highlight the line with the code:

az login

Now press F8 to run the single line of code.

Internet Explorer will open. One tab will open with a login form (there will be several other nag screens we need to avoid or close). Log in with the Azure credentials given by the lab environment, the same ones used to log in to the Portal.

When the login is complete, Internet Explorer will say so, and the az login commnand will return in the Visual Studio Code terminal.

Retrieve the Name of the Function App and Resource Group

Execute the following lines of code in the scripts.ps1 file:

$funcappname = $(az functionapp list --query "[0].name" -o tsv)
$funcappname

$group = $(az group list --query "[0].name" -o tsv)
$group 
Create the Staging Slot and Production Slot

Execute the following lines of code in the scripts.ps1 file:

az functionapp deployment slot create --name $funcappname -g $group --slot production
az functionapp deployment slot create --name $funcappname -g $group --slot staging

Note that the production slot is already created, and the one line has essentially no effect and is present just for example.

Build and Deploy the Production Application

Execute the following lines of code in the scripts.ps1 file:

# modify code (A)
dotnet clean
dotnet build

func azure functionapp publish $funcappname --force --csharp
Update the Function

Open the MyHttpFunction.cs file. Modify the following line of code from:

return (ActionResult)new OkObjectResult($"A");

To:

return (ActionResult)new OkObjectResult($"B");

Then execute the following lines of code in the scripts.ps1 file:

# modify code (B)
dotnet clean
dotnet build

func azure functionapp publish $funcappname --slot staging --force --csharp
Run the Production and Staging Apps and Examine Output

In the Azure Portal, navigate to the Function App Service.

Starting in the Azure dashboard perform the following tasks:

  1. Open the navigation menu in the upper-left of the Portal.
  2. Click All resources.

Wait for all the resources to appear.

In the list, click the app service that has a name that starts with fa-. This will open the overview page of the function app service.

Click on the MyHttpFunction in the production slot near the top of the function apps navigation tree, not in the Slots section of this tree. When the function opens, press the Run button. The test panel will open and show "A" in the output panel.

Now click on the MyHttpFunction in the staging slot. When the function opens, press the Run button. The test panel will open and show "B" in the output panel.

Swap Slots, Run Functions, and Check Output

Back in Visual Studio Code, run the following line of the scripts.ps1 file:

az functionapp deployment slot swap -g $group -n $funcappname --slot staging --target-slot production 

Back in the Azure Portal, click on the MyHttpFunction in the production slot near the top of the function apps navigation tree, not in the Slots section of this tree. When the function opens, press the Run button. The test panel will open and show "B" in the output panel.

Now click on the MyHttpFunction in the staging slot. When the function opens, press the Run button. The test panel will open and show "A" in the output panel.

The functions in the two slots have been swapped.

Additional Resources

Your employer wants to build a CI/CD pipeline for your function apps. They want to have both a production slot and a staging slot. They also want to deploy continuously upon every code check and successful build into the staging slot. You decide to prototype the solution. This involves building the prototype with PowerShell and Azure Function Core Tools, creating the slots, deploying test applications, and swapping the slots.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?