.ebextensions commands for Django fail after restart of ec2 instance in Elastic Beanstalk environment


The deployment done via CI/CD or manually (via Elastic Beanstalk web interface -> Actions) has no issue. But, if the instance is restarted or a re-deployment is done automatically by the autoscaling group (the environment type is load balanced), all the commands in .ebextensions folder fail.

In cfn-init-cmd.log this is the error messages:

2023-04-06 08:53:54,931 PXXYY [INFO] Test for Command 01_migrate
2023-04-06 08:53:54,935 PXXYY [ERROR] Exited with error code 1
2023-04-06 08:53:54,950 PXXYY [INFO] ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2023-04-06 08:53:54,950 PXXYY [INFO] Config postbuild_1_DjangoWebApp
2023-04-06 08:53:54,957 PXXYY [INFO] ============================================================
2023-04-06 08:53:54,957 PXXYY [INFO] Test for Command 01_collectstatic
2023-04-06 08:53:54,960 PXXYY [ERROR] Exited with error code 1

In cfn-init.log:

2023-04-06 08:53:54,903 [INFO] -----------------------Starting build-----------------------
2023-04-06 08:53:54,910 [INFO] Running configSets: Infra-EmbeddedPostBuild
2023-04-06 08:53:54,912 [INFO] Running configSet Infra-EmbeddedPostBuild
2023-04-06 08:53:54,915 [INFO] Running config postbuild_0_MyWebApp
2023-04-06 08:53:54,935 [INFO] Test failed with code 1
2023-04-06 08:53:54,946 [INFO] Test failed with code 1
2023-04-06 08:53:54,950 [INFO] Running config postbuild_1_MyWebApp
2023-04-06 08:53:54,961 [INFO] Test failed with code 1
2023-04-06 08:53:54,962 [INFO] ConfigSets completed
2023-04-06 08:53:54,962 [INFO] -----------------------Build complete-----------------------

Is there any other log file that I should look into? I did not find anything interesting in django.log and web.stdout.log files.

My migrate.config file:

        command: "source /var/app/venv/*/bin/activate && python3 manage.py migrate"
        leader_only: true

The commands in post_actions.config also fail:

    command: "source /var/app/venv/*/bin/activate && python3 manage.py collectstatic --noinput"
    leader_only: true
1 Answer

Greetings User-5343824

This is expected behavior, looking at your .ebexensions (migrate.config, post_actions.config) your test is leader_only. As mentioned in the Elastic Beanstalk ebestensions documentation.

You can use leader_only to only run the command on a single instance, or configure a test to only run the command when a test command evaluates to true. Leader-only container commands are only executed during environment creation and deployments, while other commands and server customization operations are performed every time an instance is provisioned or updated. Leader-only container commands are not executed due to launch configuration changes, such as a change in the AMI Id or instance type.

In extension to this Reboots of current instances or the launch of new instances do not trigger the leader_only evaluation.

If you need commands like the 01_migrate or 01_collectstatic I would recommend removing the leader_only test. from their definition

migrate.config file:

        command: "source /var/app/venv/*/bin/activate && python3 manage.py migrate"


    command: "source /var/app/venv/*/bin/activate && python3 manage.py collectstatic --noinput"

Should you require a constant command to be run within your fleet you could implement the following ebextension cron-leaderonly-linux

This .ebextension configures a script on the instances that perform a describe on the Environment Auto Scaling group and if it matches the first instance in the sorted list it will exit 0. This will mean that this script will only exit 0 for one of the instances in your environment.

Hope this helps

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions