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 (Flask) web app with PostgreSQL in Azure
2
2
3
-
This is a Python web app using the Flask framework and the Azure Database for PostgreSQL relational database service. The Flask app is hosted in a fully managed Azure App Service. This app is designed to be be run locally and then deployed to Azure. For more information on how to use this web app, see the tutorial [*Deploy a Python (Django or Flask) web app with PostgreSQL in Azure*](https://docs.microsoft.com/en-us/azure/app-service/tutorial-python-postgresql-app).
4
-
5
-
If you need an Azure account, you can [create on for free](https://azure.microsoft.com/free/).
6
-
7
-
A Django sample application is also available for the article at https://github.com/Azure-Samples/msdocs-django-postgresql-sample-app.
3
+
This is a Python web app using the Flask framework and the Azure Database for PostgreSQL relational database service. The Flask app is hosted in a fully managed Azure App Service. This app is designed to be be run locally and then deployed to Azure. You can either deploy this project by following the tutorial [*Deploy a Python (Django or Flask) web app with PostgreSQL in Azure*](https://docs.microsoft.com/en-us/azure/app-service/tutorial-python-postgresql-app) or by using the [Azure Developer CLI (azd)](https://learn.microsoft.com/en-us/azure/developer/azure-developer-cli/overview?WT.mc_id=python-79651-pamelafox) according to the instructions below.
8
4
9
5
## Requirements
10
6
@@ -18,4 +14,84 @@ The [requirements.txt](./requirements.txt) has the following packages:
18
14
|[Flask-Migrate](https://pypi.org/project/Flask-Migrate/)| SQLAlchemy database migrations for Flask applications using Alembic. Allows functionality parity with Django version of this sample app.|
19
15
|[pyscopg2](https://pypi.org/project/psycopg2/)| PostgreSQL database adapter for Python. |
20
16
|[python-dotenv](https://pypi.org/project/python-dotenv/)| Read key-value pairs from .env file and set them as environment variables. In this sample app, those variables describe how to connect to the database locally. <br><br> Flask's [dotenv support](https://flask.palletsprojects.com/en/2.1.x/cli/#environment-variables-from-dotenv) sets environment variables automatically from an `.env` file. |
21
-
|[flask_wtf](https://pypi.org/project/Flask-WTF/)| Form rendering, validation, and CSRF protection for Flask with WTForms. Uses CSRFProtect extension. |
17
+
|[flask_wtf](https://pypi.org/project/Flask-WTF/)| Form rendering, validation, and CSRF protection for Flask with WTForms. Uses CSRFProtect extension. |
18
+
19
+
20
+
## Using this project with the Azure Developer CLI (azd)
21
+
22
+
This project is designed to work well with the [Azure Developer CLI](https://learn.microsoft.com/en-us/azure/developer/azure-developer-cli/overview?WT.mc_id=python-79651-pamelafox),
23
+
which makes it easier to develop apps locally, deploy them to Azure, and monitor them.
24
+
25
+
### Local development
26
+
27
+
This project has devcontainer support, so you can open it in Github Codespaces or local VS Code with the Dev Containers extension. If you're unable to open the devcontainer,
28
+
then it's best to first [create a Python virtual environment](https://docs.python.org/3/tutorial/venv.html#creating-virtual-environments) and activate that.
29
+
30
+
Install the requirements:
31
+
32
+
```shell
33
+
pip install -r requirements.txt
34
+
```
35
+
36
+
Create an `.env` file using `.env.sample` as a guide. Set the value of `DBNAME` to the name of an existing database in your local PostgreSQL instance. Set the values of `DBHOST`, `DBUSER`, and `DBPASS` as appropriate for your local PostgreSQL instance.
37
+
38
+
Run the migrations:
39
+
40
+
```shell
41
+
flask db migrate
42
+
```
43
+
44
+
Run the local server: (or use VS Code "Run" button and select "Run server")
45
+
46
+
```shell
47
+
flask run
48
+
```
49
+
50
+
### Deployment
51
+
52
+
This repo is set up for deployment on Azure App Service (w/PostGreSQL server) using the configuration files in the `infra` folder.
53
+
54
+
Steps for deployment:
55
+
56
+
1. Sign up for a [free Azure account](https://azure.microsoft.com/free/?WT.mc_id=python-79651-pamelafox)
57
+
2. Install the [Azure Dev CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/install-azd?WT.mc_id=python-79651-pamelafox). (If you opened this repository in a devcontainer, that part will be done for you.)
58
+
3. Provision and deploy all the resources:
59
+
60
+
```shell
61
+
azd up
62
+
```
63
+
64
+
It will prompt you to login and to provide a name (like "flask-app") and location (like "eastus"). Then it will provision the resources in your account and deploy the latest code. If you get an error with deployment, changing the location (like to "centralus") can help, as there are availability constraints for some of the resources.
65
+
66
+
4. When `azd` has finished deploying, you'll see an endpoint URI in the command output. Visit that URI, and you should see the front page of the restaurant review app! 🎉 If you see an error, open the Azure Portal from the URL in the command output, navigate to the App Service, select Logstream, and check the logs for any errors.
67
+
68
+

69
+
70
+
5. When you've made any changes to the app code, you can just run:
71
+
72
+
```shell
73
+
azd deploy
74
+
```
75
+
76
+
### CI/CD pipeline
77
+
78
+
This project includes a Github workflow for deploying the resources to Azure
79
+
on every push. That workflow requires several Azure-related authentication secrets to be stored as Github action secrets. To set that up, run:
80
+
81
+
```shell
82
+
azd pipeline config
83
+
```
84
+
85
+
### Monitoring
86
+
87
+
The deployed resources include a Log Analytics workspace with an Application Insights dashboard to measure metrics like server response time.
88
+
89
+
To open that dashboard, just run:
90
+
91
+
```shell
92
+
azd monitor --overview
93
+
```
94
+
95
+
## Getting help
96
+
97
+
If you're working with this project and running into issues, please post in [Discussions](/discussions).
0 commit comments