Skip to content Skip to sidebar Skip to footer

Static Assets Don't Show Up For Flask On Elastic Beanstalk

How do you get aws elastic beanstalk to recognize your static assets in your flask app? I did the standard /.ebextensions/python.config couple of YAML lines a la: option_settings:

Solution 1:

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

Solution 2:

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,deny
Allow fromall</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.

Solution 3:

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>

Solution 4:

I spent a goodly amount of time trying to figure this out and sort through these suggestions. I've made comments on the answers that were still relevant in 2020. Here's the TL;DR of the solution I encountered:

  • The static files section in the Modify Software screen under the Configuration left navigation pane on the Elastic Beanstalk UI sets the property aws:elasticbeanstalk:container:python:staticfiles. This configuration does not map to the static_url_path which Flask needs to find the static assets.
  • The correct property to set is StaticFiles, which can be set by adding a .config file to the .ebextensions directory.

Read more here.

Solution 5:

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.

Post a Comment for "Static Assets Don't Show Up For Flask On Elastic Beanstalk"