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:
"aws:elasticbeanstalk:container:python:staticfiles":
"/static/": "static/"
All of my calls to assets in templates are wrapped in "{{url_for('static', filename='img/office.jpg')}}" type things.
But no images, styles or javascript shows up on page load. Here is an example log file 404. IP - - [25/Feb/2013:21:48:13 +0000] "GET /static/css/bootstrap.css HTTP/1.1" 404 328 "http://xyz.elasticbeanstalk.com/"
Am I missing something obvious? Everything runs great on my local, just static assets dont load once i git aws.push
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:
This renders urls like
/s/scripts/my-script.js
and since I always useurl_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:
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:
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.
It can be done also through Elastic Beanstalk Panel:
Configuration -> Software Configuration - > Static Files
and then
just as an alternative option
I had a similar problem where, strangely enough, all files from
static/img/
were being served, but anything instatic/css/
orstatic/js/
were simply not being served.To fix this I opened
.elasticbeanstalk/optionsettings.app-env
from the root directory of the Flask source and edited this block of code to beNote that
StaticFiles
actually takes a regular expression, so in/static/.*
the.*
allows for any path after/static/
4+ years later, I'm able to get static files working using:
(file:
.ebextensions/WHATEVER_NAME.config
)...in my case, this was
I found that changing my
to
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>
I'm not sure where you put your static files, but since mine are typically in
app/static
, I have:Both sections seem to be important. (The latter section has other stuff in it too but I've left that out.) I actually have two resource paths here, both the
/static/
directory and the top-level/favicon.ico
file that dumb old browsers always ask for.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.