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

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


It can be done also through Elastic Beanstalk Panel:

Configuration -> Software Configuration - > Static Files

and then

enter image description here

just as an alternative option


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.

Create an Apache conig file at your project root, named app-httpd.conf:

Alias /s /opt/python/current/app/static<Directory /opt/python/current/app/static>Order allow,denyAllow 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.

Create this file at .ebextensions/custom-apache.config:

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.


4+ years later, I'm able to get static files working using:

(file: .ebextensions/WHATEVER_NAME.config)

option_settings:  - namespace: aws:elasticbeanstalk:container:python    option_name: StaticFiles    value: /static/=PATH/FROM/MY/APP/BASE/DIR/TO/STATIC/DIR/

...in my case, this was

    value: /static/=distrib/static/

I found that changing my

app = Flask(__name__)

to

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

was neither necessary nor sufficient. When I only set static_url_path but not StaticFiles, it didn't work; when I set StaticFiles but not static_url_path, it worked fine.

<sarcasm>Elastic Beanstalk is super straightforward and well documented!</sarcasm>