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
Copy file name to clipboardExpand all lines: articles/service-connector/tutorial-django-webapp-postgres-cli.md
+33-29Lines changed: 33 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,36 +12,38 @@ ms.custom:
12
12
- devx-track-python
13
13
- linux-related-content
14
14
- sfi-ropc-nochange
15
+
#customer intent: As a Django app developer and PostgreSQL user, I want to learn how to use Service Connector to connect Azure Postgres backing databases and other services to my App Service apps, so I can easily store and serve app data to my users.
15
16
---
17
+
16
18
# Tutorial: Connect a Django web app to Azure PostgreSQL using Service Connector
17
19
18
-
In this tutorial, you learn how to deploy a data-driven Python [Django](https://www.djangoproject.com/) web app to [Azure App Service](/azure/app-service/overview) and use Service Connector to connect it to an [Azure Database for PostgreSQL](/azure/postgresql/flexible-server/) database and an [Azure Storage] account.
20
+
In this tutorial, you learn how to deploy a data-driven Python Django web app to Azure App Service and use Service Connector to connect it to other Azure services. The sample web app stores restaurant and review information in an Azure Database for PostgreSQL database and stores photos in an Azure Storage container.
19
21
20
-
The tutorial uses Azure CLI to complete the following tasks:
22
+
You use Azure CLI to complete the following tasks:
21
23
22
24
> [!div class="checklist"]
23
-
> * Create a Python [Django](https://www.djangoproject.com/) web app and deploy it to Azure App Service.
24
-
> * Create an Azure Database for PostgreSQL database.
25
-
> * Create an Azure Storage account and container.
26
-
> *Use Service Connector to connect the Azure web app to the PostgreSQL database and Azure Storage container.
27
-
> *Use the Azure portal to interact with the web app.
25
+
> * Create a Python [Django](https://www.djangoproject.com/) web app and deploy it to [Azure App Service](/azure/app-service/overview).
26
+
> * Create an [Azure Database for PostgreSQL](/azure/postgresql/flexible-server/) flexible server and database.
27
+
> * Create an [Azure Storage](/azure/storage/common/storage-introduction) account and container.
28
+
> *Connect the web app to the database and storage container using Service Connector with [managed identity](/azure/active-directory/managed-identities-azure-resources/overview) authentication.
29
+
> *Interact with the web app.
28
30
29
31
> [!NOTE]
30
-
> This tutorial is similar to the App Service [Deploy a Python Django web app with PostgreSQL in Azure](/azure/app-service/tutorial-python-postgresql-app-django) tutorial, but uses a system-assigned passwordless [managed identity](/azure/active-directory/managed-identities-azure-resources/overview) with Azure role-based access control to access [Azure Storage](/azure/storage/common/storage-introduction) and [Azure Database for PostgreSQL](/azure/postgresql/flexible-server)resources. The [Create a passwordless connector to Postgres database](#create-a-passwordless-connector-to-postgres-database) section of this article shows how Service Connector simplifies the connection process.
32
+
> This tutorial is similar to the App Service [Deploy a Python Django web app with PostgreSQL in Azure](/azure/app-service/tutorial-python-postgresql-app-django) tutorial, but uses a system-assigned passwordless managed identity with Azure role-based access control to access other Azure resources. The [Create a passwordless service connection](#create-a-passwordless-service-connection) section of this article shows how Service Connector simplifies the connection process.
31
33
>
32
-
> The web app uses the [DefaultAzureCredential](/azure/developer/intro/passwordless-overview#introducing-defaultazurecredential) class of the Python [Azure Identity client library](/python/api/overview/azure/identity-readme) to automatically detect when a managed identity exists and use it to access other Azure resources.
34
+
> The web app uses the [DefaultAzureCredential](/azure/developer/intro/passwordless-overview#introducing-defaultazurecredential) class of the Python [Azure Identity client library](/python/api/overview/azure/identity-readme) to automatically detect when a managed identity exists and uses it to access the other resources.
33
35
34
36
## Prerequisites
35
37
36
-
- An Azure subscription with write and role-assignment permissions for the tutorial resources, in a region that supports Service Connector and has sufficient App Service support and quota.
38
+
- An Azure subscription with write and role-assignment permissions for the tutorial resources, in an Azure region that [supports Service Connector](concept-region-support.md) and has sufficient [App Service support and quota](/azure/azure-resource-manager/management/azure-subscription-service-limits#azure-app-service-limits).
37
39
38
40
-[Azure Cloud Shell](/azure/cloud-shell/overview) to run the tutorial steps, or if you prefer to run locally:
39
41
1. Install [Azure CLI](/cli/azure/install-azure-cli) 2.30.0 or higher. To check your version, run `az --version`. To upgrade, run `az upgrade`.
40
42
1. Sign in to Azure by using `az login` and following the prompts.
41
43
42
44
## Set up your environment
43
45
44
-
1. Make sure your subscription is registered to use the `Microsoft.ServiceLinker` and 'Microsoft.DBforPostgreSQL' resource providers. If not, run `az provider register -n Microsoft.[name of service]` to register the providers.
46
+
1. Make sure your subscription is registered to use the `Microsoft.ServiceLinker` and `Microsoft.DBforPostgreSQL` resource providers. If not, run `az provider register -n Microsoft.[name of service]` to register the providers.
45
47
46
48
1. Install the following needed Azure CLI extensions:
47
49
@@ -52,12 +54,6 @@ The tutorial uses Azure CLI to complete the following tasks:
52
54
53
55
### Clone the sample app
54
56
55
-
In the sample app, the web app production settings are in the *azuresite/production.py* file, and development settings are in *azuresite/settings.py*. The production settings configure Django to run in any production environment and aren't specific to App Service.
56
-
57
-
The app uses production settings when the `WEBSITE_HOSTNAME` environment variable is set. App Service automatically sets this variable to the URL of the web app, such as `msdocs-django.azurewebsites.net`.
58
-
59
-
For more information, see the [Django deployment checklist](https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/). Also see [Production settings for Django on Azure](/azure/app-service/configure-language-python.md#production-settings-for-django-apps).
60
-
61
57
1. Clone the sample app repository.
62
58
63
59
```bash
@@ -68,12 +64,18 @@ For more information, see the [Django deployment checklist](https://docs.djangop
68
64
69
65
1. Change directories into the repo folder using `cd serviceconnector-webapp-postgresql-django-passwordless` and run all remaining commands from that folder.
70
66
67
+
In the sample app, the web app production settings are in the *azuresite/production.py* file. Development settings are in *azuresite/settings.py*. The production settings configure Django to run in any production environment and aren't specific to App Service.
68
+
69
+
The app uses production settings when the `WEBSITE_HOSTNAME` environment variable is set. For Azure Postgres connection strings, App Service automatically sets this variable to the URL of the web app, such as `https://msdocs-django.azurewebsites.net`.
70
+
71
+
For more information, see the [Django deployment checklist](https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/). Also see [Production settings for Django on Azure](/azure/app-service/configure-language-python#production-settings-for-django-apps).
72
+
71
73
### Define initial environment variables
72
74
73
75
The following code defines the necessary environment variables for this tutorial.
74
76
75
77
-`LOCATION` must be an Azure region where your subscription has sufficient quota to create the resources and doesn't restrict Azure Database for PostgreSQL for your subscription.
76
-
- The `ADMIN_PW` must contain 8 to 128 characters in at least three of the four categories uppercase letters, lowercase letters, numerals, and nonalphanumeric characters. Do not use`$`.
78
+
- The `ADMIN_PW` must contain 8 to 128 characters in at least three of the four categories uppercase letters, lowercase letters, numerals, and nonalphanumeric characters, excluding`$`.
77
79
78
80
1. Set up the following environment variables, replacing the `<region>` and `<database password>` placeholders with valid values.
79
81
@@ -95,7 +97,7 @@ The following code defines the necessary environment variables for this tutorial
95
97
96
98
## Deploy the app code to App Service
97
99
98
-
Create the app host in App Service and deploy the sample app code to that host. The `az webapp up`command performs the following actions, which may take a few minutes:
100
+
Create the app host in App Service and deploy the sample app code to that host. The `az webapp up`command performs the following actions, which might take a few minutes:
99
101
100
102
* Creates an [App Service plan](../app-service/overview-hosting-plans.md) in the Basic pricing tier (B1).
101
103
* Creates the App Service app.
@@ -115,7 +117,7 @@ In the code, the `sku` defines the CPU, memory, and cost of the App Service plan
115
117
--sku B1
116
118
```
117
119
118
-
1. Configure App Service to use the repository *start.sh* file by running the [az webapp config set](/cli/azure/webapp/config#az-webapp-config-set) command.
120
+
1. After the deployment completes, configure App Service to use the repository *start.sh* file by running the [az webapp config set](/cli/azure/webapp/config#az-webapp-config-set) command.
119
121
120
122
```azurecli
121
123
az webapp config set \
@@ -173,11 +175,11 @@ Create the Azure Database for PostgreSQL database to store the app information.
173
175
--querytext 'create database restaurant;'
174
176
```
175
177
176
-
## Create a passwordless Service Connector connection
178
+
## Create a passwordless service connection
177
179
178
-
Use [az webapp connection create postgres-flexible](/cli/azure/webapp/connection/create#az-webapp-connection-create-postgres-flexible) to add a service connector that connects the Azure web app to the Postgres database using passwordless managed identity for authentication. The following commands create an environment variable named `AZURE_POSTGRESQL_CONNECTIONSTRING` that provides the database connection information for the app. The commands also configure Azure Database for PostgreSQL to use managed identity and Azure role-based access control.
180
+
Use [az webapp connection create postgres-flexible](/cli/azure/webapp/connection/create#az-webapp-connection-create-postgres-flexible) to add a service connector that connects the Azure web app to the Postgres database using passwordless managed identity authentication. The following command configures Azure Database for PostgreSQL to use managed identity and Azure role-based access control. The command output lists the actions Service Connector takes.
179
181
180
-
The Python appcode accesses app environment variables with statements like `os.environ.get('AZURE_POSTGRESQL_HOST')`. For more information, see [Access environment variables](/azure/app-service/configure-language-python#access-environment-variables). The command output lists the actions Service Connector takes.
182
+
The command creates creates an environment variable named `AZURE_POSTGRESQL_CONNECTIONSTRING` that provides the database connection information for the app. The app code accesses app environment variables with statements like `os.environ.get('AZURE_POSTGRESQL_HOST')`. For more information, see [Access environment variables](/azure/app-service/configure-language-python#access-environment-variables).
181
183
182
184
```azurecli
183
185
az webapp connection create postgres-flexible \
@@ -192,13 +194,15 @@ az webapp connection create postgres-flexible \
192
194
193
195
## Create and connect to a storage account
194
196
195
-
Use [az webapp connection create storage-blob](/cli/azure/webapp/connection/create#az-webapp-connection-create-storage-blob) to create an Azure storage account and a service connector. The code takes the following actions:
197
+
Use [az webapp connection create storage-blob](/cli/azure/webapp/connection/create#az-webapp-connection-create-storage-blob) to create an Azure storage account and a service connector. The command takes the following actions:
196
198
197
199
* Enables system-assigned managed identity on the web app.
198
200
* Adds the web app with role **Storage Blob Data Contributor** to the new storage account.
199
201
* Configures the storage account network to accept access from the web app.
200
202
* Creates an environment variable named `AZURE_STORAGEBLOB_RESOURCEENDPOINT` for the Azure Storage account.
201
203
204
+
1. Run the following command to create the storage account and connection:
@@ -237,17 +241,17 @@ Use [az webapp connection create storage-blob](/cli/azure/webapp/connection/crea
237
241
238
242
## Test the Python web app in Azure
239
243
240
-
The app uses the [azure.identity](https://pypi.org/project/azure-identity/) package and its `DefaultAzureCredential` class. When the app is running in Azure, The `DefaultAzureCredential` automatically detects when a managed identity exists forthe App Service, and uses it to access the Azure Storage and Azure Database for PostgreSQL resources. You don't need to provide storage keys, certificates, or credentials to App Service to access these resources. The app stores restaurant and review informationin the Postgres database and stores the photos in the Azure Storage container.
244
+
The app uses the [azure.identity](https://pypi.org/project/azure-identity/) package and its `DefaultAzureCredential` class. When the app is running in Azure, The `DefaultAzureCredential` automatically detects when a managed identity exists for the App Service, and uses it to access the Azure Storage and Azure Database for PostgreSQL resources. You don't need to provide storage keys, certificates, or credentials to App Service to access these resources.
241
245
242
-
1. Browse to the deployed application at the URL `http://$APP_SERVICE_NAME.azurewebsites.net`, or by selecting the **Default domain** link on the Azure portal app page. It can take a minute or two for the app to start. If you see a default app page that isn't the default sample app page, wait a minute and refresh the browser.
246
+
1. Browse to the deployed application at the URL `https://$APP_SERVICE_NAME.azurewebsites.net`, or by selecting the **Default domain** link on the Azure portal app page. It can take a minute or two for the app to start. If you see a default app page that isn't the sample app, wait a minute and refresh the browser.
243
247
244
248
1. Test the functionality of the sample app by adding a restaurant and some reviews with photos for the restaurant. The app should resemble the following screenshot:
245
249
246
250
:::image type="content" source="media/tutorial-django-webapp-postgres-cli/example-of-review-sample-app-production-deployed-small.png" lightbox="media/tutorial-django-webapp-postgres-cli/example-of-review-sample-app-production-deployed.png" alt-text="Screenshot of the sample app showing restaurant review functionality using App Service, Azure Database for PostgreSQL, and Azure Storage." :::
247
251
248
252
## Clean up resources
249
253
250
-
If you don't want to keep the app or resources, you can avoid ongoing charges by deleting the resource group that contains them. Be sure you no longer need the resources before using the command.
254
+
If you don't want to keep the app or resources you created for this tutorial, you can delete the resource group that contains the resources to avoid ongoing charges. Be sure you no longer need the resources before using the command.
251
255
252
256
```azurecli
253
257
az group delete --name $RESOURCE_GROUP_NAME --no-wait
@@ -259,8 +263,8 @@ Deleting all the resources can take some time. The `--no-wait` argument allows t
259
263
260
264
If you have issues with this tutorial, see the following resources:
261
265
262
-
- [Troubleshoot Linux Python apps for Azure App Service](/azure/app-service/configure-language-python#troubleshooting),
263
-
- [Fill out a contact form](https://aka.ms/DjangoCLITutorialHelp).
266
+
- [Troubleshoot Linux Python apps for Azure App Service](/azure/app-service/configure-language-python#troubleshooting)
0 commit comments