pythonamazon-web-servicesflaskaws-lambdazappa

Zappa+Flask: A GET request to '/' yielded a 502 response code


I am trying to deploy my flask app on aws lambda via zappa. And I am getting the following error when I do zappa deploy dev:

Calling deploy for stage dev..
Downloading and installing dependencies..
 - sqlalchemy==1.3.16: Downloading
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.24M/1.24M [00:00<00:00, 6.06MB/s]
 - psycopg2-binary==2.8.5: Downloading
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 2.94M/2.94M [00:00<00:00, 8.30MB/s]
 - pandas==1.0.3: Using locally cached manylinux wheel
 - numpy==1.18.3: Using locally cached manylinux wheel
 - markupsafe==1.1.1: Downloading
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 27.5k/27.5k [00:00<00:00, 1.46MB/s]
Packaging project as zip.
Uploading prices-service-dev-1587997268.zip (44.2MiB)..
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 46.3M/46.3M [01:05<00:00, 710kB/s]
Scheduling..
Scheduled prices-service-dev-zappa-keep-warm-handler.keep_warm_callback with expression rate(4 minutes)!
Uploading prices-service-dev-template-1587997350.json (1.6KiB)..
100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.65k/1.65k [00:00<00:00, 19.7kB/s]
Waiting for stack prices-service-dev to create (this can take a bit)..
 75%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████                                          | 3/4 [00:09<00:03,  3.13s/res]
Deploying API Gateway..
Error: Warning! Status check on the deployed lambda failed. A GET request to '/' yielded a 502 response code.

When I do zappa init, it generates the following zappa_settings.json file:

{
    "dev": {
        "app_function": "main.app",
        "aws_region": "eu-central-1",
        "profile_name": "default",
        "project_name": "ps-zappa-test",
        "runtime": "python3.7",
        "s3_bucket": -MY_BUCKET_NAME-
    }
}

and my main.py file is:

import os
from app import create_app
from config import config_by_name
from app.extensions import db

config_name = os.getenv("FLASK_CONFIG") or "default"
app_config = config_by_name[config_name]
app = create_app(app_config)

db.create_all(app=app)

if __name__ == '__main__':
    app.run()

My dependencies:

aniso8601==8.0.0
argcomplete==1.11.1
attrs==19.3.0
beautifulsoup4==4.9.0
boto3==1.12.46
botocore==1.15.46
certifi==2020.4.5.1
cfn-flip==1.2.3
chardet==3.0.4
click==7.1.1
docutils==0.15.2
durationpy==0.5
entsoe-py==0.2.13
Flask==1.1.2
flask-marshmallow==0.12.0
flask-restx==0.2.0
Flask-SQLAlchemy==2.4.1
future==0.18.2
gunicorn==20.0.4
hjson==3.0.1
idna==2.9
importlib-metadata==1.6.0
itsdangerous==1.1.0
Jinja2==2.11.2
jmespath==0.9.5
jsonschema==3.2.0
kappa==0.6.0
MarkupSafe==1.1.1
marshmallow==3.5.1
marshmallow-sqlalchemy==0.23.0
more-itertools==8.2.0
numpy==1.18.3
packaging==20.3
pandas==1.0.3
pip-tools==5.0.0
placebo==0.9.0
pluggy==0.13.1
psycopg2-binary==2.8.5
py==1.8.1
pyparsing==2.4.7
pyrsistent==0.16.0
pytest==5.4.1
python-dateutil==2.6.1
python-dotenv==0.13.0
python-slugify==4.0.0
pytz==2019.3
PyYAML==5.3.1
requests==2.23.0
s3transfer==0.3.3
six==1.14.0
soupsieve==2.0
SQLAlchemy==1.3.16
text-unidecode==1.3
toml==0.10.0
tqdm==4.45.0
troposphere==2.6.0
urllib3==1.25.9
wcwidth==0.1.9
Werkzeug==0.16.1
wsgi-request-logger==0.4.6
zappa==0.51.0
zipp==3.1.0

When I do zappa tail, it gives:

Calling tail for stage dev..
[1587996139600] Instancing..
[1587996139773] Instancing..
[1587996143093] OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k
[1587996143255] OpenBLAS WARNING - could not determine the L2 cache size on this system, assuming 256k
[1587996148073] [ERROR] OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
[1587996148073] Is the server running on host "localhost" (127.0.0.1) and accepting
[1587996148073] TCP/IP connections on port 5432?
[1587996148073] could not create socket: Address family not supported by protocol
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)nectption_noconnection
[1587996148289] [ERROR] OperationalError: (psycopg2.OperationalError) could not connect to server: Connection refused
[1587996148289] Is the server running on host "localhost" (127.0.0.1) and accepting
[1587996148289] TCP/IP connections on port 5432?
[1587996148289] could not create socket: Address family not supported by protocol
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)nectption_noconnection

Any suggestions on how this issue can be solved?


Solution

  • From the zappa tail output, it seems that your function tries to connect localhost but since it is lambda I doubt there is a running postgres instance on localhost. You should update your connection settings for postgres (correct address of the remote postgres instance). If you are using sqlalchemy, you can check if SQLALCHEMY_DATABASE_URI is correct.