Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Prepare applications for Azure deployment by generating infrastructure code, Dockerfiles, and config files.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/services/app-service/templates/web-api.md
1# Web API Base Template — REFERENCE ONLY23Default template for REST API workloads on Azure App Service. Use when no specific integration is detected or as the base for recipe composition.45## Templates by Runtime67| Runtime | AZD Template | Framework |8|---------|-------------|-----------|9| C# (.NET) | `azd init -t todo-csharp` | ASP.NET Core Minimal API |10| Node.js (JS) | `azd init -t todo-nodejs-mongo` | Express.js |11| Node.js (TS) | `azd init -t todo-nodejs-mongo` | Express.js (TypeScript) |12| Python | `azd init -t todo-python-mongo` | FastAPI / Flask |13| Java | `azd init -t todo-java-mongo` | Spring Boot |1415**Browse all:** [Awesome AZD App Service](https://azure.github.io/awesome-azd/?tags=appservice)1617> ⚠️ The AZD templates above include Mongo/Cosmos by default. When using as a pure Web API base, strip the database layer before applying a recipe:18> - Remove `infra/app/db.bicep` (or equivalent database module)19> - Remove Cosmos/Mongo module references from `infra/main.bicep`20> - Remove Cosmos/Mongo packages from the application source21>22> Then apply the appropriate [recipe](recipes/README.md) for your data store.2324## Project Structure2526```27project-root/28├── azure.yaml # AZD service config29├── infra/30│ ├── main.bicep # Main orchestrator31│ ├── main.parameters.json32│ └── app/33│ ├── web.bicep # App Service + Plan34│ └── web-network.bicep # VNet integration (conditional)35└── src/36└── api/ # Application source37```3839## App Service Plan Configuration4041```bicep42resource appServicePlan 'Microsoft.Web/serverfarms@2023-12-01' = {43name: '${name}-plan'44location: location45tags: tags46sku: {47name: 'B1' // Dev/test — use P1v3+ for production48}49properties: {50reserved: true // Required for Linux51}52}53```5455| Environment | Recommended SKU | Notes |56|-------------|----------------|-------|57| Dev/Test | B1 | Basic tier, no autoscale |58| Production | P1v3 | Premium v3, autoscale, VNet integration |59| High-scale | P2v3 / P3v3 | Higher CPU/memory |6061## Health Check Endpoint6263> **CRITICAL**: Always include a health check endpoint. App Service uses it for instance monitoring.6465Configure in App Service:66```bicep67properties: {68siteConfig: {69healthCheckPath: '/health'70}71}72```7374### Health check by language7576**C# (ASP.NET Core)**77```csharp78app.MapGet("/health", () => Results.Ok(new { status = "healthy" }));79```8081**Node.js (Express)**82```javascript83app.get('/health', (req, res) => {84res.json({ status: 'healthy' });85});86```8788**Python (FastAPI)**89```python90@app.get("/health")91async def health():92return {"status": "healthy"}93```9495**Java (Spring Boot)**96```java97@GetMapping("/health")98public Map<String, String> health() {99return Map.of("status", "healthy");100}101```102103## Dockerfile Reference104105All App Service deployments can use container deployment. Each runtime should include a Dockerfile:106107**Python (FastAPI)**108```dockerfile109FROM python:3.12-slim110WORKDIR /app111COPY requirements.txt .112RUN pip install --no-cache-dir -r requirements.txt113COPY . .114EXPOSE 8000115CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]116```117118**Node.js**119```dockerfile120FROM node:20-slim121WORKDIR /app122COPY package*.json ./123RUN npm ci --production124COPY . .125EXPOSE 3000126CMD ["node", "src/index.js"]127```128129## App Settings (Managed Identity)130131> 💡 Call `mcp_bicep_get_az_resource_type_schema` with resource type `Microsoft.Web/sites` to validate properties before generating this resource.132133```bicep134resource webApp 'Microsoft.Web/sites@2023-12-01' = {135properties: {136siteConfig: {137appSettings: [138{ name: 'WEBSITE_HEALTHCHECK_MAXPINGFAILURES', value: '3' }139{ name: 'SCM_DO_BUILD_DURING_DEPLOYMENT', value: 'true' }140]141}142}143identity: {144type: 'UserAssigned'145userAssignedIdentities: {146'${managedIdentity.id}': {}147}148}149tags: {150'azd-service-name': 'api'151}152}153```154155> ⚠️ **Without `azd-service-name` tag, `azd deploy` fails with:**156> `resource not found: unable to find a resource tagged with 'azd-service-name: api'`157>158> The tag value must match the `services.<name>` key in `azure.yaml`. Use `api` for Web API services.159160## Deployment161162```bash163ENV_NAME="$(basename "$PWD" | tr '[:upper:]' '[:lower:]' | tr ' _' '-')-dev"164azd init -t <template> -e "$ENV_NAME" --no-prompt165azd env set AZURE_LOCATION eastus2166azd up --no-prompt167```168169**PowerShell:**170```powershell171$ENV_NAME = "$(Split-Path -Leaf (Get-Location) | ForEach-Object { $_.ToLower() -replace '[ _]','-' })-dev"172azd init -t <template> -e $ENV_NAME --no-prompt173azd env set AZURE_LOCATION eastus2174azd up --no-prompt175```176177## References178179- [App Service overview](https://learn.microsoft.com/en-us/azure/app-service/overview)180- [App Service best practices](https://learn.microsoft.com/en-us/azure/app-service/app-service-best-practices)181- [Health check](https://learn.microsoft.com/en-us/azure/app-service/monitor-instances-health-check)182