Environment variable not recognised when deploying to Elastic Beanstalk

Question:

I’m trying to deploy my Laravel application to Elastic Beanstalk in development mode. To make the application run in development mode rather than production, I’ve done the following in my /bootstrap/start.php file:

To actually create the environment variable, I’ve created a .config file in the following path: /.ebextensions/00environmentVariables.config with these contents:

When I run eb start from the command line, it spins up an EC2 instance and attempts to provision it, at which point it tells me it fails. and to check the logs. In the logs, I can see these entries:

PHP Notice: Undefined index: ENV_NAME in
/var/app/ondeck/bootstrap/start.php on line 28

Notice: Undefined index: ENV_NAME in /var/app/ondeck/bootstrap/start.php on line 28

So for some reason, the ENV_NAME environment variable doesn’t exist, even though I’ve specified it in 00environmentVariables.config. What’s even weirder, is that I can see the environment variable does exist under the software configuration settings of the EB environment:

enter image description here

To summarize:

  • I know my .config files are being parsed from looking at the logs
  • For some reason my Laravel application still doesn’t think that ENV_NAME eixsts
  • ENV_NAME eixsts both in the .config file and in my Elastic Beanstalk settings for this environment

EDIT

Alright so I worked out that the environment variables do work correctly when serving the application over the Apache HTTP server, but the environment variables don’t exist when running the PHP CLI.

In the above logs, it’s complaining about ENV_NAME not existing when running a /usr/bin/composer.phar install.

So, for some reason, my environment variables don’t exist within the PHP CLI but they do work normally when serving over Apache.

FURTHER EDIT

So I SSH’d into the EC2 instance that’s hosting my Laravel application on Elastic Beanstalk, and I can see the proper environment variables when I use the printenv command`:

However, if I do a die(var_dump($_SERVER)); and run the PHP CLI, I don’t see the environment variables that I’ve defined. Same story with $_ENV and getenv().

Why can’t I access my environment variables within the PHP CLI, when I can access them when Apache processes my PHP scripts?

YET ANOTHER EDIT

I made a test.php file with one line: die(var_dump($_ENV));.

When I run this using php test.php I successfully get my custom environment variables coming out, so this seems like a composer only problem, not a PHP CLI problem.

Answer:

I use a YAML script which sets the environment variables for the root user from the existing variables set for ec2-user. Add this to your .ebextensions folder with the .config extension.

From there you can run PHP cli and it will see the correct environment variables

From XuDing’s answer to this question and this answer

Leave a Reply