18

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?

alh
  • 2,569
  • 6
  • 29
  • 42

7 Answers7

13

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
PachinSV
  • 3,680
  • 2
  • 29
  • 41
Will B
  • 131
  • 1
  • 5
12

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.

kukido
  • 10,431
  • 1
  • 45
  • 52
  • I dont seem to have a .ebextensions directory; should I? I tried updating the .elasticbeanstalk/optionsettings.myApp.config file changing the WSGIPath=... line, is there a difference between the two config files? – alh Dec 14 '13 at 10:13
  • .elasticbeanstalk contains metadata for eb tool itself, better not tamper with it. If you don't have .ebextensions directory, please create one and add config file there. I should've been more elaborative on that in the answer. – kukido Dec 15 '13 at 04:14
  • 1
    This is actually outdated as of awsebcli 3.0. I added an answer detailing how it is done now. – davetw12 Mar 02 '15 at 21:01
  • @davetw12 can you provide documentation that says that config files are out of date? – HaveAGuess Mar 03 '16 at 13:05
7

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

WSGIPath is set by default to application.py. Set to manage.py.

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

Tirtha R
  • 1,148
  • 1
  • 14
  • 24
5

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.

davetw12
  • 1,815
  • 2
  • 20
  • 27
  • 2
    You said in a comment above that the old .config way is outdated; but all the documentation still refers to that as the standard way to alter the environment. In fact, in my case the WSGIPath appears to be getting set by the saved configuration I use, but it doesn't appear at all in the local file I see when I use `eb config`. – Kylotan Jul 22 '15 at 18:01
  • @Kylotan I actually meant that eb config should give you direct access to the config file that exists on your AWS server. The command doesn't actually save one locally. My bad as I mistakingly said that it will pull the file. – davetw12 Jul 23 '15 at 18:16
  • 1
    `eb config get ` will download the config file for that saved configuration. And it makes no mention of the WSGIPath in there. But it does, somehow, overwrite or know to ignore what you have in your .ebextensions/*.config files when you use these saved configurations. – Kylotan Jul 24 '15 at 07:40
  • @kylotan `eb config` will show you every setting. `eb config save` will create a new saved configuration that shows ONLY the settings set at an api level. Meaning, if it isn't in your saved configuration, it will not override your ebextensions. – Nick Humrich Jul 26 '15 at 14:50
  • I found that confusing @davetw12 can I recommend updating your answer. This doesn't apply setting locally that can be redeployed later somewhere else so you don't have a repeatable build this way – HaveAGuess Mar 03 '16 at 13:02
1

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

Bedi Egilmez
  • 1,494
  • 1
  • 18
  • 26
1

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.

Bjc
  • 93
  • 6
0

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.

amar
  • 1
  • 1
  • Which files were a problem? I'm struggling with this right now. – burntsugar Mar 09 '19 at 12:08
  • Sorry for late reply; I was deploying my code with all python environment configuration related files/folders (pyvenv.cfg, Scripts, Lib), which was not required. EB does environment setup on its own. – amar Mar 18 '19 at 12:16