How to deploy structured Flask app on AWS elastic

2019-01-17 05:00发布

问题:

After successfully deploying a test app using the steps outlined here: 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.

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. and the application does not work (404 for any path).

How can I deploy the above Flask application to elastic beanstalk?

回答1:

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.



回答2:

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


回答3:

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.



回答4:

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.



回答5:

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!)



回答6:

When I encountered this problem it was because I was using the GUI to upload a zip of my project files. Initially I was uploading a zip of the project directory and got this error.

Then I switched to simply uploading 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.