dotenv and Elastic Beanstalk - Environment file .env not found or not readable
Asked Answered
S

1

6

I'm trying to upload a Lumen project in Amazon Elastic Beanstalk.

.env is in .gitignore.

This is OK, because I have several environement ( dev, qa, prod), so I need to configure have separate env variable for each environement

I get this error message:

 Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Dotenv: Environment file .env not found or not readable. Create file with your environment settings at /var/app/current/bootstrap/../.env' in /var/app/current/vendor/vlucas/phpdotenv/src/Dotenv.php:33 Stack trace: #0 /var/app/current/bootstrap/app.php(4): Dotenv::load('/var/app/curren...') #1 /var/app/current/public/index.php(13): require('/var/app/curren...') #2 {main} thrown in /var/app/current/vendor/vlucas/phpdotenv/src/Dotenv.php on line 33

I understand that system doesn't find .env

Thing is I have set variables in Amazon Console :

Software Configuration
Environment variables: APP_ENV, DB_USERNAME, DB_PASSWORD, DB_DATABASE,  DB_HOST, APP_KEY

eb printenv :

 Environment Variables:
  DB_DATABASE = ebdb
  DB_PASSWORD = xxxxxxxx
  APP_KEY = xAY4hnrXlht5fdvB9PzPAwDqc1R
  DB_HOST = xxxxxxcnzd3rux8ue7.us-east-1.rds.amazonaws.com:3306
  APP_ENV = dev
  DB_USERNAME = myuser

I also have in .ebextensions/environment.config :

 container_commands:
 # Copy EB env configuration file over
 01_config_environment:
  command: mv /var/app/ondeck/.env.elasticbeanstalk /var/app/ondeck/.env
 02-install-packages:
command: "composer.phar install -d /var/app/ondeck/www"
 option_settings:
 option_name: DB_HOST
 value: xxxxxxx.cnzd3rux8ue7.us-east-1.rds.amazonaws.com
- option_name: DB_PORT
 value: 3306
- option_name: DB_NAME
 value: ebdb
- option_name: DB_USER
 value: myuser
- option_name: DB_PASS
 value: xxxxxx

But can't get rid of this error!

Stitching answered 14/12, 2015 at 22:20 Comment(2)
Did you find the answer ?Autarchy
Nop, I didn't find the answerStitching
L
0

Lumen 5.0, 5.1

If you're on Lumen < 5.2, you can update your bootstrap/app.php file that is loading the the .env file. Just catch the exception and ignore it.

try {
    Dotenv::load(__DIR__.'/../');
} catch (InvalidArgumentException $e) {
    //
}

Lumen 5.2 - 5.7 don't have this issue, since they basically do the above already.

Lumen 5.8+

Lumen 5.8 reintroduced the issue, and also made it a little bit harder to get around. For this, you need to create a class that extends \Laravel\Lumen\Bootstrap\LoadEnvironmentVariables, and override the bootstrap() method. For example, create a new file app/Bootstrap/MyLoadEnvironmentVariables.php:

<?php

namespace App\Bootstrap;

use Dotenv\Exception\InvalidFileException;
use Laravel\Lumen\Bootstrap\LoadEnvironmentVariables;

class MyLoadEnvironmentVariables extends LoadEnvironmentVariables
{
    public function bootstrap()
    {
        try {
            $this->createDotenv()->safeLoad();
        } catch (InvalidFileException $e) {
            //
        }
    }
}

Now, you need to update your bootstrap/app.php file to use your new class instead of the base class:

(new App\Bootstrap\MyLoadEnvironmentVariables(
    dirname(__DIR__)
))->bootstrap();
Lozier answered 24/12, 2020 at 21:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.