Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:
  "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

like image 316
user1609682 Avatar asked Feb 25 '13 22:02

user1609682


People also ask

Can I serve static files on Elastic Beanstalk?

Elastic Beanstalk supports configuring the proxy to serve static files on most platform branches based on Amazon Linux 2. The one exception is Docker. On the Python and Ruby platforms, Elastic Beanstalk configures some static file folders by default. For details, see the static file configuration sections for Python and Ruby.

What is flask?

Flask is an open source web application framework for Python. This tutorial walks you through the process of generating a Flask application and deploying it to an AWS Elastic Beanstalk environment. In this tutorial, you’ll do the following:

How can I find my application's code using flask?

Using application.py as the filename and providing a callable application object (the Flask object, in this case) allows Elastic Beanstalk to easily find your application's code. Run application.py with Python:

What happens when I finish working with Elastic Beanstalk?

When you finish working with Elastic Beanstalk, you can terminate your environment. Elastic Beanstalk terminates all AWS resources associated with your environment, such as Amazon EC2 instances , database instances , load balancers , security groups, and alarms .


1 Answers

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>

like image 114
Ben Wheeler Avatar answered Oct 30 '22 15:10

Ben Wheeler