简体   繁体   English

如何在AWS弹性beanstalk上部署结构化Flask应用程序

[英]How to deploy structured Flask app on AWS elastic beanstalk

After successfully deploying a test app using the steps outlined here: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Python_flask.html 使用此处列出的步骤成功部署测试应用程序后: http//docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_Python_flask.html

I tried to deploy my actual flask application which has the following structure: 我试图部署我的实际烧瓶应用程序,它具有以下结构:

myApp/
   runServer.py
   requirements.txt
   myApp/
      __init__.py
      helpers.py
      clean.sh
      static/
         myApp.css
      handlers/
         __init__.py
         views.py
      templates/
         layout.html
         viewOne.html
         viewTwo.html

Where views.py contains my url mappings. 其中views.py包含我的url映射。

I have tried initializing the eb instance in the root directory as well as within the myApp module and git aws.push but I get the following error on the AWS dashboard: ERROR Your WSGIPath refers to a file that does not exist. 我已尝试在根目录以及myApp模块和git aws.push初始化eb实例,但我在AWS仪表板上收到以下错误: ERROR Your WSGIPath refers to a file that does not exist. and the application does not work (404 for any path). 并且应用程序不起作用(任何路径为404)。

How can I deploy the above Flask application to elastic beanstalk? 如何将上述Flask应用程序部署到弹性beanstalk?

Add the following to .ebextensions/<env-name>.config : 将以下内容添加到.ebextensions/<env-name>.config

option_settings:
  "aws:elasticbeanstalk:container:python":
    WSGIPath: myApp/handlers/views.py

Update: 更新:

If you don't have .ebextensions directory, please create one for the project. 如果您没有.ebextensions目录,请为项目创建一个。 You can find more information of what can be done regarding the container configuration in Customizing and Configuring AWS Elastic Beanstalk Environments guide. 您可以在“ 自定义和配置AWS Elastic Beanstalk环境”指南中找到有关容器配置的更多信息。

I encountered a similar problem deploying a Flask application to EB, with a similar directory structure, and had to do 2 things: 我遇到了类似的问题,将Flask应用程序部署到EB,具有类似的目录结构,并且必须做两件事:

  1. Update my manage.py to create an object of name application, not app 更新我的manage.py以创建名称应用程序的对象,而不是app

     import os from application import create_app, db from flask.ext.script import Manager, Shell application = create_app(os.getenv('FLASK_CONFIG') or 'default') manager = Manager(application) 
  2. Create .ebextensions/myapp.config , and define the following block to point to manage.py 创建.ebextensions/myapp.config ,并定义以下块以指向manage.py

     option_settings: "aws:elasticbeanstalk:container:python": WSGIPath: manage.py "aws:elasticbeanstalk:container:python:staticfiles": "/static/": "application/static/" 

This let Elastic Beanstalk find the application callable correctly. 这让Elastic Beanstalk正确地找到了可调用的应用程序。

This is described briefly at the official docs , and is described in more detail in this blog post 这在官方文档中进行简要描述,并在本博文中有更详细的描述

EDIT - see project structure below 编辑 - 参见下面的项目结构

  • ProjectRoot ProjectRoot
    • .ebextensions .ebextensions
      • application.config application.config
    • application 应用
      • main 主要
        • forms.py forms.py
        • views.py views.py
    • static 静态的
    • templates 模板
    • tests 测试
    • manage.py manage.py
    • requirements.txt requirements.txt
    • config.py config.py
    • etc, etc 等等

As of awsebcli 3.0, you can actually edit your configuration settings to represent your WSGI path via eb config . 从awsebcli 3.0开始,您实际上可以通过eb config编辑配置设置以表示您的WSGI路径。 The config command will then pull (and open it in your default command line text editor, ie nano) an editable config based on your current configuration settings. 然后, config命令将根据您当前的配置设置拉(并在默认的命令行文本编辑器中打开它,即nano)一个可编辑的配置。 You'll then search for WSGI and update it's path that way. 然后,您将搜索WSGI并以此方式更新它的路径。 After saving the file and exiting, your WSGI path will be updated automatically. 保存文件并退出后,您的WSGI路径将自动更新。

Your WSGIPath refers to a file that does not exist. 您的WSGIPath指的是不存在的文件。

This error appears because Beanstalk, by default, looks for application.py. 出现此错误是因为默认情况下,Beanstalk会查找application.py。 Check at Beanstalk web UI, Configuration > Software Configuration , WSGIPath is mapped to application.py 检查Beanstalk Web UI, Configuration > Software ConfigurationWSGIPath映射到application.py

