This repository includes a Flask surveys app that uses SQLAlchemy (via Flask-SQLAlchemy and Flask-Migrate) to interact with a PostgreSQL database.
The app is organized using Flask Blueprints, tested with pytest, linted with ruff, and formatted with black. Code quality issues are all checked with both pre-commit and Github actions.
The repository is designed for use with Docker containers, both for local development and deployment, and includes infrastructure files for deployment to Azure Container Apps. 🐳
This project has Dev Container support, so it will be be setup automatically if you open it in Github Codespaces or in local VS Code with the Dev Containers extension.
If you're not using one of those options for opening the project, then you'll need to:
-
Create a Python virtual environment and activate it.
-
Install requirements:
python3 -m pip install --user -r requirements-dev.txt
-
Install the pre-commit hooks:
pre-commit install
Since this app depends on a database, there's a docker-compose.yaml
file that creates two containers
(one for the app, one for the DB) as well as a volume to store the database data.
-
Install Docker Desktop. If you opened this inside Github Codespaces or a Dev Container in VS Code, installation is not needed.
⚠️ If you're on an Apple M1/M2, you won't be able to rundocker
commands inside a Dev Container; either use Codespaces or do not open the Dev Container. -
Create an
.env
file by copying.env.example
. -
Start the services with this command:
docker-compose up --build
-
Try creating a new survey and answering your newly created survey. 📋 🎉
This repo is set up for deployment on Azure Container Apps with a PostGreSQL server using the Dockerfile
and the configuration files in the infra
folder.
Steps for deployment:
-
Sign up for a free Azure account and create an Azure Subscription.
-
Install the Azure Developer CLI. (If you open this repository in Codespaces or with the VS Code Dev Containers extension, that part will be done for you.)
-
Login to Azure:
azd auth login
-
Provision and deploy all the resources:
azd up
It will prompt you to provide an
azd
environment name (like "flask-app"), select a subscription from your Azure account, and select a 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 can help, as there may be availability constraints for some of the resources. -
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 app! 🎉 -
When you've made any changes to the app code, you can just run:
azd deploy
This project includes a Github workflow for deploying the resources to Azure on every push to main. That workflow requires several Azure-related authentication secrets to be stored as Github action secrets. To set that up, run:
azd pipeline config
Pricing varies per region and usage, so it isn't possible to predict exact costs for your usage. Some of the Azure resources used in this infrastructure are on usage-based pricing tiers, but the Azure Database for PostgreSQL flexible server and Container Registry have fixed hourly costs.
You can try the Azure pricing calculator for the resources:
- Azure Container App: Consumption tier with 0.5 CPU, 1GiB memory/storage. Pricing is based on resource allocation, and each month allows for a certain amount of free usage. Pricing
- Azure Container Registry: Basic tier. Pricing
- Azure Database for PostgreSQL flexible server - Burstable tier (B1ms). Pricing
- Key Vault - Standard tier. Pricing based on number of operations, only a few are used on each deploy. Pricing
- Log analytics: Pay-as-you-go tier. Costs based on data ingested. Pricing
azd down
.
If you're working with this project and running into issues, please post in Discussions.