Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 249
alh Avatar asked Dec 13 '13 03:12

alh


1 Answers

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
like image 164
Will B Avatar answered Oct 04 '22 09:10

Will B