You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Tutorial for using Azure App Configuration Key Vault references in a Python app | Microsoft Docs
3
+
description: In this tutorial, you learn how to use Azure App Configuration's Key Vault references from a Python app
4
+
services: azure-app-configuration
5
+
author: mrm9084
6
+
ms.service: azure-app-configuration
7
+
ms.devlang: python
8
+
ms.topic: tutorial
9
+
ms.date: 03/16/2026
10
+
ms.author: mametcal
11
+
ms.custom: devx-track-python, devx-track-azurecli
12
+
#Customer intent: I want to update my Python application to reference values stored in Key Vault through App Configuration.
13
+
---
14
+
15
+
# Tutorial: Use Key Vault references in a Python app
16
+
17
+
In this tutorial, you learn how to implement Key Vault references in a Python application using App Configuration. It builds on the web app introduced in the quickstart. Before you continue, complete [Create a Python app with App Configuration](./quickstart-python-provider.md) first.
18
+
19
+
In this tutorial, you learn how to:
20
+
21
+
> [!div class="checklist"]
22
+
> * Create an App Configuration key that references a value stored in Key Vault.
23
+
> * Access the value of this key from a Python application.
24
+
25
+
## Prerequisites
26
+
27
+
* Azure subscription - [create one for free](https://azure.microsoft.com/pricing/purchase-options/azure-account?cid=msft_learn)
28
+
* Python 3.8 or later - for information on setting up Python on Windows, see the [Python on Windows documentation](/windows/python/)
29
+
* Finish the [Create a Python app with App Configuration](./quickstart-python-provider.md) quickstart.
30
+
31
+
## Create a key vault
32
+
33
+
1. Sign in to the [Azure portal](https://portal.azure.com), and then select **Create a resource**.
34
+
35
+
1. In the search box, enter **Key Vault**. In the result list, select **Key Vault**.
36
+
37
+
1. On the **Key Vault** page, select **Create**.
38
+
39
+
1. On the **Create a key vault** page, enter the following information:
40
+
- For **Subscription**: Select a subscription.
41
+
- For **Resource group**: Enter the name of an existing resource group or select **Create new** and enter a resource group name.
42
+
- For **Key vault name**: Enter a unique name.
43
+
- For **Region**: Select a location.
44
+
45
+
1. For the other options, use the default values.
46
+
47
+
1. Select **Review + create**.
48
+
49
+
1. After the system validates and displays your inputs, select **Create**.
50
+
51
+
At this point, your Azure account is the only one authorized to access this new vault.
52
+
53
+
## Add a secret to Key Vault
54
+
55
+
Add a secret to the vault to test Key Vault retrieval. The secret is called **Message**, and its value is "Hello from Key Vault."
56
+
57
+
1. On the Key Vault resource menu, select **Objects** > **Secrets**.
58
+
59
+
1. Select **Generate/Import**.
60
+
61
+
1. In the **Create a secret** dialog, enter the following values:
62
+
- For **Upload options**: Enter **Manual**.
63
+
- For **Name**: Enter **Message**.
64
+
- For **Secret value**: Enter **Hello from Key Vault**.
65
+
66
+
1. For the other options, use the default values.
67
+
68
+
1. Select **Create**.
69
+
70
+
## Add a Key Vault reference to App Configuration
71
+
72
+
1. Sign in to the [Azure portal](https://portal.azure.com). Select **All resources**, and then select your App Configuration store.
73
+
74
+
1. Select **Configuration Explorer**.
75
+
76
+
1. Select **+ Create** > **Key vault reference**, and then specify the following values:
77
+
***Key**: Enter **TestApp:Settings:KeyVaultMessage**.
78
+
***Label**: Leave this value blank.
79
+
***Subscription**, **Resource group**, and **Key vault**: Enter the values corresponding to the key vault you created in the previous section.
80
+
***Secret**: Select the secret named **Message** that you created in the previous section.
81
+
82
+
## Grant your app access to Key Vault
83
+
84
+
Your application uses `DefaultAzureCredential` to authenticate to both App Configuration and Key Vault. This credential automatically works with managed identities in Azure, and with your developer credentials locally.
85
+
86
+
1. Grant your identity access to Key Vault. Assign the **Key Vault Secrets User** role to your user account or managed identity:
87
+
88
+
```azurecli
89
+
az role assignment create --role "Key Vault Secrets User" --scope /subscriptions/<subscriptionId>/resourceGroups/<group-name>/providers/Microsoft.KeyVault/vaults/<your-unique-keyvault-name> --assignee <your-azure-ad-user-or-managed-identity>
90
+
```
91
+
92
+
1. Grant your identity access to App Configuration. Assign the **App Configuration Data Reader** role:
93
+
94
+
```azurecli
95
+
az role assignment create --role "App Configuration Data Reader" --scope /subscriptions/<subscriptionId>/resourceGroups/<group-name>/providers/Microsoft.AppConfiguration/configurationStores/<your-app-configuration-store> --assignee <your-azure-ad-user-or-managed-identity>
96
+
```
97
+
98
+
## Update your code to use a Key Vault reference
99
+
100
+
1. Install the required packages by running the following command:
1. Create an environment variable called **AZURE_APPCONFIG_ENDPOINT**. Set its value to the endpoint of your App Configuration store. You can find the endpoint on the **Overview** blade in the Azure portal.
# Connect to Azure App Configuration and resolve Key Vault references.
141
+
config = load(
142
+
endpoint=endpoint,
143
+
credential=credential,
144
+
keyvault_credential=credential,
145
+
)
146
+
147
+
# Access configuration values, including resolved Key Vault references.
148
+
print(config["TestApp:Settings:KeyVaultMessage"])
149
+
```
150
+
151
+
The `keyvault_credential` parameter tells the provider to use the given credential when resolving Key Vault references. The same `DefaultAzureCredential` instance is used for both App Configuration and Key Vault authentication.
152
+
153
+
> [!NOTE]
154
+
> If your Key Vault references point to multiple key vaults that require different credentials, you can use the `keyvault_client_configs` parameter instead to provide per-vault credentials. For more information, see the [Python provider reference](./reference-python-provider.md).
155
+
156
+
1. Run the application:
157
+
158
+
```console
159
+
python app.py
160
+
```
161
+
162
+
You see the message that you entered in App Configuration. You also see the message that you entered in Key Vault, resolved through the Key Vault reference.
In this tutorial, you created an App Configuration key that references a value stored in Key Vault. To learn more about the Python provider for Azure App Configuration, see the [Python provider reference documentation](./reference-python-provider.md).
#Customer intent: I want to update my Spring Boot application to reference values stored in Key Vault through App Configuration.
@@ -36,116 +36,75 @@ In this tutorial, you learn how to:
36
36
* Azure subscription - [create one for free](https://azure.microsoft.com/pricing/purchase-options/azure-account?cid=msft_learn)
37
37
* A supported [Java Development Kit (JDK)](/java/azure/jdk) with version 17.
38
38
*[Apache Maven](https://maven.apache.org/download.cgi) version 3.0 or above.
39
+
* Finish the [Create a Java Spring app with App Configuration](./quickstart-java-spring-app.md) quickstart.
39
40
40
-
## Create a vault
41
+
## Create a key vault
41
42
42
-
1.Select the **Create a resource** option in the upper-left corner of the Azure portal:
43
+
1.Sign in to the [Azure portal](https://portal.azure.com), and then select **Create a resource**.
43
44
44
-

45
-
1. In the search box, enter **Key Vault**.
46
-
1. From the results list, select **Key vaults**.
47
-
1. In **Key vaults**, select **Add**.
48
-
1. On the right in **Create key vault**, provide the following information:
49
-
* Select **Subscription** to choose a subscription.
50
-
* In **Resource Group**, select **Create new** and enter a resource group name.
51
-
* In **Key vault name**, a unique name is required. For this tutorial, enter **Contoso-vault2**.
52
-
* In the **Region** drop-down list, choose a location.
53
-
1. Leave the other **Create key vault** options with their default values.
54
-
1. Select **Create**.
45
+
1. In the search box, enter **Key Vault**. In the result list, select **Key Vault**.
55
46
56
-
At this point, your Azure account is the only one authorized to access this new vault.
47
+
1. On the **Key Vault** page, select **Create**.
48
+
49
+
1. On the **Create a key vault** page, enter the following information:
50
+
- For **Subscription**: Select a subscription.
51
+
- For **Resource group**: Enter the name of an existing resource group or select **Create new** and enter a resource group name.
52
+
- For **Key vault name**: Enter a unique name.
53
+
- For **Region**: Select a location.
54
+
55
+
1. For the other options, use the default values.
57
56
58
-

57
+
1. Select **Review + create**.
58
+
59
+
1. After the system validates and displays your inputs, select **Create**.
60
+
61
+
At this point, your Azure account is the only one authorized to access this new vault.
59
62
60
63
## Add a secret to Key Vault
61
64
62
-
To add a secret to the vault, you need to take just a few more steps. In this case, add a message that you can use to test Key Vault retrieval. The message is called **Message**, and you store the value "Hello from Key Vault" in it.
65
+
Add a secret to the vault to test Key Vault retrieval. The secret is called **Message**, and its value is "Hello from Key Vault."
66
+
67
+
1. On the Key Vault resource menu, select **Objects** > **Secrets**.
63
68
64
-
1. From the Key Vault properties pages, select **Secrets**.
65
69
1. Select **Generate/Import**.
66
-
1. In the **Create a secret** pane, enter the following values:
67
-
***Upload options**: Enter **Manual**.
68
-
***Name**: Enter **Message**.
69
-
***Value**: Enter **Hello from Key Vault**.
70
-
1. Leave the other **Create a secret** properties with their default values.
70
+
71
+
1. In the **Create a secret** dialog, enter the following values:
72
+
- For **Upload options**: Enter **Manual**.
73
+
- For **Name**: Enter **Message**.
74
+
- For **Secret value**: Enter **Hello from Key Vault**.
75
+
76
+
1. For the other options, use the default values.
77
+
71
78
1. Select **Create**.
72
79
73
80
## Add a Key Vault reference to App Configuration
74
81
75
-
1. Sign in to the [Azure portal](https://portal.azure.com). Select **All resources**, and then select the App Configuration store instance that you created in the quickstart.
82
+
1. Sign in to the [Azure portal](https://portal.azure.com). Select **All resources**, and then select your App Configuration store.
76
83
77
84
1. Select **Configuration Explorer**.
78
85
79
86
1. Select **+ Create** > **Key vault reference**, and then specify the following values:
***Subscription**, **Resource group**, and **Key vault**: Enter the values corresponding to the values in the key vault you created in the previous section.
89
+
***Subscription**, **Resource group**, and **Key vault**: Enter the values corresponding to the key vault you created in the previous section.
83
90
***Secret**: Select the secret named **Message** that you created in the previous section.
84
91
85
-
## Connect to Key Vault
86
-
87
-
1. In this tutorial, you use a service principal for authentication to Key Vault. To create this service principal, use the Azure CLI [az ad sp create-for-rbac](/cli/azure/ad/sp#az-ad-sp-create-for-rbac) command:
88
-
89
-
```azurecli
90
-
az ad sp create-for-rbac -n "http://mySP" --role Contributor --scopes /subscriptions/{subscription-id} --sdk-auth
91
-
```
92
+
## Grant your app access to Key Vault
92
93
93
-
This operation returns a series of key/value pairs:
Your application uses `DefaultAzureCredential` to authenticate to both App Configuration and Key Vault. This credential automatically works with managed identities in Azure, and with your developer credentials locally.
108
95
109
-
1. Run the following command to let the service principal access your key vault:
96
+
1.Grant your identity access to Key Vault. Assign the **Key Vault Secrets User** role to your user account or managed identity:
110
97
111
98
```azurecli
112
-
az keyvault set-policy -n <your-unique-keyvault-name> --spn <clientId-of-your-service-principal> --secret-permissions delete get
99
+
az role assignment create --role "Key Vault Secrets User" --scope /subscriptions/<subscriptionId>/resourceGroups/<group-name>/providers/Microsoft.KeyVault/vaults/<your-unique-keyvault-name> --assignee <your-azure-ad-user-or-managed-identity>
113
100
```
114
101
115
-
1. Run the following command to get your object-id, then add it to App Configuration.
102
+
1. Grant your identity access to App Configuration. Assign the **App Configuration Data Reader** role:
116
103
117
104
```azurecli
118
-
az ad sp show --id <clientId-of-your-service-principal>
119
-
az role assignment create --role "App Configuration Data Reader" --scope /subscriptions/<subscriptionId>/resourceGroups/<group-name> --assignee-principal-type --assignee-object-id <objectId-of-your-service-principal> --resource-group <your-resource-group>
120
-
```
121
-
122
-
1. Create the environment variables **AZURE_CLIENT_ID**, **AZURE_CLIENT_SECRET**, and **AZURE_TENANT_ID**. Use the values for the service principal that were displayed in the previous steps. At the command line, run the following commands and restart the command prompt to allow the change to take effect:
123
-
124
-
```cmd
125
-
setx AZURE_CLIENT_ID "clientId"
126
-
setx AZURE_CLIENT_SECRET "clientSecret"
127
-
setx AZURE_TENANT_ID "tenantId"
128
-
```
129
-
130
-
If you use Windows PowerShell, run the following command:
131
-
132
-
```azurepowershell
133
-
$Env:AZURE_CLIENT_ID = "clientId"
134
-
$Env:AZURE_CLIENT_SECRET = "clientSecret"
135
-
$Env:AZURE_TENANT_ID = "tenantId"
105
+
az role assignment create --role "App Configuration Data Reader" --scope /subscriptions/<subscriptionId>/resourceGroups/<group-name>/providers/Microsoft.AppConfiguration/configurationStores/<your-app-configuration-store> --assignee <your-azure-ad-user-or-managed-identity>
136
106
```
137
107
138
-
If you use macOS or Linux, run the following command:
139
-
140
-
```cmd
141
-
export AZURE_CLIENT_ID ='clientId'
142
-
export AZURE_CLIENT_SECRET ='clientSecret'
143
-
export AZURE_TENANT_ID ='tenantId'
144
-
```
145
-
146
-
> [!NOTE]
147
-
> These Key Vault credentials are only used within your application. Your application authenticates directly with Key Vault using these credentials without involving the App Configuration service. The Key Vault provides authentication for both your application and your App Configuration service without sharing or exposing keys.
148
-
149
108
## Update your code to use a Key Vault reference
150
109
151
110
1. Create an environment variable called **APP_CONFIGURATION_ENDPOINT**. Set its value to the endpoint of your App Configuration store. You can find the endpoint on the **Access Keys** blade in the Azure portal. Restart the command prompt to allow the change to take effect.
0 commit comments