How to deploy structured Flask app on AWS elastic beanstalk

自古美人都是妖i 提交于 2019-11-28 05:06:48

Add the following to .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. You can find more information of what can be done regarding the container configuration in Customizing and Configuring AWS Elastic Beanstalk Environments guide.

Will B

I encountered a similar problem deploying a Flask application to EB, with a similar directory structure, and had to do 2 things:

  1. Update my manage.py to create an object of name application, not 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

    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.

This is described briefly at the official docs, and is described in more detail in this blog post

EDIT - see project structure below

  • ProjectRoot
    • .ebextensions
      • application.config
    • application
      • main
        • forms.py
        • views.py
    • static
    • templates
    • tests
    • manage.py
    • requirements.txt
    • config.py
    • etc, etc

As of awsebcli 3.0, you can actually edit your configuration settings to represent your WSGI path via eb config. The config command will then pull (and open it in your default command line text editor, i.e nano) an editable config based on your current configuration settings. You'll then search for WSGI and update it's path that way. After saving the file and exiting, your WSGI path will be updated automatically.

Your WSGIPath refers to a file that does not exist.

This error appears because Beanstalk, by default, looks for application.py. Check at Beanstalk web UI, Configuration > Software Configuration, WSGIPath is mapped to application.py

Update the WSGIPath as shown in the previous replies or rename to application.py file.

WSGI configuration was painful for me. I did changed WSCI settings using eb config command but it did not work. Below you can fix this in 5 easy steps.

1- Moved app.py function to the root of the directory (where I runned eb init command.

2- Also renamed app.py as application.py and in that initilized application as application = Flask(__name__) not 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. Delete all .extensions, .gitignore etc from your project.

4- re initialize your project on EB with eb init and follow the prompts. when deployment is done, eb open would launch your webapp (hopefully!)

When I encountered this problem it was because I was using the GUI to upload a zip of my project files. Initially I was zipping the project level directory and uploading that zip to 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.

Well, In my case I followed the entire process and conventions but was still getting 404. The problem was my virtual environment. I was ignoring all environment config related folders/files in my .gitignore but not in .ebignore. After creating .ebignore and ignoring all the folders/files which were not related to project code, fixed the issue.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!