Skip to main content

Update Azure Function Environment Variables with Azure CLI

Hands-On Lab

 

Photo of

Training Architect

Length

00:30:00

Difficulty

Intermediate

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

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Update Azure Function Environment Variables with Azure CLI

Introduction

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

Scenario

You want to develop your function appication 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.

Log In to the Azure Portal

  1. Log in to the Azure Portal using the credentials provided on the lab instructions page.

Create the HTTP-Triggered Function

  1. Open the menu in the top-left corner and select All resources.

  2. Click on the function name starting with fa-.

  3. Click the plus sign next to Functions in the left-hand pane.

  4. Select the box for In-portal.

  5. At the bottom of the screen, click Continue.

  6. Select the box for Webhook + API.

  7. At the bottom of the screen, click Create.

  8. Replace the function code with the following code.

    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 and run.

  1. Verify a 200 OK return status.

Create the MySetting Environment Variable

  1. In the left-hand pane, click the funtion name starting with fa-.

  2. Click the Platform features tab.

  3. Under General Settings, click Configuration.

  4. Click + New application setting.

  5. In the Name box, enter "MySetting" without the quotes.

  6. In the Value box, enter "Set in the portal" without the quotes.

  7. Click OK.

  8. Click Save.

Run the Function and Examine the Output

  1. At the top of the window, click the link for the function name starting with fa-.

  2. In the left-hand pane, under Functions, click HttpTrigger1.

  3. Click Run.

  4. Verify the output says "Set in the portal".

Set the Environment Variable using Azure CLI

  1. In the top ribbon, click the button for the CloudShell.

  2. In the CloudShell pane, click PowerShell.

  3. Click Show advanced settings.

  4. Under File share, select Create new.

  5. Enter "shell" without quotes in the box provided under File share.

  6. Click Create storage.

  7. Retrieve the name of the function app and resource group.

    $funcappname = $(az functionapp list --query "[0].name" -o tsv)
    $group = $(az group list --query "[0].name" -o tsv)
  8. Set the value of the environment variable.

    az functionapp config appsettings set --name $funcappname -g $group --settings MySetting="Set with CLI"

Run the Function and Examine the Output

  1. Back in the main Portal view, click Run.

  2. Verify the output says "Set with CLI".

Conclusion

Congratulations, you've completed this hands-on lab!