Search code examples
pythonamazon-web-servicesflaskgunicornamazon-app-runner

Flask app running on AWS App Runner returns 502 Bad Gateway


I have a Flask application running in AWS App Runner with Gunicorn. It was running fine but I added a new feature which includes an API that calls to different endpoints and takes around 2-3 minutes to return a response.

In local it's working fine but when deployed it returns 502 Bad Gateway every time I call this API. Logs seem fine, no errors nor I can see any differences from running in local. All the other endpoints work fine.

Dockerfile

FROM python:3.11

ENV VIRTUAL_ENV=/opt/venv
RUN python3 -m venv $VIRTUAL_ENV
ENV PATH="$VIRTUAL_ENV/bin:$PATH"

RUN pip cache purge
RUN pip install --upgrade pip

# Install dependencies:
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Run the application:
COPY . .
CMD ["gunicorn", "--config", "gunicorn_config.py", "app:app"]

Gunicorn_config.py

import os

workers = int(os.environ.get('GUNICORN_PROCESSES', '2'))
threads = int(os.environ.get('GUNICORN_THREADS', '4'))
bind = os.environ.get('GUNICORN_BIND', '0.0.0.0:8080')
#timeout = int(os.environ.get('GUNICORN_TIMEOUT', '600'))

forwarded_allow_ips = '*'

secure_scheme_headers = { 'X-Forwarded-Proto': 'https' }

Solution

  • There is a 120 second timeout limit on AWS App Runner apps, see the documentation here:

    There is a total of 120 seconds request timeout limit on the HTTP requests. The 120 seconds include the time the application takes to read the request, including the body, and complete writing the HTTP response.