How do I configure aws:elasticbeanstalk:container:python:staticfiles in Elastic Beanstalk?

Viewed 61

I'm trying to run eb create to deploy my Django project to AWS. The error I'm getting is ERROR: ServiceError - Configuration validation exception: Invalid option specification (Namespace: 'aws:elasticbeanstalk:container:python:staticfiles', OptionName: '/static/'): Unknown configuration setting. I'm unsure what this error means and there's not much I can find on it.

I've tried to define this variable in .ebextensions/django.config.

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: ebdjango.wsgi:application
  aws:elasticbeanstalk:container:python:staticfiles:
    /static/: 'frontend/views/build/static/'

My settings.py is configured with the following vars:

STATIC_URL = '/static/'
    
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'frontend/views/build/static')
]

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

I'm trying to run this ebs instance on Amazon Linux 2

2 Answers

If your environment uses a platform branch based on Amazon Linux 2, use the aws:elasticbeanstalk:environment:proxy:staticfiles namespace.

The following example configuration file tells the proxy server to serve files:

Example .ebextensions/django.config

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

Note:

  1. Your static files are collected in STATIC_ROOT path not in STATICFILES_DIRS

Ah yes, Amazon Linux 2 documentation are not entirely up to speed.

  1. update your django.config like so:
option_settings:
  aws:elasticbeanstalk:environment:proxy:staticfiles:
    /static: /path/to/static
  1. ensure that your static files are collected on deploy, to do so we can use hooks:

Create a postdeploy hook file called .platform/hooks/01_collect_static.sh and make it executable with chmod.

Enter the following content:

#!/bin/bash

source "$PYTHONPATH/activate" && {

  python manage.py collectstatic --noinput;

}

On deploy, django will now collect your static files into the folder you've defined in settings.py.

Note, I include my migrate command here as well, such that my hook file looks like:

#!/bin/bash

source "$PYTHONPATH/activate" && {  
    
  if [[ $EB_IS_COMMAND_LEADER == "true" ]];
  then 
    # log which migrations have already been applied
    python manage.py showmigrations;
    # migrate
    python manage.py migrate --noinput;
  else 
    echo "this instance is NOT the leader";
  fi
  python manage.py collectstatic --noinput;

}
Related