- Lab
- A Cloud Guru
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.
Path Info
Table of Contents
-
Challenge
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.
-
Challenge
Create the HTTP-Triggered Function
-
Take note of the Location of the lab-generated resources as you will need this later in the lab.
-
From the Overview page, click on the function name starting with fa-.
-
Under Create functions in your preferred environment, click the Create in Azure portal button.
-
In the Create function pane that opens on the right, click HTTP trigger.
-
Click Create.
-
Once created, in the left-hand navigation menu, click Code + Test.
-
Starting from line 8 and going down, delete the existing code.
-
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}"); }
-
Click Save.
-
Click Test/Run
-
Click the popup at the top to accept CORS. Then click Run.
-
Review the Logs at the bottom of the page and check for any errors.
-
-
Challenge
Create the MySetting Environment Variable
- Using the navigation links at the top of the page, return to the function.
- In the left-hand navigation menu, under Settings, click Configuration.
- Click + New application setting
- In the Name box, enter MySetting.
- In the Value box, enter Set in the portal.
- Click OK.
- Click Save.
- Click Continue.
-
Challenge
Run the Function and Examine the Output
- Back on the main function page, down toward the bottom of the page, click on HttpTrigger1.
- Click Code + Test.
- Click Test/Run and review the HTTP response content.
-
Challenge
Set the Environment Variable Using Azure CLI
-
Using the icon in the top-right corner of the page, activate the Cloud Shell.
-
When prompted, click PowerShell.
-
Click Show advanced settings.
-
For Region, select the same region that the lab-generated resources were created in.
-
Under Storage account, select Use existing.
-
Under File share, select Create new.
-
Enter shell for the name.
-
Click Create storage.
-
Retrieve the function app name and verify the information:
$functionapp = (Get-AzFunctionApp).name
$functionapp
-
Retrieve the resource group name and verify the information:
$resourcegroup = (Get-AzResourceGroup).ResourceGroupName
$resourcegroup
-
Update the application setting to
Set with PowerShell CLI
:Update-AzFunctionAppSetting -Name $functionapp -ResourceGroupName $resourcegroup -AppSetting @{"MySetting" = "Set with PowerShell CLI"}
-
-
Challenge
Run the Function and Examine the Output
- Back on the Code + Test page, click Run.
- Verify the updated output.
- In the top-left corner of the Cloud Shell, click the PowerShell dropdown menu.
- Switch to Bash.
- When prompted, click Confirm.
- Set the function app name:
funcappname=$(az functionapp list --query "[0].name" -o tsv)
- Set the resource group name:
group=$(az group list --query "[0].name" -o tsv)
- Update the application setting to
Set with Bash CLI
:az functionapp config appsettings set --name $funcappname -g $group --settings MySetting="Set with Bash CLI"
- Back on the Code + Test page, click Run.
- Verify the updated output.
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.