1

We are deploying a Laravel-based application to AWS using OpsWorks. We have defined environment variables through the OpsWorks console and they are correctly seen by Apache. However, when running CLI tasks such as artisan commands, they are not set.

I understand this is due to environment variables being set on Apache's virtual host configuration files. I thought I could fix the issue by running a deploy hook that echoes these variables and appends them to /etc/environment, but this does not work (and even if it did, it would be unbelievably weak).

Is there any other way to achieve this? Does OpsWorks allow this specific requirement?

Thanks.

cafonso
  • 121
  • 4

2 Answers2

1

Here's how we managed to solve this (although not ideal, it does the trick):

  • Use getenv() to access all environment variables: Apache won't see them if using $_ENV.
  • Use an OpsWorks deploy hook to write all environment variables to .env.php, Laravel's config file for the production environment. Environment variables will be available to the deploy hook code via node[:deploy]['<short_app_name>'][:environment_variables][:<VAR_NAME>].
  • Run artisan commands specifying the production environment: php artisan --env=production --force <command-to-be-run>.

We are mainly using the above to run migrations from the first instance on the deployment queue.

cafonso
  • 121
  • 4
0

If you're also using custom Chef cookbooks you can set properties from JSON or OpsWorks Application variables at run time with a Chef template. The below is a config.php example for a Moodle setup and assumes you have an application called webapp with attributes configured such as moodle_dbhost, moodle_dbname, moodle_dbuser etc .

CFG->dbtype    = 'pgsql';
$CFG->dblibrary = 'native';
$CFG->dbhost    = '<%= node['deploy']['webapp']['environment']['moodle_dbhost'] %>';
$CFG->dbname    = '<%= node['deploy']['webapp']['environment']['moodle_dbname'] %>';
$CFG->dbuser    = '<%= node['deploy']['webapp']['environment']['moodle_dbuser'] %>';
$CFG->dbpass    = '<%= node['deploy']['webapp']['environment']['moodle_dbpwd'] %>';
$CFG->prefix    = 'mdl_';
$CFG->dboptions = array (
  'dbpersist' => 0,
  'dbport' => '<%= node['deploy']['webapp']['environment']['moodle_dbport'] %>',
  'dbsocket' => 0,
);

$CFG->wwwroot   = 'http://example.com/moodle/';
$CFG->dataroot  = '/var/www/moodle-data/';
J. Lawson
  • 86
  • 10