Search code examples
amazon-web-servicesdeploymentcomposer-phpsymfonyaws-code-deploy

bug in codedeploy afterinstall hook?


We have autoscaling set up for our symfony3 application. We are using aws codedeploy to deploy to autoscaling instances.

My appspec.yml file

version: 0.0
os: linux
files:
  - source: /
    destination: /usr/share/nginx/<some_dir>
hooks:
  AfterInstall:
    - location: post_deploy.sh
      timeout: 180
      runas: ubuntu

post_deploy.sh

#!/bin/bash
doc_root=/usr/share/nginx/<some_dir>
current_dir=$PWD
cd $doc_root
sudo -E composer install --no-interaction --no-dev --optimize-autoloader
cd $current_dir

and also exported environment variables for parameters.yml file

when we deploy revision, codedeploy succeed in deployment. But when i access my app through browser, nginx error log says:

PHP Fatal error:  Uncaught exception 'Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException' with message 'You have requested a non-existent parameter "database.host". Did you mean one of these: "database_host", "database_port"?

Strange thing is that when i run post_deploy.sh script manually by logging in to my server it executes well and no error afterwards. I don't know how to deal with it.


Solution

  • codedeploy doesn't preserve env var in spite of -E option. So i pass the env var in command itself, like this

    sudo SYMFONY_ENV=$SYMFONY_ENV SYMFONY__DATABASE__NAME=$SYMFONY__DATABASE__NAME SYMFONY__DATABASE__USER=$SYMFONY__DATABASE__USER SYMFONY__DATABASE__HOST=$SYMFONY__DATABASE__HOST SYMFONY__DATABASE__PORT=$SYMFONY__DATABASE__PORT SYMFONY__DATABASE__PASSWORD=$SYMFONY__DATABASE__PASSWORD COMPOSER_HOME=$COMPOSER_HOME composer install --no-interaction --no-dev --optimize-autoloader
    

    worked for me.