3

So this has been a long-running problem for me and I'd love to fix it - I also think it will help a lot of others. I'd love to run Django commands after ssh'ing on my Elastic Beanstalk EC2 instance. E. g.

python manage.py dumpdata

The reason why this is not possible are the missing environment variables. They are present when the server boots up but are unset as soon as the server is running (EB will create a virtual env within the EC2 and delete the variables from there).

I've recently figured out that there is a prebuilt script to retrieve the env variables on the EC2 instances:

/opt/elasticbeanstalk/bin/get-config environment

This will return a stringified object like this:

{"AWS_STATIC_ASSETS_SECRET_ACCESS_KEY":"xxx-xxx-xxx","DJANGO_KEY":"xxx-xxx-xxx","DJANGO_SETTINGS_MODULE":"xx.xx.xx","PYTHONPATH":"/var/app/venv/staging-LQM1lest/bin","RDS_DB_NAME":"xxxxxxx":"xxxxxx","RDS_PASSWORD":"xxxxxx"}

This is where I'm stuck currently. I think need would need a script, that takes this object parses it and sets the key / values as environment variables. I would need to be able to run this script from the ec2 instance.

Or a command to execute from the .ebextensions that would get the variables and sets them.

Am I absolutely unsure how to proceed at this point? Am I overlooking something obvious here? Is there someone who has written a script for this already? Is this even the right approach?

I'd love your help!

Xen_mar
  • 8,330
  • 11
  • 51
  • 74
  • related: https://stackoverflow.com/q/62881140, https://stackoverflow.com/q/63280543, https://stackoverflow.com/a/62212223 – djvg Aug 03 '21 at 09:26

1 Answers1

8

Your env variables are stored in /opt/elasticbeanstalk/deployment/env

Thus to export them, you can do the following (must be root to access the file):

export $(cat /opt/elasticbeanstalk/deployment/env | xargs)

Once you execute the command you can confirm the presence of your env variables using:

env

To use this in your .extentions, you can try:

container_commands:
  10_dumpdata:
    command: |
      export $(cat /opt/elasticbeanstalk/deployment/env | xargs)
      source $PYTHONPATH/activate
      python ./manage.py dumpdata
Marcin
  • 215,873
  • 14
  • 235
  • 294