Container_commands fail to migrate django database?

If I remove the following:

    command: "source /var/app/venv/*/bin/activate && python migrate --no-input"
    leader_only: true

The app gets successfully deployed.

When I eb ssh and try to run python migrate I get the following error:

Traceback (most recent call last): File "", line 22, in main() File "", line 18, in main execute_from_command_line(sys.argv) File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/core/management/", line 446, in execute_from_command_line utility.execute() File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/core/management/", line 386, in execute settings.INSTALLED_APPS File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/conf/", line 87, in getattr self._setup(name) File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/conf/", line 74, in _setup self._wrapped = Settings(settings_module) File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/conf/", line 183, in init mod = importlib.import_module(self.SETTINGS_MODULE) File "/usr/lib64/python3.8/importlib/", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 1014, in _gcd_import File "", line 991, in _find_and_load File "", line 975, in _find_and_load_unlocked File "", line 671, in _load_unlocked File "", line 783, in exec_module File "", line 219, in _call_with_frames_removed File "/var/app/current/dca/dca/", line 104, in 'NAME': os.environ['RDS_DB_NAME'], File "/usr/lib64/python3.8/", line 675, in getitem raise KeyError(key) from None KeyError: 'RDS_DB_NAME'

I looked at a lot of the answers posted and have tried almost all the solutions given. I still get the same errors. I have been trying for 4 hours.

How could I resolve this?


Your error suggests that EB is having difficulties with finding your environment variables. During deployment, these environment variables are available. When ssh'ing into your instance however, you will have to import them manually. You can find the documentation here

One way to do this is to use the get-config tool, which is located at /opt/elasticbeanstalk/bin/get-config.

You can use the following command to print your environment variables:

/opt/elasticbeanstalk/bin/get-config environment

However, you still need to export them. This can be done with the following command:

export $(/opt/elasticbeanstalk/bin/get-config environment| xargs)

Now Django should be able to find the environment variables as set in your EB environment.

EDIT: Alternatively, you should also be able to export you environment variables via:

export $(cat /opt/elasticbeanstalk/deployment/env | xargs) 

try this container_commands: 10_migrate: command: |
source $PYTHONPATH/activate pipenv run python ./ migrate

This activates python environment that EB is using to install Pipfile dependencies before pipenv is executed.

Below is a version that will also load EB environmental variables that may be required to run the migrate job if you have database connection details passed as such.

container_commands: 10_migrate: command: | export $(cat /opt/elasticbeanstalk/deployment/env | xargs) source $PYTHONPATH/activate pipenv run python ./ migrate

Back to Top