2

How can I run sequelize db:migrate on ElasticBeanstalk with env vars?

Running sequelize migrate fails since it cannot find the .env file.

{ Error: ENOENT: no such file or directory, open '.env'

my master.config looks like:

container_commands:
  00_node_binary:
    command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/node /bin/node"
  00_npm_binary:
      command: "ln -sf `ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin/npm /bin/npm"
  01_migrations:
    command: npm run migrate
    leader_only: true

and my package.json contains

"migrate": "node_modules/sequelize-cli/bin/sequelize db:migrate"
Manuel
  • 9,112
  • 13
  • 70
  • 110

4 Answers4

2

Edit:

I just found out what's going on with environment variables. Try running the migration script without npm. It'll be something like:

./node_modules/.bin/sequelize db:migrate

This way, you'll get all the environment variables as you expect.

Old answer:

Are you sure your .env file is committed to your git repo? In general, it's not a good idea to commit a .env to git and use it in production. You should instead set environment variables in your Elastic Beanstalk dashboard under Software Configuration.

You could also use the eb command line utility as documented here.

Mouad Debbar
  • 3,126
  • 2
  • 20
  • 20
  • Thats exactly the issue. The .env file is not commited and not supposed to be committed. However the env vars set in software configuration are not available in container commands. – Manuel Feb 27 '17 at 09:12
  • Yeah I've been fighting with container commands this weekend. I tried all the suggestions but nothing worked. Apparently, the only way to get env variables is to manually invoke `/opt/elasticbeanstalk/bin/get-config environment` which returns a JSON containing all env variables. – Mouad Debbar Feb 27 '17 at 18:17
  • Actually, an idea just came to mind, but I don't have my personal laptop to try it. Maybe there is an Elastic Beanstalk hook that exports environment variables, and all we need to do is put our container commands after it (in alphabetical order)? – Mouad Debbar Feb 27 '17 at 18:19
  • Good idea, that could work. Than just export them to bash that they are available to node. I read about running it as script within /home/ec2-user should do the hack since env vars should be available there. But have not tried yet. – Manuel Feb 27 '17 at 18:24
  • @Manuel were you able to get this to work in /home/ec2-user? I can't seem to grab the sequelize bin from there or are you running the npm run script from there? – daxiang28 May 31 '17 at 16:57
  • Have not had time to test yet :/ – Manuel May 31 '17 at 19:51
  • @daxiang28 the sequelize binary is not located in `/home/ec2-user`. You can find it in the app folder: `/var/app/current`. – Mouad Debbar Jun 01 '17 at 04:30
  • 1
    During the deployment, your app will be temporarily located in a "staging" dir. You can run `sudo /opt/elasticbeanstalk/bin/get-config container -k app_staging_dir` which will give you the staging location. – Mouad Debbar Jun 01 '17 at 04:33
  • Thanks @MouadDebbar. I think my problem is that I'm not using the preconfigured Node deployment and using the official one from Node. It seems like the volumes are exposed a little differently there. – daxiang28 Jun 01 '17 at 18:24
2

Don't forget to include the first two commands, the file migration.config that worked for me in .ebextensions looks like this

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

It looks like the ./node_modules/.bin/sequelize uses /usr/bin/env/node and will give you the following error:

/usr/bin/env: node: No such file or directory

Because apparently node is called nodejs... the first two container commands will take care of that.

See https://github.com/nodejs/node-v0.x-archive/issues/3911 for further reference

Fabio Espinosa
  • 880
  • 6
  • 14
0

I resolve this error through running command in .ebextension/config_file.sh node vsersion is same which is using in the EB console

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 work for me!!

Dani
  • 109
  • 1
  • 12
0

None of the solutions worked for me. What worked was lazartravica's answer on the following page:

https://github.com/sequelize/sequelize/issues/12913#issuecomment-782254695

TLDR:
Don't run npm install in the Deploy step, this is both a security and a performance issue. Run node_modules/sequelize-cli/lib/sequelize db:migrate in the Build step to fix the issue.

lejlun
  • 4,140
  • 2
  • 15
  • 31