'aws eb deploy doesn't load environment variables
I'm deploying symfony project via eb deploy to ec2 instance (aws linux 2). Post deploy migrations scripts works well, but then I try to run symfony command with ec2-user I get an error about wrong database credentials. It is because I have pushed dump .env file with empty values. All my enviroment variables are stored in eb -> configuration -> Environment properties. How to make these variables to be visible to other users to properly execute commands?
I can see these variables as json with:
/opt/elasticbeanstalk/bin/get-config environment
Solution 1:[1]
The EB env variables on Amazon Linux 1 are stored in:
/opt/elasticbeanstalk/support/envvars
Thus, to load them when you login to the instance you can do the following:
source /opt/elasticbeanstalk/support/envvars
To check if they got loaded, you can just execute:
env
p.s.
For Amazon Linux 2:
export $(cat /opt/elasticbeanstalk/deployment/env | xargs)
Solution 2:[2]
I stumbled on the same issue, this documentation shed some light on how to fix permission issues in Amazon Linux 2 although it is not straightforward.
Solution 3:[3]
Something to note, is that the answer provided by @Marcin will fail if implemented in a deployment script within the .ebextensions or .platform folders on new instance deployments.
In order to properly retrieve the environment variables, and source them to the shell, AWS has an article outlining how this is done:
https://aws.amazon.com/premiumsupport/knowledge-center/elastic-beanstalk-env-variables-shell/
For future reference, here are the instructions copied from the AWS docs:
Resolution
- Create a .ebextension file in your application source bundle and include the following:
commands:
setvars:
command: /opt/elasticbeanstalk/bin/get-config environment | jq -r 'to_entries | .[] | "export \(.key)=\"\(.value)\""' > /etc/profile.d/sh.local
packages:
yum:
jq: []
Note: The configuration file in step 1 is called setvars.config.
Save the .ebextension file, and then deploy it to your Elastic Beanstalk environment.
To test if the variables are being exported, connect to your instance using SSH, and then run the following command. Before testing, close any existing sessions and then reconnect using SSH. env | grep VARIABLE_NAME Important: For step 3, set VARIABLE_NAME to a variable defined in your environment.
The output shows if your environment variables are set correctly. In the following example output, a variable called RDS_PORT is defined in the Elastic Beanstalk environment.
$ env|grep RDS_PORT
RDS_PORT=3306
Note: Because you're using the commands on the .ebextension, you can update the sh.local file only with a new deployment. If you add or change a variable in the environment, then you must create a new deployment before the variable is exported to the operating system.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | George Raphael |
Solution 3 | Zac Fair |