Run manage.py from AWS EB Linux instance
As of February 2022 the solution is as follows:
$ eb ssh
$ sudo su -
$ export $(cat /opt/elasticbeanstalk/deployment/env | xargs)
$ source /var/app/venv/*/bin/activate
$ python3 /var/app/current/manage.py <command name>
$ export $(cat /opt/elasticbeanstalk/deployment/env | xargs) is needed to import your environment variables if you have a database connection (most likely you will)
TL;DR
This answer assumes you have installed EB CLI. Follow these steps:
- Connect to your running instance using ssh.
eb ssh <environment-name>
- Once you are inside your environment, load the environment variables (this is important for database configuration)
. /opt/python/current/env
If you wish you can see the environment variables using printenv
.
- Activate your virtual environment
source /opt/python/run/venv/bin/activate
- Navigate to your project directory (this will depend on your latest deployment, so use the number of your latest deployment instead of
XX
)
cd /opt/python/bundle/XX/app/
- Run the command you wish:
python manage.py <command_name>
Running example
Asumming that your environment name is my-env
, your latest deployment number is 13
, and you want to run the shell
command:
eb ssh my-env # 1
. /opt/python/current/env # 2
source /opt/python/run/venv/bin/activate # 3
cd /opt/python/bundle/13/app/ # 4
python manage.py shell # 5
How to run manage.py from AWS Elastic Beanstalk AMI.
- SSH login to Linux (
eb ssh
)- (optional may need to run
sudo su -
to have proper permissions)
- (optional may need to run
source /opt/python/run/venv/bin/activate
source /opt/python/current/env
cd /opt/python/current/app
python manage.py <commands>
Or, you can run command as like the below:
cd /opt/python/current/app
/opt/python/run/venv/bin/python manage.py <command>
With the new version of Python paths seem to have changed.
- The app is in /var/app/current
- The virtual environment is in /var/app/venv/[KEY]
So the instructions are:
- SSH to the machine using
eb shh
- Check the path of your environment with ls
/var/app/venv/
. The only folder should be the[KEY]
for the next step - Activate the environment with
source /var/app/venv/[KEY]/bin/activate
- Execute the command
python3 /var/app/current/manage.py <command>
Of course Amazon can change it anytime.