Run Django manage.py shell commands on elastic beanstalk on AL2
S

1

6

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 ?

Stenotype answered 5/6, 2020 at 9:24 Comment(0)
S
33

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

Stenotype answered 5/6, 2020 at 9:32 Comment(6)
there does not seem to be any need to change to the staging directoryExtraneous
AWS post about accessing environment variables on AL2Extraneous
An alternative way to load the environment properties is to use get-config, 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.Extraneous
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 hoursFiddlestick
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 - webappMenstruation
execute this after connecting to running instance using eb ssh <environment-name>. Also have a look at thisAdrianadriana

© 2022 - 2024 — McMap. All rights reserved.