0
0
Azurecloud~5 mins

Environment variables and configuration in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Apps often need settings like passwords or URLs that can change without changing the app code. Environment variables let you store these settings outside the app so you can update them easily and keep secrets safe.
When you want to keep database passwords separate from your app code to avoid exposing them.
When you need to change API keys or URLs without rebuilding your app.
When running the same app in different places like testing and production with different settings.
When you want to avoid hardcoding sensitive information in your app files.
When you want to manage app settings centrally in Azure for easier updates.
Config File - azure-pipelines.yml
azure-pipelines.yml
trigger:
- main

variables:
  - name: DATABASE_URL
    value: "Server=tcp:myserver.database.windows.net,1433;Initial Catalog=mydb;User ID=myuser;Password=MyP@ssw0rd;"
  - name: API_KEY
    value: "12345-abcde-67890-fghij"

jobs:
- job: BuildAndDeploy
  pool:
    vmImage: 'ubuntu-latest'
  steps:
  - script: |
      echo "Using database URL: $(DATABASE_URL)"
      echo "Using API key: $(API_KEY)"
    displayName: 'Show environment variables'

This Azure Pipelines YAML file defines two environment variables: DATABASE_URL and API_KEY. These variables store connection strings and keys outside the app code.

The variables section sets these values centrally.

In the steps, the script prints these variables to show how the app or pipeline can access them during build or deployment.

Commands
This command sets environment variables DATABASE_URL and API_KEY for the Azure Web App named 'my-app'. It stores these settings securely so the app can use them at runtime.
Terminal
az webapp config appsettings set --name my-app --resource-group my-resource-group --settings DATABASE_URL="Server=tcp:myserver.database.windows.net,1433;Initial Catalog=mydb;User ID=myuser;Password=MyP@ssw0rd;" API_KEY="12345-abcde-67890-fghij"
Expected OutputExpected
{"id":"/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/my-resource-group/providers/Microsoft.Web/sites/my-app/config/appsettings","name":"appsettings","type":"Microsoft.Web/sites/config","location":"East US","properties":{"DATABASE_URL":"Server=tcp:myserver.database.windows.net,1433;Initial Catalog=mydb;User ID=myuser;Password=MyP@ssw0rd;","API_KEY":"12345-abcde-67890-fghij"}}
--name - Specifies the name of the Azure Web App
--resource-group - Specifies the resource group containing the Web App
--settings - Defines the environment variables to set
This command lists all environment variables currently set for the Azure Web App 'my-app'. It helps verify that the variables were set correctly.
Terminal
az webapp config appsettings list --name my-app --resource-group my-resource-group
Expected OutputExpected
[{"name":"DATABASE_URL","value":"Server=tcp:myserver.database.windows.net,1433;Initial Catalog=mydb;User ID=myuser;Password=MyP@ssw0rd;"},{"name":"API_KEY","value":"12345-abcde-67890-fghij"}]
--name - Specifies the name of the Azure Web App
--resource-group - Specifies the resource group containing the Web App
This command restarts the Azure Web App 'my-app' to apply any changes made to environment variables or configuration.
Terminal
az webapp restart --name my-app --resource-group my-resource-group
Expected OutputExpected
No output (command runs silently)
--name - Specifies the name of the Azure Web App
--resource-group - Specifies the resource group containing the Web App
Key Concept

If you remember nothing else from this pattern, remember: environment variables let you change app settings safely without touching the app code.

Common Mistakes
Setting environment variables with incorrect syntax or missing quotes around values with special characters.
The command fails or the app reads wrong values, causing errors or security leaks.
Always wrap values containing special characters or spaces in double quotes when setting environment variables.
Not restarting the Azure Web App after changing environment variables.
The app continues running with old settings and does not pick up the new variables.
Run 'az webapp restart' after updating environment variables to apply changes.
Hardcoding sensitive information like passwords directly in app code instead of using environment variables.
This exposes secrets in code repositories and makes updates difficult and risky.
Use environment variables or Azure Key Vault to store secrets outside the app code.
Summary
Use 'az webapp config appsettings set' to add or update environment variables for your Azure Web App.
Verify environment variables with 'az webapp config appsettings list' to ensure they are set correctly.
Restart the app with 'az webapp restart' to apply any changes to environment variables.