Elastic Beanstalk run post deploy script

12,045

Solution 1

Thanks to @gustaf and some other sites I managed to pull a solution together. It's not pretty but it works.

I created a file in .ebextensions/01_build.config

commands:
  create_post_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
    ignoreErrors: true
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/99_build_app.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      cd /var/app/current/app/
      sudo /opt/elasticbeanstalk/node-install/node-v6.2.2-linux-x64/bin/npm install
      sudo /opt/elasticbeanstalk/node-install/node-v6.2.2-linux-x64/bin/npm run build

hard coding the node-version is not perfect but it works for now.

Solution 2

Update: This is now much more developed and defined.

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html

Original:

I haven't done nodejs myself with Elastic Beanstalk, but I have done a lot of Django.

The commands: section runs before the project files are put in place. This is where you can install server packages for example.

The container_commands: section runs in a staging directory before the files are put in its final destination (which is /var/app/current it seems for nodejs). Here you can modify your files if you need to.

Reference for above: http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html

You can also run a post deploy scripts. This is not very well documented (at least it wasn't). You can do something like this:

files:
    "/opt/elasticbeanstalk/hooks/appdeploy/post/99_restart_delayed_job.sh":
        mode: "000755"
        owner: root
        group: root
        content: |
            #!/usr/bin/env bash
            service httpd restart

Solution 3

If you refer to the docs you'll find that container_commands is where you should put the app related stuff.

The file is executed based on preference of various parts, commands being the first and container_commands being the last. During this phase, the app hasn't been copied to the desired location (/var/app/current).

Also, if you set environment variables, these will be available to you in container_commands only.

Solution 4

As of Amazon Linux 2 you must use Platform Hooks. Other answers here only work with Amazon Linux AMI platform versions.

For example in your application root, create the

.platform/hooks/postdeploy

folder(s), then create a file called service-httpd-restart.sh

with contents of

#!/usr/bin/env bash
service httpd restart

To do the same thing as Gustaf's comment.

Share:
12,045
auo
Author by

auo

Updated on June 25, 2022

Comments

  • auo
    auo almost 2 years

    My node.js app consists of two things, an express backend and a react frontend.

    I have two package.json files in this structure

    package.json 
    /app/package.json
    

    What I want to do is run a script in my frontend folder, app, that builds my code.

    the script I want to run is npm install and npm run build How do I run this script after a deploy have happened?

    commands:
        01_app_npm_install:
            command: npm install
            cwd: app/
        02_app_npm_build:
            command: npm run build
            cwd: app/
    

    But it returns an error saying that "No such file or direc tory: 'app/'."

    Is this possible to do on aws elastic beanstalk?

  • auo
    auo over 7 years
    Even if I change it to container_commands it still can't find the directory app/. I have the .config file in a directory called .ebextensions, is that correct?
  • Gustaf
    Gustaf over 7 years
    I don't think you need to create that directory, it should be there already. I didn't have to do that in a python environment. You could obviously move a file into that directory in container_commands as well if you want.
  • Murakami
    Murakami about 4 years
    Thank you for the answer. One question, I used your answer but cannot figure out how can I set env variables when building react app. It is undefined even though I set it in GUI configuration. Have you encounter the same issue?
  • Mike Averto
    Mike Averto almost 3 years
    This pointed me in the right direction, BUT, as of AWS Linux 2, this is slightly different, see: docs.aws.amazon.com/elasticbeanstalk/latest/dg/… You must create a folder called .platform/hooks and put content like #!/usr/bin/env bash service httpd restart Right into the file
  • A_01
    A_01 about 2 years
    Its not working for me too. Throwing below err in eb-engine.log 2022/03/01 16:08:00.766820 [INFO] Following scripts will be executed in order: [99post_deploy_script.sh] 2022/03/01 16:08:00.766826 [INFO] Running script: .platform/hooks/postdeploy/99post_deploy_script.sh 2022/03/01 16:08:00.770608 [ERROR] An error occurred during execution of command [app-deploy] - [RunAppDeployPostDeployHooks]. Stop running the command. Error: Command .platform/hooks/postdeploy/99post_deploy_script.sh failed with error exit status 127. Stde: No such file or directory