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

Updating Azure Function Environment Variables with the Azure CLI

In this hands-on lab, we use the Azure CLI to modify an Azure Function App environment variable.

Azure icon
Labs

Path Info

Level
Clock icon Intermediate
Duration
Clock icon 30m
Published
Clock icon Nov 17, 2021

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

    Create the HTTP-Triggered Function

    1. Take note of the Location of the lab-generated resources as you will need this later in the lab.

    2. From the Overview page, click on the function name starting with fa-.

    3. Under Create functions in your preferred environment, click the Create in Azure portal button.

    4. In the Create function pane that opens on the right, click HTTP trigger.

    5. Click Create.

    6. Once created, in the left-hand navigation menu, click Code + Test.

    7. Starting from line 8 and going down, delete the existing code.

    8. Replace the code with the following:

      public static async Task<IActionResult> Run(HttpRequest req, ILogger log)
      {
          log.LogInformation("Azure Function Demo - Accessing Environment variables");
          var customSetting =  Environment.GetEnvironmentVariable("MySetting", EnvironmentVariableTarget.Process);
          log.LogInformation(customSetting);
          return new OkObjectResult($"{customSetting}");
      }
      
    9. Click Save.

    10. Click Test/Run

    11. Click the popup at the top to accept CORS. Then click Run.

    12. Review the Logs at the bottom of the page and check for any errors.

  3. Challenge

    Create the MySetting Environment Variable

    1. Using the navigation links at the top of the page, return to the function.
    2. In the left-hand navigation menu, under Settings, click Configuration.
    3. Click + New application setting
    4. In the Name box, enter MySetting.
    5. In the Value box, enter Set in the portal.
    6. Click OK.
    7. Click Save.
    8. Click Continue.
  4. Challenge

    Run the Function and Examine the Output

    1. Back on the main function page, down toward the bottom of the page, click on HttpTrigger1.
    2. Click Code + Test.
    3. Click Test/Run and review the HTTP response content.
  5. Challenge

    Set the Environment Variable Using Azure CLI

    1. Using the icon in the top-right corner of the page, activate the Cloud Shell.

    2. When prompted, click PowerShell.

    3. Click Show advanced settings.

    4. For Region, select the same region that the lab-generated resources were created in.

    5. Under Storage account, select Use existing.

    6. Under File share, select Create new.

    7. Enter shell for the name.

    8. Click Create storage.

    9. Retrieve the function app name and verify the information:

      $functionapp = (Get-AzFunctionApp).name
      
      $functionapp
      
    10. Retrieve the resource group name and verify the information:

      $resourcegroup = (Get-AzResourceGroup).ResourceGroupName
      
      $resourcegroup
      
    11. Update the application setting to Set with PowerShell CLI:

      Update-AzFunctionAppSetting -Name $functionapp -ResourceGroupName $resourcegroup -AppSetting @{"MySetting" = "Set with PowerShell CLI"}
      
  6. Challenge

    Run the Function and Examine the Output

    1. Back on the Code + Test page, click Run.
    2. Verify the updated output.
    3. In the top-left corner of the Cloud Shell, click the PowerShell dropdown menu.
    4. Switch to Bash.
    5. When prompted, click Confirm.
    6. Set the function app name:
      funcappname=$(az functionapp list --query "[0].name" -o tsv)
      
    7. Set the resource group name:
      group=$(az group list --query "[0].name" -o tsv)
      
    8. Update the application setting to Set with Bash CLI:
      az functionapp config appsettings set --name $funcappname -g $group --settings MySetting="Set with Bash CLI"
      
    9. Back on the Code + Test page, click Run.
    10. Verify the updated output.

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