[Fixed]-Deploying Django to Elastic Beanstalk, migrations failed

33👍

Edit 11/1/2022 – I posted this roughly same answer in two places that had the same question. Another user left an answer which appears to be more up-to-date, though I have not tested it. You can find that answer here: https://stackoverflow.com/a/65199647/5180047


Original Answer 👇

I had this same issue, using Amazon Linux 2 with Python 3.7 just like OP. AWS has not updated their documentation to support the platform changes which made this very confusing. I found a fix, though I don’t love it, but here is my solution:

TLDR – here’s my solution. For how I got there, I’ve pasted in my notes as I debugged:

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

Even though I don’t love it, I have verified with AWS Support that this is in fact the recommended way to do this. You must source the python environment, as with AL2 they use virtual environments in an effort to stay more consistent.


From the AWS Announcement of Amazon Linux 2 in December I found that the installed python environment is stored in /var/app/venv/*/..

2020-07-22 19:20:41,376 [ERROR] -----------------------BUILD FAILED!------------------------
2020-07-22 19:20:41,376 [ERROR] Unhandled exception during build: Command 01_migrate failed
Traceback (most recent call last):
  File "/opt/aws/bin/cfn-init", line 171, in <module>
    worklog.build(metadata, configSets)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 129, in build
    Contractor(metadata).build(configSets, self)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 530, in build
    self.run_config(config, worklog)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 542, in run_config
    CloudFormationCarpenter(config, self._auth_config).build(worklog)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/construction.py", line 260, in build
    changes['commands'] = CommandTool().apply(self._config.commands)
  File "/usr/lib/python2.7/site-packages/cfnbootstrap/command_tool.py", line 117, in apply
    raise ToolError(u"Command %s failed" % name)
ToolError: Command 01_migrate failed

I found a log file that actually tells me more useful information: /var/log/cfn-init-cmd.log

which gives me this error:

2020-07-22 21:08:32,757 P1620 [INFO] ============================================================
2020-07-22 21:08:32,757 P1620 [INFO] Command 01_migrate
2020-07-22 21:08:32,771 P1620 [INFO] -----------------------Command Output-----------------------
2020-07-22 21:08:32,772 P1620 [INFO]      File "manage.py", line 17
2020-07-22 21:08:32,772 P1620 [INFO]        ) from exc
2020-07-22 21:08:32,772 P1620 [INFO]             ^
2020-07-22 21:08:32,772 P1620 [INFO]    SyntaxError: invalid syntax
2020-07-22 21:08:32,772 P1620 [INFO] ------------------------------------------------------------
2020-07-22 21:08:32,772 P1620 [ERROR] Exited with error code 1

which is weird. This likely means I’m running the wrong python version. So I need to modify my command probably:
so… the things I’ve tried are:

  1. python manage.py migrate
  2. python3 manage.py migrate

That gave me a better error message

2020-07-23 14:47:06,906 P14080 [INFO] ============================================================
2020-07-23 14:47:06,906 P14080 [INFO] Command 01_migrate
2020-07-23 14:47:06,936 P14080 [INFO] -----------------------Command Output-----------------------
2020-07-23 14:47:06,936 P14080 [INFO]   Traceback (most recent call last):
2020-07-23 14:47:06,936 P14080 [INFO]     File "manage.py", line 11, in main
2020-07-23 14:47:06,936 P14080 [INFO]       from django.core.management import execute_from_command_line
2020-07-23 14:47:06,936 P14080 [INFO]   ModuleNotFoundError: No module named 'django'
2020-07-23 14:47:06,937 P14080 [INFO]   
2020-07-23 14:47:06,937 P14080 [INFO]   The above exception was the direct cause of the following exception:
2020-07-23 14:47:06,937 P14080 [INFO]   
2020-07-23 14:47:06,937 P14080 [INFO]   Traceback (most recent call last):
2020-07-23 14:47:06,937 P14080 [INFO]     File "manage.py", line 28, in <module>
2020-07-23 14:47:06,937 P14080 [INFO]       main()
2020-07-23 14:47:06,937 P14080 [INFO]     File "manage.py", line 17, in main
2020-07-23 14:47:06,937 P14080 [INFO]       ) from exc
2020-07-23 14:47:06,937 P14080 [INFO]   ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
2020-07-23 14:47:06,937 P14080 [INFO] ------------------------------------------------------------
2020-07-23 14:47:06,937 P14080 [ERROR] Exited with error code 1

but it seems the python libraries aren’t installed when I run with python3, so it’s probably referencing the system python3 installation and not the virtual environment.

Little rant I left on this issue here: Python on Amazon Linux 2 platform · Issue #15 · aws/elastic-beanstalk-roadmap · GitHub which is due to.

  1. 💥 source /var/app/venv/*/bin/activate && python3 manage.py migrate💥
    I found the environment on the EC2 instance and manually sourced it to force it to use the correct python version. This appears to have solved the python environment issue. Now RDS seems to not have the database, but this seems much more fixable.
2020-07-23 15:26:32,016 P14702 [INFO] ============================================================
2020-07-23 15:26:32,016 P14702 [INFO] Command 01_migrate
2020-07-23 15:26:32,426 P14702 [INFO] -----------------------Command Output-----------------------
2020-07-23 15:26:32,427 P14702 [INFO]   Traceback (most recent call last):
2020-07-23 15:26:32,427 P14702 [INFO]     File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
2020-07-23 15:26:32,427 P14702 [INFO]       self.connect()
2020-07-23 15:26:32,427 P14702 [INFO]     File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
2020-07-23 15:26:32,427 P14702 [INFO]       return func(*args, **kwargs)
2020-07-23 15:26:32,427 P14702 [INFO]     File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/db/backends/base/base.py", line 197, in connect
2020-07-23 15:26:32,427 P14702 [INFO]       self.connection = self.get_new_connection(conn_params)
2020-07-23 15:26:32,427 P14702 [INFO]     File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
2020-07-23 15:26:32,427 P14702 [INFO]       return func(*args, **kwargs)
2020-07-23 15:26:32,427 P14702 [INFO]     File "/var/app/venv/staging-LQM1lest/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
2020-07-23 15:26:32,427 P14702 [INFO]       connection = Database.connect(**conn_params)
2020-07-23 15:26:32,428 P14702 [INFO]     File "/var/app/venv/staging-LQM1lest/lib64/python3.7/site-packages/psycopg2/__init__.py", line 127, in connect
2020-07-23 15:26:32,428 P14702 [INFO]       conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
2020-07-23 15:26:32,428 P14702 [INFO]   psycopg2.OperationalError: FATAL:  database "ebdb" does not exist

for me, at this point I knew the migrate command had succeeded, so the solution is just:

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

Of course, I had to connect to the RDS instance and create the database as well, but it worked from here.

Leave a comment