dotenv and Elastic Beanstalk – Environment file .env not found or not readable

Question:

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:

I understand that system doesn’t find .env

Thing is I have set variables in Amazon Console :

eb printenv :

I also have in .ebextensions/environment.config :

But can’t get rid of this error!

Answer:

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.

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:


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

Leave a Reply