Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django AWS Elastic Beanstalk migrate database

I'm deploying a Django project to AWS using Elastic Beanstalk and am stuck on migrating the database.

Where I'm at: I am able to successfully deploy my django project and load the page through mysubdomain.elasticbeanstalk.com. The page loads without error until I get to a page that needs to do a database call. I then get an error like relation "accounts_user" does not exist LINE 1: SELECT COUNT(*) FROM "accounts_user" because my database hasn't been migrated.

What I've tried: I've tried quite a few variations of things. Fortunately there are an abundance of stackoverflow posts and a couple tutorials. Unfortunately, they all seem to be using a different version and what they suggest do not apply to my project.

It is pretty clear to me that I need to run the migration in a foobar.config file inside the .ebextensions/ folder. Here is the base of what I want to do:

container_commands:
  01_migrate:
    command: "python manage.py migrate --noinput"
    leader_only: true

In the logs, I see that the post deployment script tried to run but it failed. I don't receive any other info on the error, the only thing I see is something like "ERROR: 01_migrate post deployment script failed"

I find out that I need to activate the virtual environment for the command, which makes sense. From asdf I try this:

container_commands:
  01_migrate:
    command: "source /opt/python/run/venv/bin/activate && python rlg/manage.py migrate --noinput"
    leader_only: true

But it doesn't work. In fact, through SSH I find out I don't even have a /opt/python/ folder, only /opt/aws/ and /opt/elasticbeanstalk/. All tutorials and SO questions refer to this folder but I don't have it?

VERSIONS: Python 3.4.1, Django 1.7.7, AWS CLI 3.2.1, Postgres 9.3

like image 589
awwester Avatar asked Apr 09 '15 13:04

awwester


2 Answers

I know this is an old post but I want to post my answer here as it took me quite a long time to figure out.

Sebastian kind of pointed me in the right direction but the problem with this approach is it runs before the deployment (so you migrate the old code)

You can also use the files command in ebextensions and write a file to /opt/elasticbeanstalk/hooks/appdeploy/post but this will run on ever instance

You can combine these two things into:

container_commands:
  01migrate:
    command: "mkdir -p /opt/elasticbeanstalk/hooks/appdeploy/post/ && echo -e '#!/bin/bash\ndocker exec `docker ps -a -q | head -n 1` python <path_to_code> migrate' > /opt/elasticbeanstalk/hooks/appdeploy/post/99_migrate.sh && chmod +x /opt/elasticbeanstalk/hooks/appdeploy/post/99_migrate.sh"
    leader_only: true

This will create a post deploy script in the correct dir and ONLY on the leader.

This is working really well for me but be warned, the hooks directories are un-documented features

like image 57
nkhumphreys Avatar answered Oct 20 '22 09:10

nkhumphreys


The container_commands are NOT executed within the docker container. They are executed on the ec2 instance directly. Currently I'm using docker exec to do the migration. As the concerned docker container is afaik the last one started I use docker ps -a --no-trunc -q | head -n 1 to get the container id.

In the end my setup.config looks like that

container_commands:
  01syncdb:
    command: "docker exec `docker ps -a --no-trunc -q | head -n 1` /var/app/bin/python /var/app/manage.py syncdb --noinput  &>> /tmp/deploy.log"
    leader_only: true
  02migrate:
    command: "docker exec `docker ps -a --no-trunc -q | head -n 1` /var/app/bin/python /var/app/manage.py migrate --noinput  &>> /tmp/deploy.log"
    leader_only: true

I hope that solves your problem as well.

like image 20
Sebastian Annies Avatar answered Oct 20 '22 09:10

Sebastian Annies