1

I am testing my Flask app on AWS EC2 instance (Ubuntu).

Main app:

from sonip.api.factory import create_app
app = create_app()
def main():
    app.run(debug=True, threaded=True)
if __name__ == "__main__":
    main()

The actual set up of the Flask app is done in a factory including registering blueprint, etc.

def create_app():
    app = Flask(__name__)
    app.config['SERVER_NAME'] = settings.FLASK_SERVER_NAME
    app.config['SWAGGER_UI_DOC_EXPANSION'] = settings.RESTPLUS_SWAGGER_UI_DOC_EXPANSION
    app.config['RESTPLUS_VALIDATE'] = settings.RESTPLUS_VALIDATE
    app.config['RESTPLUS_MASK_SWAGGER'] = settings.RESTPLUS_MASK_SWAGGER
    app.config['ERROR_404_HELP'] = settings.RESTPLUS_ERROR_404_HELP
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://{}:{}@{}:{}/{}".format(
        settings.DB_USER,
        settings.DB_PASS,
        settings.DB_HOST,
        settings.DB_PORT,
        settings.DB_NAME)
    db.init_app(app)
    blueprint = Blueprint('api', __name__, url_prefix='/api')
    app.register_blueprint(blueprint)

    return app

When I run python application.py and use curl -X GET http://localhost:5000/api, it returns the correct Swagger page. However, if I tried to run the app by specifying host=0.0.0.0 for external traffic, I got 404 for the same request.

(env) ubuntu@ip-172-31-18-136:~/aae$ python application.py
 * Serving Flask app "sonip.api.factory" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://localhost:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 948-062-124
127.0.0.1 - - [16/May/2018 18:07:24] "GET /api/ HTTP/1.1" 200 -
♥(env) ubuntu@ip-172-31-18-136:~/aae$ vi application.py
(env) ubuntu@ip-172-31-18-136:~/aae$ python application.py
 * Serving Flask app "sonip.api.factory" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 948-062-124
165.225.34.185 - - [16/May/2018 18:08:28] "GET /api/ HTTP/1.1" 404 -

Port 5000 is open to allow all inbound traffic in the security group. I tried a vanilla Flask app with just a few line of code, it worked just fine.

app = Flask(__name__)
if __name__ == '__main__':
    app.run(debug=True, port=8080, host='0.0.0.0')

This at least means 5000 is fine. Could it be the Blueprint or Swagger?

ddd
  • 4,665
  • 14
  • 69
  • 125

1 Answers1

2

Setting SERVER_NAME and changing the host/port to something different in app.run() used to be a recipe for problems. At best, it's under-documented.

Try changing settings.FLASK_SERVER_NAME to 0.0.0.0:5000. Or if your app wants to be using cookies, try the trick of using something.dev:5000 and adding an entry for something.dev to your local /etc/hosts.

Dave W. Smith
  • 24,318
  • 4
  • 40
  • 46
  • @DaveWSmith I changed setting.FLASK_SERVER_NAME to `0.0.0.0:5000` but it didn't help. However removing the line `app.config['SERVER_NAME'] = settings.FLASK_SERVER_NAME` did the trick – ddd May 16 '18 at 18:59