Azure App Configuration Provider Library for Python
The `azure-appconfiguration-provider` is a Python library that enables applications to centralize and dynamically manage configuration settings using Azure App Configuration. It provides a dictionary-like interface for accessing settings, supports features like dynamic refresh, feature flags, and automatic resolution of Key Vault references. This library, currently at version 2.4.0, is actively maintained within the Azure SDK for Python, with frequent updates addressing features and bug fixes.
Warnings
- breaking The `load_provider` class method was replaced by a module-level `load` function in version 2.0.0. Code using `AzureAppConfigurationProvider.load_provider(...)` will break.
- breaking In version 2.0.0, the handling of `AzureAppConfigurationKeyVaultOptions` changed. The `secret_clients` parameter was removed, and `client_configs` should be used instead, mapping endpoints to client configurations.
- gotcha Dynamic refresh of configurations requires both setting `refresh_on` (with `WatchKey`) and explicitly calling the `config.refresh()` method. Simply setting `refresh_interval` without `refresh_on` or calling `refresh()` will not update configurations.
- gotcha Refreshing feature flags and regular configuration settings are independent. Enabling `feature_flags_enabled` allows feature flag refresh, but a change in a feature flag will not automatically trigger a refresh of other configuration settings, and vice-versa.
- gotcha Resolving Key Vault references requires providing separate Azure Active Directory credentials that have access to the Key Vault. The credentials used to access App Configuration are not automatically used for Key Vault.
- gotcha When using multiple `SettingSelector` objects, the order in which they are provided to the `selectors` parameter matters. Later selectors in the list will override values from earlier selectors if duplicate keys are found.
Install
-
pip install azure-appconfiguration-provider -
pip install azure-identity
Imports
- load
from azure.appconfiguration.provider import load
- SettingSelector
from azure.appconfiguration.provider import SettingSelector
- WatchKey
from azure.appconfiguration.provider import WatchKey
- DefaultAzureCredential
from azure.identity import DefaultAzureCredential
Quickstart
import os
from azure.appconfiguration.provider import load, SettingSelector
from azure.identity import DefaultAzureCredential
# Set your App Configuration endpoint as an environment variable, e.g., AZURE_APPCONFIG_ENDPOINT
endpoint = os.environ.get("AZURE_APPCONFIG_ENDPOINT", "")
if not endpoint:
print("Please set the AZURE_APPCONFIG_ENDPOINT environment variable.")
exit(1)
try:
# Authenticate with Azure Active Directory (recommended)
# Ensure your principal (user/service principal) has 'App Configuration Data Reader' role.
credential = DefaultAzureCredential()
# Load configuration from Azure App Configuration
# By default, loads all configurations with no label.
config = load(
endpoint=endpoint,
credential=credential,
# Example: Load 'TestApp:Settings:Message' and 'TestApp:Settings:FeatureX' with label 'prod'
# and all configs with no label, with 'prod' taking precedence.
selectors=[
SettingSelector(key_filter="TestApp:Settings:*", label_filter="prod"),
SettingSelector(key_filter="*", label_filter="\0") # '\0' represents no label
],
# Enable feature flags loading
feature_flags_enabled=True
)
print(f"Loaded configuration: ")
for key, value in config.items():
print(f" {key}: {value}")
# Access configuration values like a dictionary
message = config.get("TestApp:Settings:Message", "Default message")
print(f"\nMessage from App Configuration: {message}")
# Check a feature flag
if config.get("TestApp:Settings:FeatureX", False):
print("FeatureX is enabled.")
else:
print("FeatureX is disabled.")
except Exception as e:
print(f"An error occurred: {e}")