Azure App Configuration Provider Library for Python

2.4.0 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart demonstrates how to connect to Azure App Configuration using Azure Active Directory, load configurations (including using `SettingSelector` for filtering), and access values like a dictionary. It also shows how to enable and check feature flags. Ensure `AZURE_APPCONFIG_ENDPOINT` is set as an environment variable and the executing principal has 'App Configuration Data Reader' role.

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}")

view raw JSON →