[Django]-Run the right scripts before deployment elastic beanstalk

8đź‘Ť

âś…

AWS support solved the problem. Here’s their answer:

When Beanstalk is deploying an application, it keeps your application files in a “staging” directory while the EB Extensions and Hook Scripts are being processed. Once the pre-deploy scripts have finished, the application is then moved to the “production” directory. The issue you are having is related to the “manage.py” file not being in the expected location when your “01_collectstatic” command is being executed.

The staging location for your environment (Python 3.4, Amazon Linux 2017.03) is “/opt/python/ondeck/app”.

The EB Extension “commands” section is executed before the staging directory is actually created. To run your script once the staging directory has been created, you should use “container_commands”. This section is meant for modifying your application after the application has been extracted, but before it has been deployed to the production directory. It will automatically run your command in your staging directory.

Can you please try implementing the container_command section and see if it helps resolve your problem? The syntax will look similar to this (but please test it before deploying to production):

container_commands:
  01_collectstatic:
    command: "source /opt/python/run/venv/bin/activate && python manage.py collectstatic --noinput"

0đź‘Ť

So, the thing to remember about beanstalk is that each of the commands are independent, and you do not maintain state between them. You have two options in this case, put your commands into a shell script that is uploaded in the files section of ebextensions. Or, you can write one line commands that do all stateful activities prefixed to your command of interest.

e.g.,

00_collectstatic:
    command: "pushd /path/to/django && source /opt/python/run/venv/bin/activate && python manage.py collectstatic --noinput && popd"

Leave a comment