6

I'm trying to run some commands on my NodeJS app that need to be run via SSH (Sequelize seeding for instance), however when I do so, I noticed that the expected env vars were missing.

If I run eb printenv on my local machine I see the expected environment variables that were set in my EB Dashboard

If I SSH in, and run printenv, all of those variables I expect are missing.

So what happens, is when I run my seeds, I get an error:

node_modules/.bin/sequelize db:seed:all
ERROR: connect ECONNREFUSED 127.0.0.1:3306

I noticed that the port was wrong, it should be 5432. I checked to see if my environment variables were set with printenv and they are not there. This leads me to suspect that the proper env variables are not loaded in my ssh session, and NodeJS is falling back to the default values I provided in my config.

I found some solutions online, like running the following to load the env variables:

/opt/python/current/env

But the python directory doesn't exist. All that's inside /opt/ is elasticbeanstalk and aws directories.

I had some success so I could at least see that the env variables exist somewhere on the server by running the following:

sudo /opt/elasticbeanstalk/bin/get-config environment --output YAML

But simply running this command does not fix the problem. All it does is output the expected env variables to the screen. That's something! At least I know they are definitely there! But the env variables are still not there when I run printenv

So how do I fix this problem? Sequelize and NodeJS are clearly not seeing the env variables either, and it's trying to use the fallback default values that are set in my config file.

Erick Maynard
  • 731
  • 6
  • 18
  • [This answer](https://stackoverflow.com/a/65059905/8090336) helps in loading the environment variables in SSH session. Sharing it here as this is the first question listed in google for this issue. – gowthz Feb 23 '23 at 14:42

1 Answers1

0

I know my answer is late, but I had the same problem and after some attempts with bash script I found a way to store it in your env vars. you can simply run the following command:

export env=`/opt/elasticbeanstalk/bin/get-config environment -k <your-variable-name>`

now you will be able to easily access this variable:

echo $your-variable-name

afterward, you can utilize the env var to do what ever you like. in my case, I use it to decide which version of my code to build in a file called build-script.sh and its content is as follows:

# get env variable to know in which environment this code is running in
export env=`/opt/elasticbeanstalk/bin/get-config environment -k environment`

# building the code based on the current environment
      if [ $env = "production" ]
      then
              echo "building for production"
              npm --prefix /var/app/current run build-prod
      else
              echo "building for non production"
              npm --prefix /var/app/current run build-prod
      fi

hope this helps anyone facing the same issue