Lesson 2 of 2 Container Hosting in Azure App Service

Supplying Environment Variables and Secrets in App Service

This lesson explains how app settings reach a container, how slot settings behave during swaps, and how Key Vault and App Configuration references supply values securely.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain how app settings become environment variables.
  • Describe the purpose of deployment slot settings.
  • Identify the requirements for a Key Vault reference.
  • Choose between plain settings and references.

App settings become environment variables

Twelve-factor application design keeps configuration outside the code, so that the same image runs in development, test and production with different settings. App Service supports this through app settings: name-value pairs stored with the app and injected into the container as environment variables at start-up. A Python application reads them with os.environ["NAME"] or os.getenv("NAME", default).

App settings are encrypted at rest and are not visible in the image. Changing any app setting restarts the app, because environment variables are read only when a process starts. Settings can be managed in the portal, with the Azure CLI, or in infrastructure as code. The following command sets two ordinary settings.

az webapp config appsettings set --resource-group RG --name APP \
  --settings EMBEDDING_DEPLOYMENT=text-embedding-3-small MAX_RESULTS=5

App Service also has a separate connection strings section. Each connection string is injected as an environment variable with a prefix that indicates its type, such as SQLAZURECONNSTR_, POSTGRESQLCONNSTR_ or CUSTOMCONNSTR_. This exists mainly for .NET applications; Python applications usually use app settings.

True or false

App settings

Exam skills 1.1.3

Decide whether each statement about App Service app settings is true.

Log in to try this activity Interactive activities are free with an account.

Slot settings

When an app uses deployment slots, each slot has its own app settings. By default a setting moves with the content during a swap: after swapping, production has the settings that staging had. A setting marked as a deployment slot setting (sticky setting) instead stays with its slot, so that values such as the database endpoint for staging and for production are never swapped.

A common mistake is to forget to mark environment-specific settings as sticky, so that a swap points production at the staging database. Settings that describe the application version, such as a feature toggle that ships with the new code, should normally move with the content.

Sort into groups

Sticky or swapped?

Exam skills 1.1.3

Decide whether each app setting should be a deployment slot setting.

Log in to try this activity Interactive activities are free with an account.

Key Vault references

Secrets such as a database password, an API key for an external model provider or a storage connection string should not be stored as plain app setting values, even though app settings are encrypted. Key Vault provides versioning, rotation, access policies per secret, and an audit log of every read. A Key Vault reference combines both: the app setting's value is a reference, and App Service fetches the secret from Key Vault and injects the secret's value as the environment variable.

A reference has one of two forms. The application code does not change, because it still reads an environment variable.

@Microsoft.KeyVault(SecretUri=https://VAULT.vault.azure.net/secrets/db-password/)
@Microsoft.KeyVault(VaultName=VAULT;SecretName=db-password)

Three conditions must be met for a reference to resolve. The app must have a managed identity; the identity needs permission to read secrets, normally the Key Vault Secrets User role on the vault; and the vault must be reachable from the app, which matters when the vault uses a firewall or a private endpoint. By default the system-assigned identity is used; the site property keyVaultReferenceIdentity selects a user-assigned identity instead.

When a reference has no version, App Service uses the latest version and fetches it again periodically (within 24 hours) or when the app's configuration changes, so a rotated secret reaches the app without editing the setting. A reference that names a version stays fixed to that version. The portal shows the resolution status of each reference, which is the first place to look when a secret appears empty.

The following diagram shows four steps that move a database password from an app setting into Key Vault, and the resulting app setting.

Four steps that move DB_PASSWORD into Key Vault without changing code: store the secret, enable the app's system-assigned managed identity, assign it Key Vault Secrets User on the vault, and replace the plain value with a version-less Key Vault reference. The code still reads the same environment variable, and each rotated version is picked up automatically.
VAULT stands for the vault name.
Troubleshooting

A Key Vault reference that does not resolve

Exam skills 1.1.3 · 4.1.1

Find why each Key Vault reference fails to resolve, choosing again until you find the fix.

Log in to try this activity Interactive activities are free with an account.

App Configuration references

In the same way, an app setting can refer to a key-value in Azure App Configuration with @Microsoft.AppConfiguration(Endpoint=https://STORE.azconfig.io; Key=Api:PageSize; Label=prod). The app's identity needs the App Configuration Data Reader role. If the App Configuration key-value is itself a Key Vault reference, the app's identity also needs access to the vault.

The two kinds of reference have different purposes, which the following comparison summarizes. Platform references are convenient for an app that needs a few values at start-up; an application that needs dynamic refresh or feature flags should load App Configuration with its provider library instead, as described in the App Configuration module.

Plain app setting
Value stored inApp Service
Suitable for secretsNo
Shared across appsNo
Needs a managed identityNo
Choose when A non-sensitive value used by one app
Key Vault reference
Value stored inKey Vault
Suitable for secretsYes
Shared across appsYes
Needs a managed identityYes, with Key Vault Secrets User
Choose when A secret that must be audited and rotated
App Configuration reference
Value stored inApp Configuration
Suitable for secretsOnly through a nested Key Vault reference
Shared across appsYes
Needs a managed identityYes, with App Configuration Data Reader
Choose when A setting shared by many apps and environments
Worked example

Removing a secret from configuration

An App Service container reads a database password from the app setting DB_PASSWORD, which holds the plain value. Auditors require that the password be stored in Key Vault, rotated every 90 days and read without any credential stored in the app. The code must not change. The app has no managed identity.

The solution has four steps. Store the password as a secret in Key Vault; enable the app's system-assigned managed identity; assign that identity Key Vault Secrets User on the vault; and change DB_PASSWORD to a version-less Key Vault reference. The code still reads the same environment variable. Because the reference has no version, App Service picks up each rotated version without another configuration change.

Rewriting the application to call the Key Vault SDK would also work, but violates the requirement that the code must not change. A reference that names a specific version would stop following rotations. Granting the identity Key Vault Administrator would work but exceeds least privilege.

Check yourself

After a slot swap, production starts using the staging database. Which setting option would have prevented this?

Show answer

Marking the database setting as a deployment slot setting. Sticky settings stay with their slot during a swap, while ordinary settings move with the content.

Exam Tips

Remember for the exam

  • App settings are injected as environment variables; changing one restarts the app.
  • Mark environment-specific settings as deployment slot settings so that swaps do not move them.
  • Key Vault references need a managed identity with Key Vault Secrets User and network access to the vault.
  • Version-less references follow the latest secret version.