[英]How to deploy Flask app to AWS Elastic beanstalk using an application factory
I'm trying to deploy my Flask application to AWS and am getting a 403 Forbidden error. 我正在尝试将Flask应用程序部署到AWS并收到403 Forbidden错误。 After much testing with AWS, I've seemed to figure out that it's an error on there end. 经过AWS的大量测试后,我似乎发现那是一个错误。 My Flask App currently is written using an application factory, and looks something like this (simplified): 目前,我的Flask应用是使用应用工厂编写的,看起来像这样(简化):
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. 但是,在Elastic beantalk上设置环境并在其中部署我的Flask应用程序之后,我访问了索引页面,我收到403 Forbidden错误。 I check my logs for EB, and see an error saying 我在日志中查看了EB,并看到一条错误消息:
"Target WSGI script '/opt/python/current/app/server/application.py' does not contain WSGI application 'application'." “目标WSGI脚本'/opt/python/current/app/server/application.py'不包含WSGI应用程序'application'。”
At first I was confused. 起初我很困惑。 I had previously had other errors with EB and had looked online. 我以前在EB上遇到其他错误,并在网上查看。 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. 我不太了解WSGI标准是什么,但是我看到有人说EB仅在Flask的“ app”对象命名为“ application”时才起作用,而我有意对其进行了更改。 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: 然后,我查看了AWS文档,并意识到他们的示例未使用应用程序工厂,因此直觉上我测试了不使用应用程序工厂:
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. 我只能得出结论,因为“应用程序”对象是在应用程序工厂中定义的,或者它返回的是应用程序而不是使用application.run()的事实,所以它没有在EB中注册。 Again, I don't really understand WSGI and what it has to do with the error. 同样,我不太了解WSGI及其与错误的关系。 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? 由于我不想解决大部分项目,因此如何解决这个问题并与应用程序工厂一起部署?
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. 将application
分配为与对create_app
的调用返回的值相等,可以满足EB的要求,即将实现WSGI的可调用对象公开为application
名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.