Rectangle 27 9

.ebextensions/<env-name>.config
option_settings:
  "aws:elasticbeanstalk:container:python":
    WSGIPath: myApp/handlers/views.py

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.

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?

.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.

This is actually outdated as of awsebcli 3.0. I added an answer detailing how it is done now.

@davetw12 can you provide documentation that says that config files are out of date?

python - How to deploy structured Flask app on AWS elastic beanstalk -...

python amazon-web-services flask elastic-beanstalk
Rectangle 27 5

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

  • 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)
  • 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.

python - How to deploy structured Flask app on AWS elastic beanstalk -...

python amazon-web-services flask elastic-beanstalk
Rectangle 27 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.

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 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.

eb config get <whatever> 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 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.

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

python - How to deploy structured Flask app on AWS elastic beanstalk -...

python amazon-web-services flask elastic-beanstalk
Rectangle 27 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.

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

python - How to deploy structured Flask app on AWS elastic beanstalk -...

python amazon-web-services flask elastic-beanstalk
Rectangle 27 0

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.

python - How to deploy structured Flask app on AWS elastic beanstalk -...

python amazon-web-services flask elastic-beanstalk
Rectangle 27 0

As mentioned in the http://flask.pocoo.org/docs/patterns/packages/ documentation in a flask app architecture like a package you get a directory structure that looks like :

/yourapplication
        /yourapplication
            /__init__.py
            /static
                /style.css
            /templates
                layout.html
                index.html
                login.html
            /views
                /__init_.py
        ... 
    ...

And you put most of your code withing you __init__.py files so that you can call your views and application as modules of your package.

But as mentioned here you are still using application.py file which doesn't seems to be a part of package. Also While working in a architecture like this you need to be very careful about circular imports as mentioned on the same link near the end of the webpage.

saying that putting most of your code in __init__.py is not fair. you can go ahead and make it modular buy adding muliples files in a module and importing all of them into the __init__.py file and then import the module. eg. you can have many different views in admin.py ,users.py etc and then importing all into __init__.py of views folder.

pygoku - I've read through that section of flask and I'm pretty sure my application structure is correct. As I noted, the imports work perfectly running with the flask development server and also when running via uwsgi (proxied through nginx). Its only when I pull this into AWS elastic beanstalk (serving via mod_wsgi) that I get 404s. Additionally there are no errors, and the files are definitely being imported as app.logger lines in the root of the message/views.py file are being put into the logs.

python - Flask 404 errors when deployed to AWS Elastic Beanstalk - Sta...

python flask elastic-beanstalk
Rectangle 27 0

As of this writing, after spending many hours fighting with AWS EB's config, I gave up trying to make the static files work the way we all expect and updated my Flask app creation to:

app = Flask(__name__, static_url_path='/s')

This renders urls like /s/scripts/my-script.js and since I always use url_for('static', ...) in my code and templates, everything continued to work outside of AWS as well.

Update on 9/30/2013: I can pretty much guarantee that the staticFiles settings are completely ignored in AWS EB's Python container.

The change I suggested above has the undesirable downside of routing all static file requests through Flask (maybe, more accurately, WSGI.) That's not very hard to fix, though.

Alias /s /opt/python/current/app/static
<Directory /opt/python/current/app/static>
Order allow,deny
Allow from all
</Directory>

This config tells Apache to take over any requests for URLs starting with /s, same prefix we chose for our static files, and serve files from our app's static folder.

container_commands:
  add_apache_conf:
    command: "cp app-httpd.conf /etc/httpd/conf.d"

This file will be used during the app deployment and will copy the new .config file to a directory from which Apache is configure to load all .config files it sees.

.ebextensions/*.config
.elasticbeanstalk/optionsettings.*

The .elasticbeanstalk directory, as far as I understand, keeps local settings for your project. These settings are used by the eb command line tools, including when you run git aws.push. I personally stopped using these tools and wrote my own scripts using their API. In my case, for Python, I use the Boto package. The stuff in .ebextensions are included with your app when it's deployed or updated. It contains settings or commands that will be executed in your app servers upon deployment.

python - Static assets don't show up for flask on elastic beanstalk - ...

python amazon-web-services flask elastic-beanstalk
Rectangle 27 0

Well it turns out I wasn't doing anything wrong and this wasn't an error with AWS or Flask, it was strictly a SQLAlchemy bug! It turns out the URI format for MySQL connections is:

dialect+driver://username:password@host:port/database

My bug came from the fact that my password with a plus (+) character in it thus fooling the URI format into thinking everything before it was the dialect. Using a '+' character is completely legal for MySQL passwords and thus why connecting via the MySQL client was working. I hope anyone else with this error is able to find this and not spend as much time as I did trying to find a solution!

amazon web services - Unable to connect to AWS MySQL RDS instance via ...

amazon-web-services flask elastic-beanstalk flask-sqlalchemy amazon-rds
Rectangle 27 0

This seems to be the problem in your logs. (You already identified it in the comments it seems).

File "retask/queue.py", line 32, in <module>

    import redis

ImportError: No module named redis**strong text**

Do you have "redis==2.4.13" (or any other version) in your requirements.txt?

Also you are right that if you want to persist changes on all instances you should use ".ebextensions" config file.

warning: no previously-included files found matching '__pycache__' warning: no previously-included files matching '*.pyc' found under directory 'tests'

python 2.7 - Deploying Flask app on AWS elastic beanstalk - Stack Over...

python-2.7 amazon-web-services flask elastic-beanstalk
Rectangle 27 0

As of this writing, after spending many hours fighting with AWS EB's config, I gave up trying to make the static files work the way we all expect and updated my Flask app creation to:

app = Flask(__name__, static_url_path='/s')

This renders urls like /s/scripts/my-script.js and since I always use url_for('static', ...) in my code and templates, everything continued to work outside of AWS as well.

Update on 9/30/2013: I can pretty much guarantee that the staticFiles settings are completely ignored in AWS EB's Python container.

The change I suggested above has the undesirable downside of routing all static file requests through Flask (maybe, more accurately, WSGI.) That's not very hard to fix, though.

Alias /s /opt/python/current/app/static
<Directory /opt/python/current/app/static>
Order allow,deny
Allow from all
</Directory>

This config tells Apache to take over any requests for URLs starting with /s, same prefix we chose for our static files, and serve files from our app's static folder.

container_commands:
  add_apache_conf:
    command: "cp app-httpd.conf /etc/httpd/conf.d"

This file will be used during the app deployment and will copy the new .config file to a directory from which Apache is configure to load all .config files it sees.

.ebextensions/*.config
.elasticbeanstalk/optionsettings.*

The .elasticbeanstalk directory, as far as I understand, keeps local settings for your project. These settings are used by the eb command line tools, including when you run git aws.push. I personally stopped using these tools and wrote my own scripts using their API. In my case, for Python, I use the Boto package. The stuff in .ebextensions are included with your app when it's deployed or updated. It contains settings or commands that will be executed in your app servers upon deployment.

python - Static assets don't show up for flask on elastic beanstalk - ...

python amazon-web-services flask elastic-beanstalk
Rectangle 27 0

.ebextensions/<env-name>.config
option_settings:
  "aws:elasticbeanstalk:container:python":
    WSGIPath: myApp/handlers/views.py

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.

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?

.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.

This is actually outdated as of awsebcli 3.0. I added an answer detailing how it is done now.

python - How to deploy structured Flask app on AWS elastic beanstalk -...

python amazon-web-services flask elastic-beanstalk
Rectangle 27 0

It looks like your python runtime environment maybe different between python console and your app. Try to print out sys.path and compare the difference. sys.path is initialized to PYTHONPATH. So you may need to fix/append to this environment variable.

amazon web services - Why does import mechanize break my aws ec2 elast...

python amazon-web-services amazon-ec2 mechanize
Rectangle 27 0

Your WSGIPath refers to a file that does not exist.

Configuration > Software Configuration
WSGIPath
application.py

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

python - How to deploy structured Flask app on AWS elastic beanstalk -...

python amazon-web-services flask elastic-beanstalk
Rectangle 27 0

I managed to work around this issue by using Tornado (though I'm still not sure what the underlying issue is):

from sandman import app
from sandman.model import activate
import logging
import os, sys
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop

app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DB']

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    activate(browser=False)
    app.debug = True
    http_server = HTTPServer(WSGIContainer(app))
    http_server.listen(os.environ['PORT'])
    IOLoop.instance().start()

Exception running Python Flask app on AWS Elastic Beanstalk - Stack Ov...

python flask elastic-beanstalk
Rectangle 27 0

As a test I decided to rename my application.py file to app.py and create a new application.py file with the following contents:

from app import app as application

This fixed the issue! I can now access all of my endpoints correctly.

Unfortunately I still don't understand what is wrong with the original setup.

python - Flask 404 errors when deployed to AWS Elastic Beanstalk - Sta...

python flask elastic-beanstalk
Rectangle 27 0

A strange thing that I found to solve this issue was editing my .gitignore file. It had included removing the /dist folders, and that included the dist folders that my CSS was generated into. So the css files were actually missing when I deployed.

Hope this may help anyone who might be in the same boat.

python - Static assets don't show up for flask on elastic beanstalk - ...

python amazon-web-services flask elastic-beanstalk
Rectangle 27 0

I have had the same problem deploying a django app to EBS with pandas, and the issue was that there are certain C libraries that need to be installed first. Just add this in your .ebextensions folder:

commands:
  install_devtools:
    command: yum -y groupinstall 'Development tools'

Heads up, I believe that this will only work on sizes T2 small and above.

Thanks. I tried your suggestion. The deployment seems to have worked but my scripts errors out saying that it can't find pandas.

I would ssh into the server and check that pandas is installed correctly before running the file above. It still seems like a requirements.txt error to me, but at the moment it is impossible to tell. At the very least if you ssh in you can avoid constantly deploying to debug.

Thanks. I have tried that as well. I ended up (for now) installing my flask application and running it using python3 without apache or beanstalk for now.

python - Deploying Flask Application with pandas to Elastic Beanstalk ...

python amazon-web-services pandas flask elastic-beanstalk
Rectangle 27 0

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

  • 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)
  • 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.

python - How to deploy structured Flask app on AWS elastic beanstalk -...

python amazon-web-services flask elastic-beanstalk