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
# Deploy a Python FastAPI web app with PostgreSQL in Azure
14
14
15
-
In this tutorial, you'll deploy a data-driven Python web app (**[FastAPI](https://fastapi.tiangolo.com/)** ) to **[Azure App Service](./overview.md#app-service-on-linux)** with the **[Azure Database for PostgreSQL](../postgresql/index.yml)** relational database service. Azure App Service supports [Python](https://www.python.org/downloads/) in a Linux server environment.
15
+
In this tutorial, you deploy a data-driven Python web app (**[FastAPI](https://fastapi.tiangolo.com/)** ) to **[Azure App Service](./overview.md#app-service-on-linux)** with the **[Azure Database for PostgreSQL](../postgresql/index.yml)** relational database service. Azure App Service supports [Python](https://www.python.org/downloads/) in a Linux server environment.
16
16
17
17
:::image type="content" border="False" source="./media/tutorial-python-postgresql-app-fastapi/python-postgresql-app-architecture-240px.png" lightbox="./media/tutorial-python-postgresql-app-fastapi/python-postgresql-app-architecture.png" alt-text="An architecture diagram showing an App Service with a PostgreSQL database in Azure.":::
In this step, you create the Azure resources. The steps used in this tutorial create a set of secure-by-default resources that include App Service and Azure Database for PostgreSQL. For the creation process, you'll specify:
95
+
In this step, you create the Azure resources. The steps used in this tutorial create a set of secure-by-default resources that include App Service and Azure Database for PostgreSQL. For the creation process, you specify:
96
96
97
97
* The **Name** for the web app. It's the name used as part of the DNS name for your webapp in the form of `https://<app-name>.azurewebsites.net`.
98
98
* The **Region** to run the app physically in the world.
@@ -170,7 +170,7 @@ The creation wizard generated the connectivity variables for you already as [app
170
170
:::row-end:::
171
171
:::row:::
172
172
:::column span="2":::
173
-
**Step 2:** In the **App settings** tab of the **Environment variables** page, verify that `AZURE_POSTGRESQL_CONNECTIONSTRING` is present. That will be injected into the runtime environment as an environment variable.
173
+
**Step 2:** In the **App settings** tab of the **Environment variables** page, verify that `AZURE_POSTGRESQL_CONNECTIONSTRING` is present. The connection string will be injected into the runtime environment as an environment variable.
174
174
:::column-end:::
175
175
:::column:::
176
176
:::image type="content" source="./media/tutorial-python-postgresql-app-fastapi/azure-portal-get-connection-string-fastapi-2.png" alt-text="A screenshot showing how to see the autogenerated connection string (FastAPI)." lightbox="./media/tutorial-python-postgresql-app-fastapi/azure-portal-get-connection-string-fastapi-2.png":::
@@ -179,7 +179,7 @@ The creation wizard generated the connectivity variables for you already as [app
179
179
180
180
## 3. Deploy sample code
181
181
182
-
In this step, you'll configure GitHub deployment using GitHub Actions. It's just one of many ways to deploy to App Service, but also a great way to have continuous integration in your deployment process. By default, every `git push` to your GitHub repository will kick off the build and deploy action.
182
+
In this step, you configure GitHub deployment using GitHub Actions. It's just one of many ways to deploy to App Service, but also a great way to have continuous integration in your deployment process. By default, every `git push` to your GitHub repository will kick off the build and deploy action.
183
183
184
184
:::row:::
185
185
:::column span="2":::
@@ -303,7 +303,7 @@ With the PostgreSQL database protected by the virtual network, the easiest way t
303
303
304
304
## 6. Stream diagnostic logs
305
305
306
-
The sample app uses the Python Standard Library logging module to help you diagnose issues with your application. The sample app includes calls to the logger as shown below.
306
+
The sample app uses the Python Standard Library logging module to help you diagnose issues with your application. The sample app includes calls to the logger as shown in the following code.
@@ -392,13 +392,13 @@ In this step, you create the Azure resources and deploy a sample app to App Serv
392
392
|What would you like to do with these files?|**Keep my existing files unchanged**|
393
393
|Enter a new environment name | Type a unique name. The azd template uses this name as part of the DNS name of your web app in Azure (`<app-name>.azurewebsites.net`). Alphanumeric characters and hyphens are allowed. |
394
394
395
-
1. Run the `azd up`command to provision the necessary Azure resources and deploy the app code. If you are not already signed-in to Azure, the browser will launch and ask you to sign-in. The `azd up`command will also prompt you to selectthe desired subscription and location to deploy to.
395
+
1. Run the `azd up`command to provision the necessary Azure resources and deploy the app code. If you aren't already signed-in to Azure, the browser will launch and ask you to sign-in. The `azd up` command will also prompt you to select the desired subscription and location to deploy to.
396
396
397
397
```bash
398
398
azd up
399
399
```
400
400
401
-
The `azd up`command can take a several minutes to complete. It also compiles and deploys your application code. While it's running, the command provides messages about the provisioning and deployment process, including a link to the deployment in Azure. When it finishes, the command also displays a link to the deploy application.
401
+
The `azd up` command can take several minutes (up to 45) to complete. It also compiles and deploys your application code. While it's running, the command provides messages about the provisioning and deployment process, including a link to the deployment in Azure. When it finishes, the command also displays a link to the deploy application.
402
402
403
403
This azd template contains files (*azure.yaml* and the *infra* directory) that generate a secure-by-default architecture with the following Azure resources:
404
404
@@ -410,7 +410,36 @@ In this step, you create the Azure resources and deploy a sample app to App Serv
410
410
- **Private DNS zone**→ Enables DNS resolution of the PostgreSQL server in the virtual network.
411
411
- **Log Analytics workspace**→ Acts as the target container for your app to ship its logs, where you can also query the logs.
412
412
413
-
Note down the values for the **Subscription ID** (Guid), the **App Service**, and the **Resource Group** in the output. You'll use them in the following sections.
413
+
1. When the `azd up`command completes, note down the values forthe **Subscription ID** (Guid), the **App Service**, and the **Resource Group**in the output. You use them in the following sections. Your output will look similar to the following (partial) output:
414
+
415
+
```output
416
+
Subscription: Your subscription name (1111111-1111-1111-1111-111111111111)
417
+
Location: East US
418
+
419
+
You can view detailed progress in the Azure Portal:
1. The startup command runs the file *src/entrypoint.sh*. Examine the code in that file to understand the commands that App Service runs to start your app:
464
+
1. The startup command runs the file *src/entrypoint.sh*. Examine the code in that file to understand the commands that App Service runs to start your app:
To learn more about app configuration and startup in App Service, see [Configure a Linux Python app for Azure App Service](configure-language-python.md).
440
469
441
470
## 4. Generate database schema
442
471
443
-
You may have noticed in the previous section that *entrypoint.sh* contains the following line: `python3 src/fastapi_app/seed_data.py`. This command migrates your database. In the sample app, it only ensures that the correct tables are created in your database. It doesn't populate these tables with any data.
472
+
You might have noticed in the previous section that *entrypoint.sh* contains the following line: `python3 src/fastapi_app/seed_data.py`. This command migrates your database. In the sample app, it only ensures that the correct tables are created in your database. It doesn't populate these tables with any data.
444
473
445
474
In this section, you'll run this command manually fordemonstration purposes. With the PostgreSQL database protected by the virtual network, the easiest way to run the command isin an SSH session with the App Service container.
0 commit comments