|
1 | 1 | # Deploy a Python (Flask) web app to Azure App Service - Sample Application
|
2 | 2 |
|
3 |
| -This is the sample Flask application for the Azure Quickstart [Deploy a Python (Django or Flask) web app to Azure App Service](https://docs.microsoft.com/en-us/azure/app-service/quickstart-python). For instructions on how to create the Azure resources and deploy the application to Azure, refer to the Quickstart article. |
| 3 | +This project demonstrates how to deploy a containerized Python Flask application to Azure using Infrastructure as Code (IaC) with Bicep and GitHub Actions for CI/CD. |
4 | 4 |
|
5 |
| -Sample applications are available for the other frameworks here: |
| 5 | +## Infrastructure Components |
6 | 6 |
|
7 |
| -* Django [https://github.com/Azure-Samples/msdocs-python-django-webapp-quickstart](https://github.com/Azure-Samples/msdocs-python-django-webapp-quickstart) |
8 |
| -* FastAPI [https://github.com/Azure-Samples/msdocs-python-fastapi-webapp-quickstart](https://github.com/Azure-Samples/msdocs-python-fastapi-webapp-quickstart) |
| 7 | +The solution consists of three main Azure resources: |
9 | 8 |
|
10 |
| -If you need an Azure account, you can [create one for free](https://azure.microsoft.com/en-us/free/). |
| 9 | +1. **Azure Key Vault** - Securely stores credentials |
| 10 | +2. **Azure Container Registry (ACR)** - Hosts the application container images |
| 11 | +3. **Azure Web App** - Runs the containerized application |
| 12 | + |
| 13 | +## Project Structure |
| 14 | + |
| 15 | +``` |
| 16 | +├── .github/workflows/ |
| 17 | +│ └── workflow.yaml # GitHub Actions CI/CD pipeline |
| 18 | +├── modules/ |
| 19 | +│ ├── key-vault.bicep # Key Vault infrastructure |
| 20 | +│ ├── container-registry.bicep # ACR infrastructure |
| 21 | +│ └── web-app.bicep # Web App infrastructure |
| 22 | +├── main.bicep # Main infrastructure template |
| 23 | +├── main.parameters.json # Infrastructure parameters |
| 24 | +└── Dockerfile # Container image definition |
| 25 | +``` |
| 26 | + |
| 27 | +## Getting Started |
| 28 | + |
| 29 | +### 1. Customize Parameters |
| 30 | + |
| 31 | +Before deploying, modify `main.parameters.json` to replace existing names with your own: |
| 32 | + |
| 33 | +- Replace all instances of `dkumlin` with your identifier |
| 34 | +- Example changes: |
| 35 | + ```json |
| 36 | + { |
| 37 | + "keyVaultName": { "value": "yourname-kv" }, |
| 38 | + "containerRegistryName": { "value": "yournamecr" }, |
| 39 | + "webAppName": { "value": "yourname-webapp" } |
| 40 | + } |
| 41 | + ``` |
| 42 | + |
| 43 | +### 2. Update GitHub Workflow |
| 44 | + |
| 45 | +Modify `.github/workflows/workflow.yaml` environment variables: |
| 46 | + |
| 47 | +```yaml |
| 48 | +env: |
| 49 | + KEY_VAULT_NAME_DEV: "yourname-kv" |
| 50 | + CONTAINER_REGISTRY_SERVER_URL_DEV: "yournamecr.azurecr.io" |
| 51 | + IMAGE_NAME_DEV: "yourname-app" |
| 52 | + WEB_APP: "yourname-webapp" |
| 53 | +``` |
| 54 | +
|
| 55 | +### 3. Configure GitHub Secrets |
| 56 | +
|
| 57 | +Set up required GitHub repository secrets: |
| 58 | +
|
| 59 | +- `AZURE_CREDENTIALS`: Service principal credentials |
| 60 | +- `AZURE_SUBSCRIPTION`: Subscription ID (For the resource subsciption) |
| 61 | + |
| 62 | +## How the Infrastructure Works |
| 63 | + |
| 64 | +### Deployment Flow |
| 65 | + |
| 66 | +1. **Infrastructure Deployment** |
| 67 | + |
| 68 | + - Bicep templates create/update Azure resources |
| 69 | + - RBAC permissions are configured automatically |
| 70 | + - Resources are created in the specified order: |
| 71 | + 1. Key Vault |
| 72 | + 2. Container Registry |
| 73 | + 3. Web App |
| 74 | + |
| 75 | +2. **Application Deployment** |
| 76 | + - GitHub Actions workflow: |
| 77 | + 1. Builds container image |
| 78 | + 2. Retrieves ACR credentials from Key Vault |
| 79 | + 3. Pushes image to ACR |
| 80 | + 4. Deploys to Web App |
| 81 | + |
| 82 | +### Customizing the Infrastructure |
| 83 | + |
| 84 | +#### Modify Key Vault (modules/key-vault.bicep) |
| 85 | + |
| 86 | +- Change SKU tier |
| 87 | +- Add/modify role assignments |
| 88 | +- Adjust network access rules |
| 89 | + |
| 90 | +```bicep |
| 91 | +resource keyVault 'Microsoft.KeyVault/vaults@2022-07-01' = { |
| 92 | + properties: { |
| 93 | + sku: { |
| 94 | + family: 'A' |
| 95 | + name: 'standard' // Change to 'premium' if needed |
| 96 | + } |
| 97 | + // Add network rules here |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +#### Customize Container Registry (modules/container-registry.bicep) |
| 103 | + |
| 104 | +- Change SKU |
| 105 | +- Enable/disable features |
| 106 | +- Configure geo-replication |
| 107 | + |
| 108 | +```bicep |
| 109 | +resource acr 'Microsoft.ContainerRegistry/registries@2023-01-01-preview' = { |
| 110 | + sku: { |
| 111 | + name: 'Basic' // Change to 'Standard' or 'Premium' |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +#### Modify Web App (modules/web-app.bicep) |
| 117 | + |
| 118 | +- Change pricing tier |
| 119 | +- Adjust app settings |
| 120 | +- Configure scaling |
| 121 | + |
| 122 | +```bicep |
| 123 | +resource webApp 'Microsoft.Web/sites@2022-03-01' = { |
| 124 | + properties: { |
| 125 | + siteConfig: { |
| 126 | + linuxFxVersion: 'DOCKER|${containerImage}' |
| 127 | + // Add custom app settings |
| 128 | + appSettings: [] |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +## Troubleshooting |
| 135 | + |
| 136 | +Common issues and solutions: |
| 137 | + |
| 138 | +1. **Deployment Failures** |
| 139 | + |
| 140 | + - Check resource name uniqueness |
| 141 | + - Verify service principal permissions |
| 142 | + - Review deployment logs in Azure Portal |
| 143 | + |
| 144 | +2. **Container Issues** |
| 145 | + |
| 146 | + - Verify ACR credentials in Key Vault |
| 147 | + - Check container logs in Web App |
| 148 | + - Ensure container image exists in ACR |
| 149 | + |
| 150 | +3. **Access Issues** |
| 151 | + - Check RBAC role assignments |
| 152 | + - Verify service principal hasn't expired |
| 153 | + - Confirm Key Vault access policies |
| 154 | + |
| 155 | +## Prerequisites |
| 156 | + |
| 157 | +- Azure Subscription |
| 158 | +- GitHub Account |
| 159 | +- Azure CLI (for local testing) |
| 160 | +- Docker (for local testing) |
0 commit comments