Cli: AWS Elastic Beanstalk Deployment with migrations

Created on 30 Jan 2017  路  10Comments  路  Source: sequelize/cli

I'm looking for a way to use sequelize-cli on an elastic beanstalk deployment strategie. However right now I do not get it working. It seems right now that the config is missing. Not sure if the problem results from:

  • missing .env vars
  • Sequelize / config not reading dot env vars correctly

What I got so far:

  1. make npm available
#.ebextensions/00_npm.conf
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-inst
all/node-* | head -1`/bin/npm /bin/npm"
  1. trigger miggrations
#.ebextensions/10_sequelize_migrate.conf
container_commands:
  10_sequelize_migrate:
    command: npm run migrate
    leader_only: true
  1. packacke json
{
  ...,
  "scripts": {
    "start": "node index.js",
    "development": "nodemon ./server.js --exec babel-node",
  }
}

config/config.js

module exports = {
  ....,
  "production": {
     "username": process.env.RDS_USERNAME || null,
     "password": process.env.RDS_PASSWORD ||聽null,
     "host"    : process.env.RDS_HOSTNAME ||聽null,
     "port"    : process.env.RDS_PORT || null,
     "database": process.env.RDS_DATABASE || null,
   }
}

Error:

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

As far as I know there is no .env file on amazon. It sets env vars.

Similar related questions

question stale

Most helpful comment

for future reference, this works :)

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"
10_db_migrate:
    command: ./node_modules/.bin/sequelize db:migrate
    leader_only: true
11_db_seed:
    command: ./node_modules/.bin/sequelize db:seed:all
    leader_only: true

All 10 comments

You're going to need a stacktrace. I wasn't able to find code in sequelize-cli or umzug which reads a .env file.

The dotenv is read if environment is not set to production. I figured this out.

However I'm still not able to get sequelize read the env vars from elastic beanstalk :/

How are you passing in env vars? I don't use elastic beanstalk but from the screenshots on this SO question, it appears that there are set names for environment variables (PARAM1, PARAM2, etc.).

http://stackoverflow.com/questions/30308198/node-elastic-beanstalk-set-environment-node-env-prod

This official documentation also mentions those same env key names: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/create_deploy_nodejs.container.html

I'm using it within container commands and as is know it seems that env vars are not available there by default and I have not yet had time to figure out how to access env vars in awe eb container commands

@manuelfink were you able to figure this out? Documentation around EB's container commands is very poor unfortunately 馃槥

I guess the .env error is resulting from me reading the .env file with require('dotenv') in the config for the development environment.

if (process.env.NODE_ENV != "production") require("dotenv");

since the env is missing because environment variables are missing, this causes an error.

solution

@wwalser I'm setting env vars within the software configuration. I believe that commiting .env into project is bad practice!

Env vars set within elastic beanstalks software configuration are note available within the container command by default.

I guess the glue is make the env vars available. According to amazon support env vars are available in the /home/ec2-user/ folder and they recommend running a script there, as described in the stackoverflow question http://stackoverflow.com/questions/29423608/accessing-environment-variables-in-aws-beanstalk-ebextensions:

files:
  "/home/ec2-user/setup.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/bin/bash

      # Commands that will be run on containter_commmands
      # Here the container variables will be visible as environment variables.

container_commands:
  set_up:
    command: /home/ec2-user/setup.sh

However I have not had time to test this and write a script running migrations.

For future reference, here's how I made it work:

.ebextensions/00_env.config

container_commands:
  10_db_migrate:
    command: ./node_modules/.bin/sequelize db:migrate
    leader_only: true

The main change is to call the sequelize script directly instead of npm run .... I don't know why using npm hides all environment variables from the sequelize script. It would be nice to understand why that happens.

@mdebbar I trying with your code but i get this error

Application update failed at 2019-01-10T15:54:36Z with exit status 127 and error: container_command 10_db_migrate in .ebextensions/03-migrate.config failed.

/bin/sh: ./node_modules/.bin/sequelize: No such file or directory.

this are my migrate code
_.ebextensions/03-migrate.config_

container_commands:
  10_db_migrate:
    command: ./node_modules/.bin/sequelize db:migrate
    leader_only: true
  11_db_seed:
    command: ./node_modules/.bin/sequelize db:seed:all
    leader_only: true

UPDATE

My mistake, sequelize-cli was at dev-development.

but now I get this error

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

for future reference, this works :)

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"
10_db_migrate:
    command: ./node_modules/.bin/sequelize db:migrate
    leader_only: true
11_db_seed:
    command: ./node_modules/.bin/sequelize db:seed:all
    leader_only: true

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shomanishikawa picture shomanishikawa  路  3Comments

eharoldreyes picture eharoldreyes  路  3Comments

rmoura-92 picture rmoura-92  路  4Comments

LucasBadico picture LucasBadico  路  5Comments

TomerRon picture TomerRon  路  3Comments