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 (Django or Flask) web app with PostgreSQL in Azure
@@ -18,6 +19,8 @@ In this tutorial, you'll deploy a data-driven Python web app (**[Django](https:/
18
19
* An Azure account with an active subscription. If you don't have an Azure account, you [can create one for free](https://azure.microsoft.com/free/python).
19
20
* Knowledge of Python with Flask development or [Python with Django development](/training/paths/django-create-data-driven-websites/)
20
21
22
+
:::zone pivot="deploy-portal"
23
+
21
24
## Sample application
22
25
23
26
Sample Python applications using the Flask and Django framework are provided to help you follow along with this tutorial. To deploy them without running them locally, skip this part.
@@ -474,14 +477,262 @@ When you're finished, you can delete all of the resources from your Azure subscr
474
477
:::column-end:::
475
478
:::row-end:::
476
479
480
+
## Troubleshooting
481
+
482
+
Listed below are issues you may encounter while trying to work through this tutorial and steps to resolve them.
483
+
484
+
#### I can't connect to the SSH session
485
+
486
+
If you can't connect to the SSH session, then the app itself has failed to start. Check the [diagnostic logs](#6-stream-diagnostic-logs) for details. For example, if you see an error like `KeyError: 'AZURE_POSTGRESQL_CONNECTIONSTRING'`, it may mean that the environment variable is missing (you may have removed the app setting).
487
+
488
+
#### I get an error when running database migrations
489
+
490
+
If you encounter any errors related to connecting to the database, check if the app settings (`AZURE_POSTGRESQL_CONNECTIONSTRING`) have been changed. Without that connection string, the migrate command can't communicate with the database.
491
+
492
+
:::zone-end
493
+
494
+
:::zone pivot="deploy-azd"
495
+
496
+
## Provision and deploy using the Azure Developer CLI
497
+
498
+
Sample Python application templates using the Flask and Django framework are provided for this tutorial. The [Azure Developer CLI](/azure/developer/azure-developer-cli/overview) greatly streamlines the process of provisioning application resources and deploying code on Azure. For a more step-by-step approach using the Azure portal and other tools, toggle to the **Azure portal** approach at the top of the page.
499
+
500
+
The Azure Developer CLI (azd) provides end-to-end support for project initialization, provisioning, deploying, monitoring and scaffolding a CI/CD pipeline to run against real Azure resources. You can use `azd` to provision and deploy the resources for the sample application in an automated and streamlined way.
501
+
502
+
Follow the steps below to setup the Azure Developer CLI and provision and deploy the sample application:
503
+
504
+
1. Install the Azure Developer CLI. For a full list of supported installation options and tools, visit the [installation guide](/azure/developer/azure-developer-cli/install-azd).
1. Run the `azd up` command to clone, provision and deploy the app resources. Provide the name of the template you wish to use for the `--template` parameter. The `azd up` command will also prompt you to login to Azure and provide a name and location for the app.
521
+
522
+
### [Flask](#tab/flask)
523
+
524
+
```bash
525
+
azd up --template msdocs-flask-postgresql-sample-app
526
+
```
527
+
528
+
### [Django](#tab/django)
529
+
530
+
```bash
531
+
azd up --template msdocs-django-postgresql-sample-app
532
+
```
533
+
534
+
1. When the `azd up` command finishes running, the URL for your deployed web app in the console will be printed. Click, or copy and paste the web app URL into your browser to explore the running app and verify that it is working correctly. All of the Azure resources and application code were set up for you by the `azd up` command.
535
+
536
+
The name of the resource group that was created is also displayed in the console output. Locate the resource group in the Azure portal to see all of the provisioned resources.
537
+
538
+
:::image type="content" border="False" source="./media/tutorial-python-postgresql-app/azd-resources-small.png" lightbox="./media/tutorial-python-postgresql-app/azd-resources.png" alt-text="A screenshot showing the resources deployed by the Azure Developer CLI.":::
539
+
540
+
The Azure Developer CLI also enables you to configure your application to use a CI/CD pipeline for deployments, setup monitoring functionality, and even remove the provisioned resources if you want to tear everything down. For more information about these additional workflows, visit the project [README](https://github.com/Azure-Samples/msdocs-flask-postgresql-sample-app/blob/main/README.md).
541
+
542
+
## Explore the completed azd project template workflow
543
+
544
+
The sections ahead review the steps that `azd` handled for you in more depth. You can explore this workflow to better understand the requirements for deploying your own apps to Azure. When you ran `azd up`, the Azure Developer CLI completed the following steps:
545
+
546
+
> [!NOTE]
547
+
> You can also use the steps outlined in the **Azure portal** version of this flow to gain additional insights into the tasks that `azd` completed for you.
548
+
549
+
### 1. Cloned and initialized the project
550
+
551
+
The `azd up` command cloned the sample app project template to your machine. The project template includes the following components:
552
+
553
+
* **Source code**: The code and assets for a Flask or Django web app that can be used for local development or deployed to Azure.
554
+
* **Bicep files**: Infrastructure as code (IaC) files that are used by `azd` to create the necessary resources in Azure.
555
+
* **Configuration files**: Essential configuration files such as `azure.yaml` that are used by `azd` to provision, deploy and wire resources together to produce a fully-fledged application.
556
+
557
+
### 2. Provisioned the Azure resources
558
+
559
+
The `azd up` command created all of the resources for the sample application in Azure using the Bicep files in the [`infra`](https://github.com/Azure-Samples/msdocs-flask-postgresql-sample-app/tree/main/infra) folder of the project template. [Bicep](/azure/azure-resource-manager/bicep/overview?tabs=bicep) is a declarative language used to manage Infrastructure as Code in Azure. Some of the key resources and configurations created by the template include:
560
+
561
+
* **Resource group**: A resource group was created to hold all of the other provisioned Azure resources. The resource group keeps your resources well organized and easier to manage. The name of the resource group is based off of the environment name you specified during the `azd up` initialization process.
562
+
* **Azure Virtual Network**: A virtual network was created to enable the provisioned resources to securely connect and communicate with one another. Related configurations such as setting up a private DNS zone link were also applied.
563
+
* **Azure App Service plan**: An App Service plan was created to host App Service instances. App Service plans define what compute resources are available for one or more web apps.
564
+
* **Azure App Service**: An App Service instance was created in the new App Service plan to host and run the deployed application. In this case a Linux instance was created and configured to run Python apps. Additional configurations were also applied to the app service, such as setting the Postgres connection string and secret keys.
565
+
* **Azure Database for PostgresSQL**: A Postgres database and server were created for the app hosted on App Service to connect to. The required admin user, network and connection settings were also configured.
566
+
* **Azure Application Insights**: Application insights was setup and configured for the app hosted on the App Service. This service enables detailed telemetry and monitoring for your application.
567
+
568
+
You can inspect the Bicep files in the [`infra`](https://github.com/Azure-Samples/msdocs-flask-postgresql-sample-app/tree/main/infra) folder of the project to understand how each of these resources were provisioned in more detail. The `resources.bicep` file defines most of the different services created in Azure. For example, the App Service plan and App Service web app instance were created and connected using the following Bicep code:
The `azd up` command also deployed the sample application code to the provisioned Azure resources. The Developer CLI understands how to deploy different parts of your application code to different services in Azure using the `azure.yaml` file at the root of the project. The `azure.yaml` file specifies the app source code location, the type of app, and the Azure Service that should host that app.
689
+
690
+
Consider the following `azure.yaml` file. These configurations tell the Azure Developer CLI that the Python code that lives at the root of the project should be deployed to the created App Service.
Once you are finished experimenting with your sample application, you can run the `azd down` command to remove the app from Azure. Removing resources helps to avoid unintended costs or unused services in your Azure subscription.
723
+
724
+
```bash
725
+
azd down
726
+
```
727
+
728
+
:::zone-end
729
+
477
730
## Frequently asked questions
478
731
479
732
- [How much does this setup cost?](#how-much-does-this-setup-cost)
480
733
- [How do I connect to the PostgreSQL server that's secured behind the virtual network with other tools?](#how-do-i-connect-to-the-postgresql-server-thats-secured-behind-the-virtual-network-with-other-tools)
481
734
- [How does local app development work with GitHub Actions?](#how-does-local-app-development-work-with-github-actions)
482
735
- [How is the Django sample configured to run on Azure App Service?](#how-is-the-django-sample-configured-to-run-on-azure-app-service)
483
-
-[I can't connect to the SSH session](#i-cant-connect-to-the-ssh-session)
484
-
-[I get an error when running database migrations](#i-get-an-error-when-running-database-migrations)
485
736
486
737
#### How much does this setup cost?
487
738
@@ -529,14 +780,6 @@ The [Django sample application](https://github.com/Azure-Samples/msdocs-django-p
529
780
530
781
For more information, see [Production settings for Django apps](configure-language-python.md#production-settings-for-django-apps).
531
782
532
-
#### I can't connect to the SSH session
533
-
534
-
If you can't connect to the SSH session, then the app itself has failed to start. Check the [diagnostic logs](#6-stream-diagnostic-logs) for details. For example, if you see an error like `KeyError: 'AZURE_POSTGRESQL_CONNECTIONSTRING'`, it may mean that the environment variable is missing (you may have removed the app setting).
535
-
536
-
#### I get an error when running database migrations
537
-
538
-
If you encounter any errors related to connecting to the database, check if the app settings (`AZURE_POSTGRESQL_CONNECTIONSTRING`) have been changed. Without that connection string, the migrate command can't communicate with the database.
539
-
540
783
## Next steps
541
784
542
785
Advance to the next tutorial to learn how to secure your app with a custom domain and certificate.
0 commit comments