AWS Elastic Beanstalk can not find static files for Django App
Asked Answered
A

2

5

I am having a little trouble setting up the static folders on elastic beanstalk for a django app I created. I manged to get the app to run without any of the css/js/etc. My file structure is:

|.ebextensions
|    --django.config
|.elasticbeanstalk
|    --config.yml
|django_app
|    --core
|    --blog
|    --other apps..
|config
|    --settings
|        --base.py
|        --local.py
|        --production.py
|    --asgi.py
|    --urls.py
|    --wsgi.py
|static
|    --blog
|        --css/js/etc
|    --other apps
|manage.py
|requirements.txt
|secrets.json

in my django.config I have it set up like so:

option_settings:
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: config.settings.production
  aws:elasticbeanstalk:container:python:
    WSGIPath: config.wsgi:application
    NumProcesses: 3
    NumThreads: 20

and for my static settings in base.py (production/local.py do import base.py):

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
STATIC_ROOT = "static"

I had a similar problem with flask but it didn't give me this much trouble. I have tried to follow some of the solutions that are on here but nothing has worked just yet or I am misinterpreting something. Any help is appreciated!

Antibiotic answered 9/6, 2020 at 0:10 Comment(0)
W
15

You have to add an additional option in your django.config option_settings, and it depends of the Elastic Beanstalk platform that you use.

I think that you are using the new platform platform/Python 3.7 running on 64bit Amazon Linux 2. If yes, use:

aws:elasticbeanstalk:environment:proxy:staticfiles:
    /static: static

But if you are using platform/Python 3.6 running on 64bit Amazon Linux or older, use:

aws:elasticbeanstalk:container:python:staticfiles:
    /static: static

You can check this link for more details.

If you don't know the platform you use, there are different ways to know it:

  • Through your terminal, it appears in the output of eb status command.
  • Through AWS console, it is mentioned in your Elastic Beanstalk environment page.
Warchaw answered 9/6, 2020 at 10:48 Comment(3)
That was exactly what I needed, thanks! It was a bit confusing with the different versions.Antibiotic
I was using platform/Python 3.7 running on 64bit Amazon Linux 2 and that resolves my issue. Thank you so much!Croner
I don't understand why this one is omited from the AWS elastic beanstalk documents, in the Deploying Djando tutorialVia
A
1

Just to post an update from @Yasser Mohsen's excellent response, in 2024, the correct syntax is described here:

option_settings:
  ...
  aws:elasticbeanstalk:environment:proxy:staticfiles:
    /static: static

Where the "/static" refers to what's set in yout STATIC_URL and the seconds "static" refers to the folder where static files are collected into, e.g. the contents of STATIC_ROOT:

In the example above, both are the same:

STATIC_URL = 'static/'
STATIC_ROOT = 'static/'
Arioso answered 23/9, 2024 at 22:5 Comment(2)
Your answer is the most concise I have come across. Thank you. The explanation of Static url vs static root in the option settings solved my issue.Aleishaalejandra
Glad it helped!Arioso

© 2022 - 2025 — McMap. All rights reserved.