WSGIPath默认设置为application.py。设置为manage.py。

Update the WSGIPath as shown in the previous replies or rename to application.py file. 如前面的回复中所示更新WSGIPath或重命名为application.py文件。

WSGI configuration was painful for me. WSGI配置对我来说很痛苦。 I did changed WSCI settings using eb config command but it did not work. 我确实使用eb config命令更改了WSCI设置,但它没有用。 Below you can fix this in 5 easy steps. 您可以通过以下5个简单步骤解决此问题。

1- Moved app.py function to the root of the directory (where I runned eb init command. 1-将app.py函数移动到目录的根目录(我在其中运行eb init命令。

2- Also renamed app.py as application.py and in that initilized application as application = Flask(__name__) not app = Flask(__name__) 2- app.py重命名为application.py并在该app.py application.py中将其重命名为application = Flask(__name__)而不是app = Flask(__name__)

3- eb deploy did not worked after this (in the same project) I tried to fix config by using eb config but it was too hairy to sort it out. 3 eb deploy在此之后没有工作(在同一个项目中)我试图通过使用eb config修复配置,但它太毛茸茸而无法解决它。 Delete all .extensions, .gitignore etc from your project. 从项目中删除所有.extensions,.gitignore等。

4- re initialize your project on EB with eb init and follow the prompts. 4-使用eb init在EB上重新初始化项目并按照提示操作。 when deployment is done, eb open would launch your webapp (hopefully!) 部署完成后, eb open会启动你的webapp(希望!)

When I encountered this problem it was because I was using the GUI to upload a zip of my project files. 当我遇到这个问题时,那是因为我使用GUI来上传我的项目文件的zip。 Initially I was zipping the project level directory and uploading that zip to EB. 最初我正在压缩项目级目录并将该zip文件上传到EB。

Then I switched to simply uploading a zip of the project files themselves-ie select all files and send those to a zip-and then the GUI upload utility was able to find my application.py file without a problem because the application.py file was not in a subfolder. 然后我切换到只是上传项目文件的拉链 - 即选择所有文件并将其发送到zip - 然后GUI上传实用程序能够找到我的application.py文件没有问题,因为application.py文件是不在子文件夹中。

Well, In my case I followed the entire process and conventions but was still getting 404. The problem was my virtual environment. 好吧,在我的情况下,我遵循了整个过程和约定,但仍然得到404.问题是我的虚拟环境。 I was ignoring all environment config related folders/files in my .gitignore but not in .ebignore. 我在.gitignore中忽略了所有与环境配置相关的文件夹/文件,但在.ebignore中没有。 After creating .ebignore and ignoring all the folders/files which were not related to project code, fixed the issue. 创建.ebignore并忽略与项目代码无关的所有文件夹/文件后,修复了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用应用程序工厂将Flask应用程序部署到AWS Elastic beantalk - How to deploy Flask app to AWS Elastic beanstalk using an application factory 尝试将Flask App部署到AWS Elastic Beanstalk时出错 - Error when Trying to Deploy Flask App to AWS Elastic Beanstalk 如何将 Visual Studio Flask 应用程序部署到 Elastic Beanstalk - How to deploy Visual Studio Flask app to Elastic Beanstalk 如何将具有多个 Docker 容器的应用程序部署到 AWS Elastic Beanstalk? - How to deploy an app with multiple Docker containers to AWS Elastic Beanstalk? ImportError - 将Falcon应用程序部署到AWS Elastic Beanstalk - ImportError - deploy Falcon app to AWS Elastic Beanstalk 在 AWS Elastic Beanstalk 中部署 Flask 应用程序 - Deploying Flask App in AWS Elastic Beanstalk 我试图在 AWS 弹性豆茎上部署 flask 应用程序,但收到此错误 - I was trying to deploy a flask app on AWS elastic beanstalk, but am getting this error 尝试将 Flask 应用程序部署到 AWS Elastic Beanstalk 时,我不断收到“ModuleNotFoundError: No module named &#39;app&#39;”错误消息 - When trying to deploy a Flask app to AWS Elastic Beanstalk, I keep getting the "ModuleNotFoundError: No module named 'app'" error message AWS Elastic Beanstalk - Flask 部署 - AWS Elastic Beanstalk - Flask deployment AWS Elastic Beanstalk - Flask 应用程序无法导入自定义模块 - AWS Elastic Beanstalk - Flask App Cannot Import Custom Module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM