All in all it was pretty simple to get my flask app up and running on Elastic Beanstalk. I ran into a couple of gotchas, and wanted to share them to save others some time.
I relied heavily on two tutorials to get going my first app up:
Three main gotchas where (see below for how to get solve / work around):
- As of June 2013, EB only supports python 2.6 (2.6.8 to be specific)
- Make sure your container is configured to point to your static files
- Make sure your Flask app is named application
Somethings that I am impressed with (again only been playing with this a day):
- Fundamentally, easy to get up and running
- Autoscaling seems to be pretty slick
- Besides the python for the web server, the instances are highly configurable (docs)
- in particular I like the configurable services that you can attach to each instance
Things that I am currently struggling with (but again its only been a day) are:
- Can not specify python version for their WSGI
- Not much of a community (or at least I can’t find the community) around EB. And so …
- Forums and StackOverflow are not manned by Amazon employees, and in general is not active
- Documentation is pretty poor
- Few documented patterns or best practices
- Logging is not great (more below)
- No console concept (
eb run python
—> and get an interactive python console)
Currently Beanstalk only supports python 2.6
For now the only real option is to make sure your web app runs on 2.6. I do not think thats critical, and I am sure in time they will upgrade to 2.7 and gasp maybe 3.0. But if you need to run a higher version — you will need to look to another solution (ec2 or heroku).
To get your localenv to run 2.6, make a virtualenv like so …
virtualenv -p /usr/bin/python2.6 your_project_name
And you will need to keep another session open with 2.7 or greater to work with the
eb
cli (this still makes be laugh)
Configure your container to point to your static files:
If you do not do this, all of your css, javascript and all your other static goodness will bot be served.
- Create a
.ebextensions
folder in your project root - Create a [project_name].config file in that .ebextensions folder
Mine looks like the following:
option_settings: "aws:elasticbeanstalk:container:python:staticfiles": "/static/": "static/"
You can read more about configuration here
Make sure your flask app (or another WSGI app is named application)
EB WSGI looks for an application
object inside an application.py
file in your project root. Mine looks like:
1 2 3 4 5 6 7 8 9 |
|
Other tips
Using brew to install the eb cli
If you are on a mac, you can use brew to install the elastic beanstalk cli (ironically you need to have python 2.7 or 3.x installed on your machine)
brew install aws-elasticbeanstalk
To Check out the logs
You have a couple of options to check out the logs:
- using the CLI,
eb logs
- going to the web console, navigate to logs, and hit ‘Snapshot logs’ (docs)
I found the that formatting of eb logs
in the console to be pretty unreadable. The code is found here (couldn’t find it on github). If I find I rely on this a lot I will probably rewrite this. I think the main issue is three fold:
- They probably are giving us too much info. They provide us separate logs from the following sources:
- /opt/python/log/httpd.out
- /var/log/httpd/error_log
- /var/log/cfn-hup.log
- /opt/python/log/supervisord.log
- /var/log/eb-tools.log
- /var/log/httpd/access_log
- /var/log/eb-cfn-init-call.log
- /var/log/eb-publish-logs.log
- /var/log/cfn-init.log
- /var/log/eb-cfn-init.log
- Each log is presented in a different format (I know why thats the case — but still)
And I think they are simply running
tail -n
on each each of these files. So you get the last n rows for each file, presented separately. What I really want to know is what just happened or even better — what is happening right now (tailing). AND to have all of these logs files interlaced and prefaced by its source. Something like:[timestamp] [source=error_log] message 1 [timestamp] [source=error_log] message 2 [timestamp] [source=access_log] message 3