Sid
Sid

Reputation: 4055

container_commands fail to migrate Django database

If I remove the following:

container_commands:
  01_migrate:
    command: "source /var/app/venv/*/bin/activate && python manage.py migrate --no-input"
    leader_only: true

The app gets successfully deployed.

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

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/core/management/__init__.py", line 386, in execute
    settings.INSTALLED_APPS
  File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/conf/__init__.py", line 87, in __getattr__
    self._setup(name)
  File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/conf/__init__.py", line 74, in _setup
    self._wrapped = Settings(settings_module)
  File "/var/app/venv/staging-LQM1lest/lib/python3.8/site-packages/django/conf/__init__.py", line 183, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/lib64/python3.8/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/var/app/current/dca/dca/settings.py", line 104, in <module>
    'NAME': os.environ['RDS_DB_NAME'],
  File "/usr/lib64/python3.8/os.py", 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.

How could I resolve this?

Upvotes: 0

Views: 275

Answers (1)

WoodyG
WoodyG

Reputation: 200

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.

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

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

Upvotes: 1

Related Questions