Skip to content

Commit db68349

Browse files
committed
Dynamic Refresh AAD
1 parent f70acc6 commit db68349

File tree

1 file changed

+45
-3
lines changed

1 file changed

+45
-3
lines changed

articles/azure-app-configuration/enable-dynamic-configuration-python.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: mrm9084
77
ms.service: azure-app-configuration
88
ms.devlang: python
99
ms.topic: tutorial
10-
ms.date: 01/29/2024
10+
ms.date: 12/03/2024
1111
ms.custom: devx-track-python, devx-track-extended-python
1212
ms.author: mametcal
1313
#Customer intent: As a Python developer, I want to dynamically update my app to use the latest configuration data in Azure App Configuration.
@@ -38,6 +38,44 @@ Add the following key-value to your Azure App Configuration store. For more info
3838

3939
1. Create a new Python file named *app.py* and add the following code:
4040

41+
### [Microsoft Entra ID (recommended)](#tab/entra-id)
42+
43+
You use the `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](./concept-enable-rbac.md#authentication-with-token-credentials) to assign your credential the **App Configuration Data Reader** role. Be sure to allow sufficient time for the permission to propagate before running your application.
44+
45+
```python
46+
from azure.appconfiguration.provider import load, WatchKey
47+
from azure.identity import DefaultAzureCredential
48+
import os
49+
import time
50+
51+
endpoint = os.environ.get("APPCONFIGURATION_ENDPOINT")
52+
53+
# Connecting to Azure App Configuration using connection string
54+
# Setting up to refresh when the Sentinel key is changed.
55+
config = load(
56+
endpoint=endpoint,
57+
credential=DefaultAzureCredential(),
58+
refresh_on=[WatchKey("sentinel")],
59+
refresh_interval=10, # Default value is 30 seconds, shorted for this sample
60+
)
61+
62+
print("Update the `message` in your Azure App Configuration store using Azure portal or CLI.")
63+
print("First, update the `message` value, and then update the `sentinel` key value.")
64+
65+
while (True):
66+
# Refreshing the configuration setting
67+
config.refresh()
68+
69+
# Current value of message
70+
print(config["message"])
71+
72+
# Waiting before the next refresh
73+
time.sleep(5)
74+
```
75+
76+
77+
### [Connection string](#tab/connection-string)
78+
4179
```python
4280
from azure.appconfiguration.provider import load, WatchKey
4381
import os
@@ -66,6 +104,7 @@ Add the following key-value to your Azure App Configuration store. For more info
66104
# Waiting before the next refresh
67105
time.sleep(5)
68106
```
107+
---
69108

70109
1. Run your script:
71110

@@ -104,6 +143,7 @@ In `app.py`, set up Azure App Configuration to load your configuration values. T
104143

105144
```python
106145
from azure.appconfiguration.provider import load, WatchKey
146+
from azure.identity import DefaultAzureCredential
107147

108148
azure_app_config = None # declare azure_app_config as a global variable
109149

@@ -112,7 +152,8 @@ def on_refresh_success():
112152

113153

114154
global azure_app_config
115-
azure_app_config = load(connection_string=os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING")
155+
azure_app_config = load(endpoint=os.environ.get("AZURE_APPCONFIG_ENDPOINT"),
156+
credential=DefaultAzureCredential(),
116157
refresh_on=[WatchKey("sentinel")],
117158
on_refresh_success=on_refresh_success,
118159
refresh_interval=10, # Default value is 30 seconds, shortened for this sample
@@ -164,7 +205,8 @@ def on_refresh_success():
164205
app.config.update(azure_app_config)
165206

166207
AZURE_APPCONFIGURATION = load(
167-
connection_string=os.environ.get("AZURE_APPCONFIG_CONNECTION_STRING"),
208+
connection_string=os.environ.get(endpoint=os.environ.get("AZURE_APPCONFIG_ENDPOINT"),
209+
credential=DefaultAzureCredential(),
168210
refresh_on=[WatchKey("sentinel")],
169211
on_refresh_success=on_refresh_success,
170212
refresh_interval=10, # Default value is 30 seconds, shortened for this sample

0 commit comments

Comments
 (0)