1

I'm trying to deploy my Flask application to AWS and am getting a 403 Forbidden error. After much testing with AWS, I've seemed to figure out that it's an error on there end. My Flask App currently is written using an application factory, and looks something like this (simplified):

from flask import Flask

def create_app(test_config=None):
    application = Flask(__name__)
    app = application

    @app.route('/')
    def index():
        return "The index page"

    ...

    return app

However, after I set up an environment on Elastic beanstalk and deploy my Flask app there, I visit the index page, I get a 403 Forbidden error. I check my logs for EB, and see an error saying

"Target WSGI script '/opt/python/current/app/server/application.py' does not contain WSGI application 'application'."

At first I was confused. I had previously had other errors with EB and had looked online. I don't really understand what the WSGI standard is, but I saw people saying that EB will only work if the Flask "app" object is named "application", which I purposefully changed. Then I looked at the AWS documentation and realized their examples didn't use an application factory, so on a hunch I tested not using the application factory:

from flask import Flask

application = Flask(__name__)    
app = application

...    

@app.route('/')
def index():
    return "The index page"

if __name__ == "__main__":
    application.run()

This worked. I can only conclude that because the "application" object is defined in the application factory, or the fact that it's returning the application instead of using application.run(), it isn't registering with EB. Again, I don't really understand WSGI and what it has to do with the error. How can I get around this and deploy with the application factory, since I don't want to get around rewriting most of my project?

1 Answer 1

2

The application factory creates the application instance, registers routes for it and returns the instance.

Assigning application to be equal to the value returned from the call to create_app fulfills the requirement by EB that a callable object which implements WSGI is exposed as application name.

Sign up to request clarification or add additional context in comments.

2 Comments

So you're saying that, after my original app factory 'create_app', I need to add a 'application = create_app()' and it'll work?
Hey there, your suggestion seemed to have worked! Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.