Azure / azure-functions-python-worker

Python worker for Azure Functions.
http://aka.ms/azurefunctions
MIT License
331 stars 100 forks source link

Python FASTAPI az functions working locally but not when deployed #1352

Closed alexx087 closed 7 months ago

alexx087 commented 7 months ago

I have developed a Python FastAPI Azure Function that includes both HTTP-triggered and timer-scheduled triggers. I followed this Azure sample code to create the FastAPI Python Azure Function: Azure Functions Python Create FastAPI App. Additionally, I implemented the timer-scheduled trigger using the code suggested in this GitHub issue: Azure/azure-functions-python-worker#1284.

Everything works fine when tested locally, but upon deploying to Azure Functions, an error occurs stating 'HTTP trigger not found'. Please help :)

Here are my application settings: image

bhagyshricompany commented 7 months ago

Thanks for reporting will check and update.

YunchuWang commented 7 months ago
import logging
import datetime
import fastapi
from urllib import response

fast_app = fastapi.FastAPI()

@fast_app.get("/return_http_no_body")
async def return_http_no_body():
    return response(content='test', media_type='text/plain', status_code=200)

app = func.AsgiFunctionApp(app=fast_app, http_auth_level=func.AuthLevel.ANONYMOUS)

@app.schedule(schedule="0 */5 * * * *", arg_name="mytimer", run_on_startup=True)
def http_trigger(mytimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()
    if mytimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function ran at %s', utc_timestamp)

@alexx087 , thanks for reporting! turned out asgiapplication does not have function_name as decorator, can you try this sample? image working for me both local and prod

DCMattyG commented 5 months ago

@YunchuWang, I can also confirm this behavior. It happens when you add this decorator:

@app.function_name(name="<funcname>")

In #1284 you show an example where you have this decorator running locally, but in your above example this decorator is missing to rename the TimerTrigger function and this is what appears to cause it to break when running in Azure Functions.

Anytime I've attempted to add this decorator and deploy the code to Azure, the Function fails to load any functions and remains in a failed state.