amazon-web-servicesamazon-ec2aws-code-deploy

Why won't my AWS CodeDeploy run my script after deployment?


I am using CodeDeploy to deploy my applications to EC2 instances created by an Auto Scaling Group.

The applications deploy fine and are moved to their correct file mapped locations, however my AfterInstallation script is never executed. I can see in the logs that it tries to make the script executable and passes that stage, but it never gets executed.

Here is my appspec.yml

version: 0.0
os: linux
files:
  - source: /
    destination: /opt/lobby
hooks:
  AfterInstall:
    - location: bootstrap.sh
      timeout: 30
      runas: root

Here is the script

#!/bin/bash
nohup nodejs lobby.js &
echo "finished"

I do not see the echo printed nor do I see any processes running related to lobby.js. I can verify that this script works by typing ./bootstrap.sh after the deployment and this works fine. This hook should do this for me though.

This needs to be a background task as I will be running multiple applications in this script but only one is displayed now just to get it working.


Edit:

I have referred to http://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html#appspec-hooks-server and tried replacing AfterInstall with ValidateService and AfterAllowTraffic (but I'm not using a LoadBalancer)

Question

Why is my AfterInstallation script not getting called, or so it seems?


Solution

  • AfterInstall: AfterInstall script contains the tasks need to be executed after installing the Application.

    Example of BeforeInstall script:

    #!/bin/bash
    cd /home/ubuntu/production/weone-backend
    sudo chown -R ubuntu:ubuntu /home/ubuntu/production
    sudo NODE_ENV=production nohup nodejs app.js > /dev/null 2> /dev/null < /dev/null &
    

    In the above script, we are changing the ownership of our application folder & starting application process.

    Note: Use “/dev/null 2> /dev/null < /dev/null &” to get out of nohup shell automatically, else your CodeDeploy would get stuck at AfterInstall event.

    https://www.oodlestechnologies.com/blogs/AWS-CodeDeploy