5

I'm very curious as to how people run a command like db migrate on their eb apps. If you add it as an .ebextensions I fear that when you have multiple instances for one app, there could be conflicts that occur if multiple servers are trying to run the same migration.

At what point during the deployment process should a db migrate command be run?

ThomasReggi
  • 621
  • 2
  • 10
  • 25

3 Answers3

10

You can try the flag: leader_only in an ebextensions configuration file. This flag will make your migration run just once even if you have the app deployed in many instances. For example, save this file into the root directory of your project at the path: /.ebextensions/migration.config

option_settings:
container_commands:
    database_migration:
        leader_only: true
        command: "db migrate"

More information: AWS Elastic Beanstalk Official Docs

smottt
  • 111
  • 3
Tulio Faria
  • 213
  • 2
  • 5
1

As the OP didn't specify the platform, I thought I'd add a specific .ebextensions/migration.config config for Ruby on Rails, as I encountered this exact issue as well:

container_commands:
  12migrate:
    command: rake db:migrate
    leader_only: true
Jasper33
  • 111
  • 2
0

This sequence works for me, in deploying Rails 5.1.4 to Elastic Beanstalk using PostgreSQL:

  1. Deploy your code to EB. I use the EB console but you might use the CLI instead.
  2. SSH to the EC2 app server
  3. Change to the app directory: cd /var/app/current/
  4. db:reset

BTW, I needed to close pgAdminIII which I was using to examine the RDS database. Since I was connected, this locks the database and prevents the last step.

I also had RAILS_SKIP_MIGRATIONS set to TRUE in the EB console (under Configuration > Software). Presumably if you left this on its default value of FALSE, the deployment would run any migrations required.

So in answer to the original question, you run the rails db:reset (or rails db:migrate) on the app server. You don't (and can't) SSH to the RDS server.

MSC
  • 125
  • 7