Skip to content

Commit 71a26e4

Browse files
Merge pull request #230299 from alexwolfmsft/azd-poc-alt
azd integration zone pivots
2 parents 27f1d86 + b8a9071 commit 71a26e4

File tree

6 files changed

+263
-12
lines changed

6 files changed

+263
-12
lines changed
58.1 KB
Loading
95.4 KB
Loading
11.1 KB
Loading
25.7 KB
Loading

articles/app-service/tutorial-python-postgresql-app.md

Lines changed: 254 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ description: Create a Python Django or Flask web app with a PostgreSQL database
44
ms.devlang: python
55
ms.topic: tutorial
66
ms.date: 02/28/2023
7-
ms.custom: mvc, seodec18, seo-python-october2019, cli-validate, devx-track-python, devdivchpfy22, event-tier1-build-2022, vscode-azure-extension-update-completed
7+
ms.custom: mvc, seodec18, seo-python-october2019, cli-validate, devx-track-python, devx-track-azurecli, devdivchpfy22, event-tier1-build-2022, vscode-azure-extension-update-completed
8+
zone_pivot_groups: deploy-python-web-app-postgressql
89
---
910

1011
# 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:/
1819
* 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).
1920
* Knowledge of Python with Flask development or [Python with Django development](/training/paths/django-create-data-driven-websites/)
2021

22+
:::zone pivot="deploy-portal"
23+
2124
## Sample application
2225

2326
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
474477
:::column-end:::
475478
:::row-end:::
476479

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).
505+
506+
### [Windows](#tab/windows)
507+
508+
```azdeveloper
509+
powershell -ex AllSigned -c "Invoke-RestMethod 'https://aka.ms/install-azd.ps1' | Invoke-Expression"
510+
```
511+
512+
### [Linux/MacOS](#tab/linuxmac)
513+
514+
```azdeveloper
515+
curl -fsSL https://aka.ms/install-azd.sh | bash
516+
```
517+
518+
---
519+
520+
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:
569+
570+
### [Flask](#tab/flask)
571+
572+
```yaml
573+
resource appServicePlan 'Microsoft.Web/serverfarms@2021-03-01' = {
574+
name: '${prefix}-service-plan'
575+
location: location
576+
tags: tags
577+
sku: {
578+
name: 'B1'
579+
}
580+
properties: {
581+
reserved: true
582+
}
583+
}
584+
585+
resource web 'Microsoft.Web/sites@2022-03-01' = {
586+
name: '${prefix}-app-service'
587+
location: location
588+
tags: union(tags, { 'azd-service-name': 'web' })
589+
kind: 'app,linux'
590+
properties: {
591+
serverFarmId: appServicePlan.id
592+
siteConfig: {
593+
alwaysOn: true
594+
linuxFxVersion: 'PYTHON|3.10'
595+
ftpsState: 'Disabled'
596+
appCommandLine: 'startup.sh'
597+
}
598+
httpsOnly: true
599+
}
600+
identity: {
601+
type: 'SystemAssigned'
602+
}
603+
```
604+
605+
### [Django](#tab/django)
606+
607+
```yml
608+
resource appServicePlan 'Microsoft.Web/serverfarms@2021-03-01' = {
609+
name: '${prefix}-service-plan'
610+
location: location
611+
tags: tags
612+
sku: {
613+
name: 'B1'
614+
}
615+
properties: {
616+
reserved: true
617+
}
618+
}
619+
620+
resource web 'Microsoft.Web/sites@2022-03-01' = {
621+
name: '${prefix}-app-service'
622+
location: location
623+
tags: union(tags, { 'azd-service-name': 'web' })
624+
kind: 'app,linux'
625+
properties: {
626+
serverFarmId: appServicePlan.id
627+
siteConfig: {
628+
alwaysOn: true
629+
linuxFxVersion: 'PYTHON|3.10'
630+
ftpsState: 'Disabled'
631+
appCommandLine: 'startup.sh'
632+
}
633+
httpsOnly: true
634+
}
635+
identity: {
636+
type: 'SystemAssigned'
637+
}
638+
639+
```
640+
641+
---
642+
643+
The Azure Database for PostgreSQL was also created using the following Bicep:
644+
645+
```yml
646+
resource postgresServer 'Microsoft.DBforPostgreSQL/flexibleServers@2022-01-20-preview' = {
647+
location: location
648+
tags: tags
649+
name: pgServerName
650+
sku: {
651+
name: 'Standard_B1ms'
652+
tier: 'Burstable'
653+
}
654+
properties: {
655+
version: '12'
656+
administratorLogin: 'postgresadmin'
657+
administratorLoginPassword: databasePassword
658+
storage: {
659+
storageSizeGB: 128
660+
}
661+
backup: {
662+
backupRetentionDays: 7
663+
geoRedundantBackup: 'Disabled'
664+
}
665+
network: {
666+
delegatedSubnetResourceId: virtualNetwork::databaseSubnet.id
667+
privateDnsZoneArmResourceId: privateDnsZone.id
668+
}
669+
highAvailability: {
670+
mode: 'Disabled'
671+
}
672+
maintenanceWindow: {
673+
customWindow: 'Disabled'
674+
dayOfWeek: 0
675+
startHour: 0
676+
startMinute: 0
677+
}
678+
}
679+
680+
dependsOn: [
681+
privateDnsZoneLink
682+
]
683+
}
684+
```
685+
686+
### 3. Deployed the application
687+
688+
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.
691+
692+
### [Flask](#tab/flask)
693+
694+
```yml
695+
name: flask-postgresql-sample-app
696+
metadata:
697+
698+
services:
699+
web:
700+
project: .
701+
language: py
702+
host: appservice
703+
```
704+
705+
### [Django](#tab/django)
706+
707+
```yml
708+
name: django-postgresql-sample-app
709+
metadata:
710+
711+
services:
712+
web:
713+
project: .
714+
language: py
715+
host: appservice
716+
```
717+
718+
---
719+
720+
## Remove the resources
721+
722+
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+
477730
## Frequently asked questions
478731

479732
- [How much does this setup cost?](#how-much-does-this-setup-cost)
480733
- [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)
481734
- [How does local app development work with GitHub Actions?](#how-does-local-app-development-work-with-github-actions)
482735
- [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)
485736

486737
#### How much does this setup cost?
487738

@@ -529,14 +780,6 @@ The [Django sample application](https://github.com/Azure-Samples/msdocs-django-p
529780

530781
For more information, see [Production settings for Django apps](configure-language-python.md#production-settings-for-django-apps).
531782

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-
540783
## Next steps
541784

542785
Advance to the next tutorial to learn how to secure your app with a custom domain and certificate.

articles/zone-pivot-groups.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2094,6 +2094,14 @@ groups:
20942094
title: Application provisioning
20952095
- id: cross-tenant-synchronization
20962096
title: Cross-tenant synchronization
2097+
- id: deploy-python-web-app-postgressql
2098+
title: Workflows
2099+
prompt: Choose your path
2100+
pivots:
2101+
- id: deploy-portal
2102+
title: Azure portal
2103+
- id: deploy-azd
2104+
title: Azure Developer CLI
20972105
# Owner: daknappe (Azure Virtual Desktop)
20982106
- id: azure-virtual-desktop-host-pool-types
20992107
title: Azure Virtual Desktop host pool management type
@@ -2102,4 +2110,4 @@ groups:
21022110
- id: host-pool-automated
21032111
title: Automated host pool
21042112
- id: host-pool-standard
2105-
title: Standard host pool
2113+
title: Standard host pool

0 commit comments

Comments
 (0)