page_type | languages | products | urlFragment | name | description | |||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
sample |
|
|
simple-flask-app-terraform |
Simple Flask (Python) Web App (Terraform) on Azure |
A simple Python Flask website, made for demonstration purposes only, and deployed to Azure App service. Uses Azure Developer CLI (azd) to build, deploy, and monitor. |
This repository includes a simple Python Flask website, made for demonstration purposes only.
The project can be developed locally with Flask and can be deployed to Azure App Service using the infrastructure files in infra
. See below for more details.
This project has Dev Container support, so you can open it in GitHub Codespaces or local VS Code with the Dev Containers extension.
Steps for running the server:
-
(Optional) If you're unable to open the devcontainer, create a Python virtual environment and activate that.
-
Install the requirements:
python3 -m pip install -r requirements.txt
-
Run the local server:
python3 -m flask --debug run --port 50505
-
Click 'http://127.0.0.1:50505' in the terminal, which should open the website in a new tab.
-
Try the index page, try '/hello?name=yourname', and try a non-existent path (to see 404 error).
This template will create infrastructure and deploy code to Azure. If you don't have an Azure Subscription, you can sign up for a free account here. Make sure you have a contributor role to the Azure subscription.
The following prerequisites are required to use this application. Please ensure that you have them all installed locally.
- Azure Developer CLI
- Python (3.8+)
- Terraform CLI
- Requires the Azure CLI
This repo is set up for deployment on Azure App Service using the configuration files in the infra
folder.
Steps for deployment:
-
Run the following command to download the project code:
azd init -t john0isaac/simple-flask-app-terraform
-
Login to Azure Developer CLI:
azd auth login
-
Login to Azure using Azure CLI:
az login --use-device-code
-
Provision and deploy all the resources:
azd up
It will prompt you to provide an
azd
environment name (like "flaskapp"), 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! 🎉
Note
When you've made any changes to the app code, you can just run:
azd deploy
This application utilizes the following Azure resources:
- Azure App service to host the Python Web App.
- Azure Monitor for monitoring and logging.
Here's a high-level architecture diagram that illustrates these components. Notice that these are all contained within a single resource group, that will be created for you when you create the resources.
This template provisions resources to an Azure subscription that you will select upon provisioning them. Please refer to the Pricing calculator for Microsoft Azure and, if needed, update the included Azure resource definitions found in
infra/main.tf
to suit your needs.
At this point, you have a complete application deployed on Azure. But there is much more that the Azure Developer CLI can do. These next steps will introduce you to additional commands that will make creating applications on Azure much easier. Using the Azure Developer CLI, you can monitor your application, and test and debug locally.
-
azd deploy
to upload the changes to your application code to Azure. -
azd monitor
- to monitor the application and quickly navigate to the various Application Insights dashboards (e.g. overview, live metrics, logs) -
azd down
- to delete all the Azure resources created with this template.