Search code examples
amazon-web-servicesaws-code-deploy

AWS codedeploy deployment throwing "[stderr] Could not open input file" while trying to invoke a php file from the sh file at afterInstall step


I have the following defined in the appspec file -

hooks:
  AfterInstall:
    - location: afterInstall.sh 

Following is the content of afterInstall.sh (I am trying to invoke a php file from the sh file) -

php afterInstall.php

Both the files afterInstall.sh and afterInstall.php are at the same level (outermost level) in the zip archive that I am uploading to S3 -

appspec.yml
afterInstall.sh
afterInstall.php

I am getting the following error -

Error Code         ScriptFailed
Script Name        afterInstall.sh
Message            Script at specified location: afterInstall.sh failed with exit code 1

Log Tail          LifecycleEvent - AfterInstall
                  Script - afterInstall.sh
                  [stderr]Could not open input file: afterInstall.php

I also tried by adding the following to the permissions section of the apppsec file -

permissions:
  - object: .
    pattern: "**"
    owner: sandeepan
    group: sandeepan
    mode: 777
    type:
      - file

Note - I have the login credentials of the deployment instances using the sandeepan user.

I am a bit confused with what exactly the permissions section does. From http://docs.aws.amazon.com/codedeploy/latest/userguide/app-spec-ref-permissions.html,

The permissions section specifies how special permissions, if any, should be applied to the files and directories/folders in the files section after they are copied to the instance.

I also tried by specifying owner/group as root/root and runas: root against the afterInstall hook, but still getting the same error.

Update

I also tried by specifying the afterInstall.php file in the files section, and ensuring its permission and ownerships are correct -

  - source: afterInstall.php
    destination: /var/cake_1.2.0.6311-beta

At /var/cake_1.2.0.6311-beta -

-rwxrwxr-x  1 sandeepan sandeepan   26 Aug  1 08:55 afterInstall.php

I have no other clue what else should be done to fix this.

Note - I am able to deploy successfully if I do not call a php file from the afterInstall.sh


Solution

  • The root cause of the error is that the php file reference is incorrect. Your script assumes that the current working directory is the the destination folder, or the deployment archive folder.

    This is a reasonable assumption, however neither of these is correct. On my Ubuntu server, the current working directory of the CodeDeploy shell invocation is actually /opt/codedeploy-agent. This explains why you get the "Could not open input file" error.

    Since you are in the afterInstall lifecycle hook, all your files already exist in the final destination. To solve the problem, use the path specified in the destination: directive in your afterInstall.sh:

    #!/bin/bash
    php /var/cake_1.2.0.6311-beta/afterInstall.php
    

    This will allow php to locate the correct file, and you deployment will run successfully.

    Update:

    If you want to run a file in the beforeInstall hook, the file must already exist on the system, and be referenced by a fixed path such as /tools.

    This can be accomplished by one of the following:

    1. Use a user-data script to download the script at instance launch time, or
    2. 'Baking' the script into the AMI image itself, and launching from that image.

    In either case, the beforeInstall hook can then call the script from its fixed path, eg php /tools/beforeInstall.php.

    I prefer option 1 in these cases. We maintain an S3 bucket with these type of assets, which are then maintained on S3, and downloaded to each instance at launch time. Any updates are pushed to S3, and are called for each new instance launch.