Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variable not recognised when deploying to Elastic Beanstalk

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.

like image 482
John Dorean Avatar asked Feb 02 '15 19:02

John Dorean


People also ask

How do I add env variables to Elastic Beanstalk?

On AWS, open Elastic Beanstalk. Go to your Application > Environment > Configuration > Software Configuration . Under Environment Properties you will find a list of properties you can configure. These variables will be attached to the process.

What are the two environments available in Elastic Beanstalk?

In AWS Elastic Beanstalk, you can create a load-balanced, scalable environment or a single-instance environment.

How do I get an Elastic Beanstalk environment ID?

The EB environment name and id is present in your instance tags. You could run the AWS CLI: aws ec2 describe-tags to get the environment name. The tag keys would be: elasticbeanstalk:environment-id.


2 Answers

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

like image 155
Anthony Manning-Franklin Avatar answered Sep 20 '22 02:09

Anthony Manning-Franklin


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.

like image 41
Nizar Blond Avatar answered Sep 19 '22 02:09

Nizar Blond