Updating Azure Function Environment Variables with the Azure CLI

30 minutes
  • 5 Learning Objectives

About this Hands-on Lab

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

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.
Create the HTTP-Triggered Function

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 starting with fa-. This will open the overview page of the function app service.

On the function app overview page, click Functions in the function apps navigation tree on the left of the page.

Now click + New function at the top of the empty list of functions.

In the list of triggers, click HTTP trigger.

In the panel that opens, name the function "MyHttpFunction".

Click the Create button.

Wait for the code of the function to open in the browser.

Replace the function 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);
            return new OkObjectResult($"{customSetting}");

Then click Save above the code.

Create the `MySetting` Environment Variable

Click on the function app name in the navigation tree (it starts with fa- and is the root item of the tree) to go to the function app.

Click on the Platform features tab, and when that loads, click on General Settings -> Configuration.

When the Application settings page loads, click + New application setting. When the panel opens, enter the following:

  • Name: MySetting
  • Value: Set in the Portal

Press the OK button.

You will be taken back to Application settings. The list will now show MySettings. Make sure to press the Save button near the top of the page. Otherwise the setting will not actually be saved.

Go back to the function’s code page. Click the Run button. The test panel will open, the function will execute, and the output will show "Set in the portal".

Set the Environment Variable using Azure CLI
  1. Open an Azure Cloud Shell by clicking the cloud shell icon at the top of the page (it looks like a rectangle with a command-line prompt within it).

  2. When the Welcome to Azure Cloud Shell panel opens at the bottom of the browser, click the PowerShell link.

  3. A You have no storage mounted form will open. In this, select Create new in the File share section. For the name of the share, enter "shell". Then click the Create storage button.

The panel will become blue and the shell will take a moment to initialize.

When the prompt is available, run the following command:

$startUrl = "<url from the Durable HTTP starter function>"

Execute the following lines of code in the cloud shell:

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

Execute the following line of code in the cloud shell to set the environment variable:

az functionapp config appsettings set --name $funcappname -g $group --settings MySetting="Set with CLI"
Run the Function and Examine the Output

Go back to the function’s code page. Click the Run button. The test panel will open, the function will execute, and the output will show "Set with CLI.

Additional Resources

You want to develop your function application to conform with 12-Factor guidelines (12factor.net). You want to move configuration into the environment. You decide to prototype a function app to read and return environment an environment variable value via HTTP. You need to code the function, create the variable, change its value using Azure CLI, and test that it returns the proper value.

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?