Search code examples
pythondjangoamazon-web-servicesstaticamazon-elastic-beanstalk

Unable to render static files in my Aws elasticbeanstalk after pushing from Cloud9 for my python web application


I have been unable to render my static files in AWS EB for my python application.

it renders well on Cloud9 however when I deploy to AWS ElasticBeanstalk, it does not render the files.

I have static folder in my base dir and I have referenced this way in my settings.py

STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = 'barterproj/static' #"barterproj" is the name of my main app in my base directory. it has the settings.py

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

Cloud9 reads the static files

Cloud9

When deployed to ElasticBeanstalk

EB


Solution

  • You need to configure beanstalk before serving static files. Read https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/environment-cfg-staticfiles.html

    Sample code that might work for you

    option_settings:
      aws:elasticbeanstalk:container:python:
        WSGIPath: mysite.wsgi:application
      aws:elasticbeanstalk:environment:proxy:staticfiles:
        /static: static
    container_commands:
      01_collectstatic:
        command: "source /var/app/venv/*/bin/activate && python manage.py collectstatic --noinput"