Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS ECS leader commands (django migrate)

We are currently deploying our Django APP on AWS Elastic Beanstalk. There we execute the django db migrations using container commands, where we assure we only run migrations on one instance by using the "leader_only" restriction.

We are considering to move our deployment to AWS EC2 Container Service. However, we cannot figure out a way to enforce the migrate to only be run on one container when new image is being deployed.

Is it possible to configure leader_only commands in AWS EC2 Container Service?

like image 775
pjotr_dolphin Avatar asked Feb 17 '16 13:02

pjotr_dolphin


People also ask

Which command is used for migration in Django?

The Commands There are several commands which you will use to interact with migrations and Django's handling of database schema: migrate , which is responsible for applying and unapplying migrations. makemigrations , which is responsible for creating new migrations based on the changes you have made to your models.

How do I fix migration issues in Django?

you can either: temporarily remove your migration, execute python manage.py migrate, add again your migration and re-execute python manage.py migrate. Use this case if the migrations refer to different models and you want different migration files for each one of them.

What is Makemigrations and migrate in Django?

Makemigrations and migrate are commands that are used to interact with Django models. Makemigrations : This command prepares a makemigrations file for our new model, or creates a new migrations file for any changes if the models have been modified. This command does not create or affect these changes to the database.


2 Answers

There is a possibility to use ECS built-in functionality to handle deployments that involve migrations. Basically, the idea is the following:

  1. Make containers fail their health checks if they are running against an unmigrated database, e.g. via making a custom view and checking for the existence of the migrations execution plan executor.migration_plan(executor.loader.graph.leaf_nodes()) status = 503 if plan else 200
  2. Make a task definition that does nothing more then just migrates the database and make sure it is scheduled for execution with the rest of the deployment process.

The result is deployment process will try to bring one new container. This new container will fail health checks as long as database is not migrated and thus will block all the further deployment process (so you will still have old instances running to serve requests). Once migration is done - health check will now succeed, so the deployment will unblock and proceed.

This is by far the most elegant solution I was able to find in terms of running Django migrations on Amazon ECS.

Source: https://engineering.instawork.com/elegant-database-migrations-on-ecs-74f3487da99f

like image 200
Alex Pavlenko Avatar answered Oct 20 '22 16:10

Alex Pavlenko


For the ones using task definitions JSON all we need to do is flag a container as not essential on containerDefinitions

    {
      "name": "migrations",
      "image": "your-image-name",
      "essential": false,
      "cpu": 24,
      "memory": 200,
      "logConfiguration": {
        "logDriver": "awslogs",
        "options": {
          "awslogs-group": "your-logs-group",
          "awslogs-region": "your-region",
          "awslogs-stream-prefix": "your-log-prefix"
        }
      },
      "command": [
        "python3", "manage.py", "migrate"
      ],
      "environment": [
        {
          "name": "ENVIRON_NAME",
          "value": "${ENVIRON_NAME}"
        }
      ]
    }

I flagged this container as "essential": false.

"If the essential parameter of a container is marked as true, and that container fails or stops for any reason, all other containers that are part of the task are stopped. If the essential parameter of a container is marked as false, then its failure does not affect the rest of the containers in a task. If this parameter is omitted, a container is assumed to be essential."

source: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html

enter image description here

like image 1
Diego Magalhães Avatar answered Oct 20 '22 14:10

Diego Magalhães