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?
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?
- What does the rest of your infrastructure look like? Are you using a load balancer? Lambdas? Also see this answer for a potentially related fix. – Vegard Commented Feb 10 at 0:05
- @Vegard Yes, I am running a load balancer but I have already raised its idle timeout. My EB environment is Python 3.12 running Amazon Linux. It's a python rest API so I am using nginx. – rxnen Commented Feb 10 at 3:01
2 Answers
Reset to default 0If you are using AWS API Gateway, then its default timeout is 29 seconds and might cause the issue. Time timeout can be increased. See this Amazon post for more details.
since this question is also tagged with AWS
, i assume that the 502 error is coming from AWS load balancer, and not from the application
in any case you'd probably like to to make this api properly async rather than increase the LB timeouts(which may lead to future issues handling load and open connections..)
it seems that the issue is with generate_pdf_task
which uses await
try this approach (not bounded to fastAPI lifecycle, but should be good enough in this case)
@router.post("/generate-pdf/{order_id}")
async def generate_pdf(order_id: str):
print("Task started")
asyncio.create_task(generate_pdf_task(order_id))
return {"message": "PDF generation started"}