6

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:

$env = $app->detectEnvironment(function() {
    return $_ENV['ENV_NAME'];
});

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

option_settings:
   - namespace: aws:elasticbeanstalk:application:environment
     option_name: ENV_NAME
     value: development
   - option_name: DB_HOST
     value: [redacted]
   - option_name: DB_PORT
     value: [redacted]
   - option_name: DB_NAME
     value: [redacted]
   - option_name: DB_USER
     value: [redacted]
   - option_name: DB_PASS
     value: [redacted]

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`:

ENV_NAME=development

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.

John Dorean
  • 3,744
  • 9
  • 51
  • 82
  • sounds like a user issue to me. perhaps the apache is running in a different user than the one you SSHed to. probably root vs. ec2-user. – Tal Feb 02 '15 at 20:55
  • 1
    Yes that's exactly it, for some reason the environment variables that I define on Elastic Beanstalk are only available to the `ec2-user` user and the web server, I need to environment variables to be system-wide. – John Dorean Feb 02 '15 at 22:05
  • Might be interested in: http://unix.stackexchange.com/questions/78061/get-environment-variable-of-other-user – Nick Humrich Feb 02 '15 at 22:29
  • @ChrisWhite why do you need the vars to be system wide? EB is designed to work with a strict set of users, each with its own roles. using SSH won't work nicely. – Tal Feb 03 '15 at 09:58
  • Is there any information on this question, I seem to be running into the same issue. – Asheliahut Jun 11 '15 at 20:01
  • Came across the same issue. Am running a JVM app and the environment variables need to show up for the "webapp" user, but they only show up for "ec2-user" (in Amazon Linux). – Raymond26 Nov 30 '15 at 01:17
  • I'm having a similar issue with environment variables for a php worker script being ran by `/opt/elasticbeanstalk/hooks/appdeploy/post/job_after_deploy.sh` Any tricks for migrating the variables from one user to another? – Anthony Manning-Franklin Jul 25 '16 at 06:44

2 Answers2

3

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

commands:
  create_post_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
    ignoreErrors: true
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/job_after_deploy.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      source /opt/elasticbeanstalk/support/envvars
      # Run PHP scripts here. #

From XuDing's answer to this question and this answer

Community
  • 1
  • 1
Anthony Manning-Franklin
  • 4,408
  • 1
  • 18
  • 23
2

I created a job that creates .env file every 5 minutes.

Add the following to your .ebextensions

   "/opt/elasticbeanstalk/hooks/appdeploy/post/91_set_create_app_env_file_job.sh":
  mode: "000755"
  owner: root
  group: root
  content: |
    #!/usr/bin/env bash

    echo "Removing any existing CRON jobs..."
    crontab -r

    APP_ENV=/var/app/current/.env
    EB_ENVVARS=/opt/elasticbeanstalk/support/envvars
    CONSTANTS=/var/app/current/.constants
    CRON_CMD="grep -oE '[^ ]+$' $EB_ENVVARS > $APP_ENV; cat $CONSTANTS >> $APP_ENV"

    echo "Creating .env file...."
    eval $CRON_CMD

    echo "Scheduling .env file updater job to run every 5 minutes..."
    (crontab -l 2>/dev/null; echo "*/5 * * * * $CRON_CMD")| crontab -

Reason I did it this way is that you may want to update your environment variables via the AWS UI Console.

This is the best solution in my opinion.

Nizar Blond
  • 1,826
  • 5
  • 20
  • 42