Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Azure icon
Labs

Toggle Deployment Slots on an Azure Function App Using AZ CLI

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.

Azure icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 45m
Published
Clock icon Feb 28, 2020

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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.
  2. Challenge

    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.

  3. Challenge

    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.

  4. Challenge

    Open the Project in Visual Studio Code

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

    cd \code\slots
    

    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.

  5. Challenge

    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.

  6. Challenge

    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 
    
  7. Challenge

    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.

  8. Challenge

    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
    
  9. Challenge

    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
    
  10. Challenge

    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.

  11. Challenge

    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.

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans