5

I have my Django service up and running on AWS Elastic Beanstalk (on Amazon Linux 2). eb deploy works perfectly. But I would like to run some functions from shell of Django on by eb or maybe perform migrations on my DB.

I am unable to find my package after I eb ssh <environment-name> so cant find the manage.py file.

Related question - Run manage.py from AWS EB Linux instance

I believe it might be outdated ?

djvg
  • 11,722
  • 5
  • 72
  • 103
Aditya Nambiar
  • 806
  • 1
  • 12
  • 22

1 Answers1

30

Change to the current app directory

cd /var/app/current/

Change to the staging app directory (only exists after a failed eb deploy)

cd /var/app/staging/

Activate the virtual env

source $(find /var/app/venv/*/bin/activate)

Load your environment variables

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

Now you can run management commands

python manage.py shell

Art
  • 2,836
  • 4
  • 17
  • 34
Aditya Nambiar
  • 806
  • 1
  • 12
  • 22
  • there does not seem to be any need to change to the staging directory – djvg Aug 03 '21 at 08:06
  • AWS post about [accessing environment variables on AL2](https://aws.amazon.com/premiumsupport/knowledge-center/elastic-beanstalk-env-variables-linux2/) – djvg Aug 03 '21 at 08:22
  • An alternative way to load the environment properties is to use [get-config](https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/custom-platforms-scripts.html), for example: `export $(/opt/elasticbeanstalk/bin/get-config --output YAML environment | sed -r 's/: /=/' | xargs)` This does not require `sudo`, so it may be useful in some cases. – djvg Aug 03 '21 at 11:35
  • Thanks a lot! I didn't execute this command ```export $(sudo cat /opt/elasticbeanstalk/deployment/env | xargs)``` And it was taking up my local environment variables. Saved me a lot of hours – Dinesh Singh Aug 09 '21 at 06:11
  • On Amazon Linux 2 it may be necessary (for example, to have permission to write to log files) to become the `webapp` user first: `sudo su -l - webapp` – Brad Solomon Jan 12 '22 at 00:36
  • execute this after connecting to running instance using `eb ssh `. Also have a look at [this](https://stackoverflow.com/questions/19997343/run-manage-py-from-aws-eb-linux-instance) – Art Jan 27 '22 at 04:03