Deploy a Python Flask application to Azure App Service
To deploy a Python Flask application to Azure App Service, you can follow these steps:
Prerequisites:
- Azure account (create one if you don’t have one: Azure Free Account).
- Install Azure CLI: Install Azure CLI.
- Install Git: Download Git.
- Have Python 3.x installed.
Steps for Deployment:
1. Create a Flask Application (if not already done)
Ensure your Flask app is structured like this:
2. Create a requirements.txt
File
You can create this file by running:
This will include all the required Python dependencies, including Flask
and azure-cosmos
.
3. Create a runtime.txt
File
In the root of your project, create a runtime.txt
file that specifies the Python version. For example:
4. Prepare a web.config
for Azure App Service (Optional)
If you need to configure the web server for Azure, create a web.config
file. For Python Flask apps, this is often not needed, but it can help for specific configurations.
5. Initialize Git Repository
If you haven’t initialized your Git repository, you can do so by running:
6. Create an Azure App Service
- Open Azure CLI and log in to your Azure account:
- Create a resource group (if you don’t have one already):
- Create an Azure App Service Plan:
- Create a Web App:
Replace my-flask-app
with a unique name for your web app. This name will be used in the URL to access your app (e.g., https://my-flask-app.azurewebsites.net
).
7. Deploy Your Flask Application
Once your Azure Web App is created, you can deploy your application.
- Push Code to Azure App Service Using Git: First, add the Azure remote repository to your Git configuration:
This will return a Git URL like https://<deployment_user>@my-flask-app.scm.azurewebsites.net/my-flask-app.git
.
- Add the Remote Git Repository:
Replace <deployment_git_url>
with the URL you received in the previous step.
- Push the Code:
This will push your Flask application to Azure App Service, where it will automatically install dependencies listed in requirements.txt
, set up the Python runtime, and start your app.
8. Access Your Application
Once the deployment is complete, you can access your Flask application by navigating to:
9. Monitor and Troubleshoot
If you need to troubleshoot or monitor your app:
- Check logs:
- Use the Azure Portal to view app diagnostics, logs, and scaling options.
Optional: Configure Custom Domain and SSL
If you want to configure a custom domain or enable SSL for your application, follow these steps in the Azure portal:
- Go to your app service.
- Under "Custom domains" or "TLS/SSL settings," configure the desired settings.
This is the typical process for deploying a Python Flask application to Azure App Service.