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.
- Open a browser.
- Navigate to the provided Azure Portal URL.
- Use the supplied username and password to authenticate.
- Create the HTTP-Triggered Function
Starting in the Azure dashboard perform the following tasks:
- Open the navigation menu in the upper-left of the Portal.
- 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); log.LogInformation(customSetting); 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
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).
When the Welcome to Azure Cloud Shell panel opens at the bottom of the browser, click the PowerShell link.
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.