3

I am trying to host a Flask app that implements a REST API using Flask-RESTful on Elastic Beanstalk through the eb cli. I have successfully deployed my application and I am not getting any error messages on my events log. I can successfully ssh into the instances and run the script to prepopulate my database and everything. But whenever I try to access any routes I keep getting a 404 error.

I initially assumed that it was because it wasn't finding my WSGIPath, so I changed the file name to application.py and updated the EBS software configuration to point to that file. I have also updated all instances of app to application in the codebase based on AWS documentation. Still nothing. Does anyone have any idea what could be wrong?

This is my application.py:

from flask import Flask

from config import Config

CONFIG = Config()

# AWS Elastic Beanstalk expects an `application` variable.
application = Flask(__name__)

if __name__ == "__main__":
    # Importing these here to avoid issue with circular imports
    from endpoints.v1.exports import exports_api
    from endpoints.v1.imports import imports_api

    application.register_blueprint(exports_api, url_prefix="/api/v1")
    application.register_blueprint(imports_api, url_prefix="/api/v1")

    application.run(debug=CONFIG.DEBUG)

Here is where the application.py file lies in the folder structure:

- project root
    - ...
    - application.py
    - ... 
MarcioPorto
  • 555
  • 7
  • 22
  • Does the Flask application start correctly? Have you tried hosting the app on a different port (just for testing it out) and allowing through the firewall? Also, if you're putting everything in the operating system root that's not the best idea and may cause problems going forwards. Stupid question, but are you also sure you are accessing the right URL, because it's odd that there are no logs. – rassa45 May 23 '18 at 17:53
  • That `root` here is the root of the project, not the OS. My bad, I'll update the question to make it clearer. And for context, I am new to both Flask and EBS, so how can I test that the application starts correctly? I am assuming it does because I don't get a WSGIPath error in the event logs like other people I read did. And I am accessing the URL shown on the EBS environment menu. I copied and pasted it a million times to be sure and I keep getting the 404. – MarcioPorto May 23 '18 at 18:42
  • So Flask by default runs on port 5000. Can you reverse proxy whatever is on 80 to connect to that via WSGI, or host Flask on 80 if you want to expose it?. Otherwise, to use 5000 you need to allow that through the AWS firewall. When you go the normal link, I don't think you are accessing Flask. Also, Flask is not an HTML page so you actually have to start it with `python application.py`, you probably know that but covering all bases here. – rassa45 May 23 '18 at 18:48

1 Answers1

2

Just realized what was going on. I was trying to register the blueprints inside of the if __name__ == "__main__": block. But since the application.py file is not run directly by Elastic Beanstalk, it was never reaching those lines and so the endpoints I was trying to hit didn't exist.

I had to refactor my code so I could move it outside of that block and avoid issues with circular imports, but now it's all working as it should.

MarcioPorto
  • 555
  • 7
  • 22