Azure / Azure/azure-functions-host

AlwaysOn with AuthLevel.FUNCTION fails - need guidance for FastAPI + authenticated warmup

Open
#10,758 7 comments 0 reactions 0 assignees View on GitHub
needs-investigation
Dominant language
C#
Stars
2k
Forks
482
Avg merge
2d 12h
Merged PRs (30d)
38

Description

Related to #9109 which identified that AlwaysOn uses `/admin/warmup` endpoint.

## Issue
We're seeing 401 errors with AlwaysOn when using `AuthLevel.FUNCTION` with FastAPI on Azure Functions. The `/admin/warmup` endpoint used by AlwaysOn doesn't include function keys, causing authentication failures.

## Current setup
```python
# function_app.py
import azure.functions as func
from fastapi import FastAPI

fast_app = FastAPI()
app = func.AsgiFunctionApp(app=fast_app, http_auth_level=func.AuthLevel.FUNCTION)
```

- Premium plan with AlwaysOn enabled
- Function authentication level set to FUNCTION

## Attempted solutions
1. Added web.config to scripts folder:
```xml













```
Result: Configuration does not get deployed to wwwroot

2. Tried `WEBSITE_WARMUP_PATH` setting
Result: Documentation confirms this doesn't affect AlwaysOn path

## Logs
```
2024-04-04T05:58:14Z [Information] Executing StatusCodeResult, setting HTTP status code 401
```

## Key Questions
1. Is there a way to set different auth levels per route while keeping app-level auth?
2. Can we configure AlwaysOn to use function keys?
3. What's the recommended approach for FastAPI apps that need both security and AlwaysOn?

## Environment
- Python 3.11
- Azure Functions runtime v4
- FastAPI on Azure Functions
- Authentication Level: FUNCTION
- Hosting: Premium plan
### extra context
* https://www.thebestcsharpprogrammerintheworld.com/2018/08/23/how-to-optimize-azure-app-service-alwayson-cold-start-warm-up-request/
* https://learn.microsoft.com/en-us/azure/app-service/reference-app-settings?tabs=kudu%2Cdotnet

Please provide guidance on the recommended approach to handle this authentication scenario without compromising security by switching to `AuthLevel.ANONYMOUS`.

Contributor guide

Open the contributing guide

Research direction

Start with function_app.py and the AsgiFunctionApp authentication configuration, then trace how the /admin/warmup request is handled. Review the reported web.config deployment result, WEBSITE_WARMUP_PATH behavior, and the 401 log. Done means establishing and documenting a supported approach for authenticated FastAPI warmup without weakening the app's security.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, fastapi, python
Domain
api, authentication, backend, cloud
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.