Python – Running a script before packages are installed from requirements.txt

amazon-web-servicesdjangoelastic-beanstalkpython

My app has some prerequisite conditions that mean that the installs from requirements won't work until a few scripts are run.

I've been reading through the documentation on customizing the Python container but it doesn't make it at all clear how you control when a script in .ebextensions gets run and all testing I've done suggests it happens after the app is deployed, or at least after the packages from requirements.txt are installed using pip.

This post on the AWS forums recommends creating a script with a filename that it inserts it into the hooks folder, but this is not apparently recommended:

Dropping files directly into the hooks directories is risky as this is not the documented method, is different in some containers and may change in the future.

I'm wondering if anyone has successfully set up a script to run before the requirements.txt file and if so what I might be doing wrong.

Here's my .ebextensions file:

packages:
  yum:
    # packages needed for my app

files:
    "/home/ec2-user/setup-script.sh":
        mode: "00755"
        owner: ec2-user
        group: ec2-user
        encoding: plain
        content: |
            #!/bin/bash
            echo $(date -u) >> /tmp/debug.log
            echo "Running as $(whoami)" >> /tmp/debug.log
            # rest of script
commands:
    01-setup-script:
        command: "sh -x /home/ec2-user/setup-script.sh"

The deploy halts in the middle of installing requirements and does not appear to run the script, or update the script as it exists in the user folder.

Best Answer

Okay, so it turns out that the script does indeed run before everything else. Looks like there was an error in the script itself that meant it did not run correctly and that's the reason that it wasn't setting up the instance correctly!

I discovered this by looking at the output of /var/log/eb-activity.log

So I guess always check the output of your scripts carefully! Wouldn't have hurt to add set -e at the top of the script so it halted execution when it ran into an error.

Related Topic