I'm using elastic beanstalk
to deploy a Django
app. I'd like to SSH
on the EC2
instance to execute some shell commands but the environment variables don't seem to be there. I specified them via the AWS GUI (configuration -> environment properties) and they seem to work during the boot-up of my app.
I tried activating and deactivating the virtual env via:
source /var/app/venv/*/bin/activate
Is there some environment (or script I can run) to access an environment with all the properties set? Otherwise, I'm hardly able to run any command like python3 manage.py ...
since there is no settings module configured (I know how to specify it manually but my app needs around 7 variables to work).
Environment properties are written to the /opt/python/current/env file, which is sourced into the virtualenv stack where the application runs. For more information, see Using the Elastic Beanstalk Python platform.
On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.
Choose one of the following ways to pass your environment variables. Using the Elastic Beanstalk console: 1. Open the Elastic Beanstalk console. 2. Select your application, and then choose Configuration from the navigation pane. 3.
Open the Elastic Beanstalk console , and in the Regions list, select your AWS Region. In the navigation pane, choose Applications, and then choose an existing application's name in the list or create one . On the application overview page, choose Create a new environment . Choose the Web server environment or Worker environment environment tier.
1. Open the Elastic Beanstalk console. 2. Select your application, and then choose Configuration from the navigation pane. 3. For the Software category, choose Modify. 4. In the Environment properties section, enter the key-value pairs for the environment properties that you want to pass to your instances. 5.
Important: To pass confidential information (such as a database password) to the instance, follow the instructions at Storing private keys securely in Amazon S3. Choose one of the following ways to pass your environment variables. Using the Elastic Beanstalk console:
During deployment, the environment properties are readily available to your .platform
hook scripts.
After deployment, e.g. when using eb ssh
, you need to load the environment properties manually.
One option is to use the EB get-config tool. The environment properties can be accessed either individually (using the -k
option), or as a JSON or YAML object with key-value pairs.
For example, one way to export
all environment properties would be:
export $(/opt/elasticbeanstalk/bin/get-config --output YAML environment |
sed -r 's/: /=/' | xargs)
Here the get-config
part returns all environment properties as YAML, the sed
part replaces the ': '
in the YAML output with '='
, and the xargs
part fixes quoted numbers.
Note this does not require sudo
.
Alternatively, you could refer to this AWS knowledge center post:
Important: On Amazon Linux 2, all environment properties are centralized into a single file called
/opt/elasticbeanstalk/deployment/env
. You must use this file during Elastic Beanstalk's application deployment process only. ...
The post describes how to make a copy of the env
file during deployment, using .platform
hooks, and how to set permissions so you can access the file later.
You can also perform similar steps manually, using SSH. Once you have the copy set up, with the proper permissions, you can source
it.
Beware:
Note: Environment properties with spaces or special characters are interpreted by the Bash shell and can result in a different value.
Try running the command /opt/elasticbeanstalk/bin/get-config environment
after you ssh into the EC2 instance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With