1
votes

I'm trying to get Sequelize migrations to run on each deploy to Elastic Beanstalk.

I'm following the advice of the documentation, of other answers on Stack Overflow, and even a different project I've worked on in the past, and am using a .config file in my /.ebextensions folder:

container_commands:
    00_node_binary:
        command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/node /bin/node"
    01_npm_binary:
        command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/npm /bin/npm"
    03_db_migrate:
        command: ./node_modules/.bin/sequelize db:migrate
        leader_only: true

When I try to run it, I get this error:

ERROR   [Instance: i-06991b5ec3283038a] Command failed on instance. Return code: 127 Output: /bin/sh: ./node_modules/.bin/sequelize: No such file or directory.

What else do I need to do (either in .extensions, in .elasticbeanstalk file, or in EB's Software Configuration settings) so EB can find this module and run this command?

Note: The ls -td commands above produce this result in my previous project:

/opt/elasticbeanstalk/node-install/node-v6.9.1-linux-x64

And this result in the current project I'm having trouble with:

/opt/elasticbeanstalk/node-install/node-v10.17.0-linux-x64

3

3 Answers

1
votes

I resolve this error by running the command in .ebextension/config_file.sh

files:

"/opt/elasticbeanstalk/hooks/appdeploy/pre/config_file.sh":

mode: "000755"
owner: root
group: root
content: |
  #!/bin/bash
  
  curl --silent --location https://rpm.nodesource.com/setup_12.x | sudo bash -
  
  sudo yum -y install nodejs

then

run command on terminal in your app directory:

./node_modules/.bin/sequelize db:migrate

its works for me!!

0
votes

I had the same exact issue and after many hours of debugging various logs I found that this was the log I needed to actually look at to isolate the real issue with sequelize migrations. The container_commands script from the original question is actually correct and the error lies elsewhere in your app. (At least in my case)

Assuming you've already configured Elastic Beanstalk with .elasticbeanstalk and .ebextensions folders where .ebextensions folder contains a file named something like 01_run_migrations.config that holds these container_commmands below:

container_commands:
    00_node_binary:
        command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/node /bin/node"
    01_npm_binary:
        command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/npm /bin/npm"
    03_db_migrate:
        command: ./node_modules/.bin/sequelize db:migrate
        leader_only: true

Debugging Logs

If your deploy fails after running eb deploy try using eb ssh from within your project folder to debug the various server logs. Here are two logs that were helpful for me to take a look at once ssh'd into the environment.

cat /var/log/cfn-init.log and

cat /var/log/cfn-init-cmd.log

The second one led me to the exact stack trace source of the error which turned out to be a misconfiguration of my sequelize config.js file. I'm using Postgres and had incorrectly setup my production config.

Take a look at Sequelize config.js

If you were getting this error for the same reason I was for configuring Postgres, then it's due to a bad configuration of your sequelize config.js file for your production environment.

Did NOT Work

{

  database: process.env.RDS_DB_NAME,
  username: process.env.RDS_USERNAME,
  password: process.env.RDS_PASSWORD,
  host: process.env.RDS_HOSTNAME,
  logging: false,
  dialect: "postgres",
  dialectModule: require("pg"),
  port: process.env.RDS_PORT,
  maxConcurrentQueries: 100,
  dialectOptions: {
       ssl: "Amazon RDS",
       multipleStatements: true
  },
  pool: { maxConnections: 5, maxIdleTime: 30 },
  language: "en",
}

Did Work

{
 database: process.env.RDS_DB_NAME,
 username: process.env.RDS_USERNAME,
 password: process.env.RDS_PASSWORD,
 host: process.env.RDS_HOSTNAME,
 logging: false,
 dialect: "postgres",
 port: process.env.RDS_PORT,
 maxConcurrentQueries: 100,
 ssl: "Amazon RDS",
 pool: { maxConnections: 5, maxIdleTime: 30 },
 language: "en",
}

Although, I don't know if this will solve your exact problem, I hope sharing this will help others more easily isolate and debug issues with Elastic Beanstalk container_commands. Oh, I should also note that running eb logs wasn't exactly helpful in my case.

0
votes

I solved this manually (which isn't going to be the best for production deployments, but suffices for testing) by installing nodejs, npm, and sequelize on the elastic beanstalk server (via SSH), as sequelize wasn't installed in my project for some odd reason.

Step 1) In your local environment, compose a config_file.sh inside the .ebextensions folder with the following contents.

mode: "000755"
owner: root
group: root
content: |
  #!/bin/bash
  
  curl --silent --location https://rpm.nodesource.com/setup_12.x | sudo bash -
  
  sudo yum -y install nodejs

Step 2) Run eb deploy
Step 3) Ssh into the elastic beanstalk server
Step 4) Confirm that node is installed by running node -v
Step 5) Go into the app directory, cd /var/app/current
Step 6) Install sequelize via npm i sequelize
Step 7) Finally, run your migrations, ./node_modules/.bin/sequelize db:migrate