title: AWS: Order of Lifecycle scripts?
body: <p>I'm trying to use CodeDeploy integrating with my Auto Scaling Group.</p>
  
  <p>My configuration for auto scaling group has user data script to install PHP and etc. And my AfterInstall hook script for CodeDeploy runs a simple php file.</p>
  
  <p>User Data(this works fine)</p>
  
  <pre><code>#!/bin/bash
  apt-get -y update
  apt-get -y install ruby
  apt-get -y install wget
  cd /home/ubuntu
  wget https://aws-codedeploy-ap-northeast-2.s3.amazonaws.com/latest/install
  chmod +x ./install
  ./install auto
  
  sudo apt-get install -y python-software-properties
  sudo add-apt-repository -y ppa:ondrej/php
  sudo apt-get update -y
  sudo apt-get install php7.1 -y
  sudo apt-get install php7.1-zip php7.1-mbstring php7.1-dom php7.1-curl -y
  
  sudo php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
  sudo php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
  sudo php composer-setup.php
  sudo php -r "unlink('composer-setup.php');"
  sudo mv composer.phar /usr/local/bin/composer
  </code></pre>
  
  <p>I created a Auto Scaling Group with this Configuration but instances fail to launch with an error that PHP is an unknown command. And I have a few question about this. </p>
  
  <p>I think this is because <strong>AfterInstall hook of Code Deploy was started to execute before my user data script finishes to install PHP. Am I correct?</strong></p>
  
  <p>If I correct, <strong>how do I fix the order?</strong></p>
  
  <p>Building an AMI including all requirements pre-installed instead of install everything by user data script, could it be a solution? <strong>What is the best practice?</strong></p>
  
  <p>Any answer will be appreciated. Thank you for your time.</p>