I have a REST API made using FastAPI that (apart from this issue) runs completely fine, whether hosted locally or on Elastic Beanstalk.
However, I have one endpoint that starts a background task (which takes about 30 seconds) that always ends in a 502 Bad Gateway error. I assumed this had to do with the automatic nginx timeout, but I have set all the timeouts to 600 in my .platform/nginx/conf.d/myconfig.conf file and added --timeout 300 to my Procfile (using Gunicorn) and still no luck.
Note that this process runs completely fine when the API is hosted locally. It's purely an EB issue. Also, my EB environment is running on Amazon Linux.
Here are the three functions involved with my issue:
## this is the background task
async def generate_pdf_task(order_id: str):
print("Generating PDF for order", order_id)
## This is a background task that takes around 30 seconds to generate a PDF
pdf_url = await upload_file(f"orders/{order_id}.zip", zip_buffer)
# Update order with PDF URL
await update_order(order_id, pdf_url)
print(f"✅ PDF available at: {pdf_url}")
## this is the endpoint that always causes 502 error
@router.post("/generate-pdf/{order_id}")
async def generate_pdf(order_id: str, background_tasks: BackgroundTasks):
print("Task started")
background_tasks.add_task(generate_pdf_task, order_id)
return {"message": "PDF generation started"}
## this function is for polling
@router.get("/get-pdf-url/{order_id}")
async def get_pdf_url(order_id: str):
print("Checking PDF status")
order = await fetch_order(order_id)
if not order:
raise HTTPException(status_code=404, detail="Order not found")
return {"pdf_url": order.get('pdf_url')}
The frontend is usually able to call get_pdf_url three times (in 5 second intervals) before it times out. Any ideas how I can fix this?