AWS Elastic Beanstalk - Set .env Laravel in Environment Properties

Viewed 2320

From my understand AWS Elastic Beanstalk has its own Environment Properties located in the Software Configuration panel, clicking on the Gear icon. In my Laravel's .env file I have information like my S3Bucket where I upload images, my Mail service and some others. Does this mean that I have to add each key and value pair from Laravel's .env file to the Environment Properties of AWS Elastic Beanstalk?

1 Answers

The short answer is Yes.

If you add environment variables within the Software Configuration, particularly, if you don't upload your .env file to the server, you'll have to reference these variables within your code.

For example, here is how you can reference this configuration within Elastic Beanstalk on the server and use the .env file locally:

Within config/app.php:

<?php

return [
    ...
    'env' => array_key_exists('APP_ENV', $_SERVER) ? $_SERVER['APP_ENV'] : env('APP_ENV'),
    'debug' => array_key_exists('APP_DEBUG', $_SERVER) && $_SERVER['APP_DEBUG'] === 'false' ? false : env('APP_DEBUG'),

    ...

Note that I am doing 2 things here.

  1. For the APP_ENV, since I will want 'production' on the server but 'dev' locally, here I want to, if available (i.e. only on the production server) use $_SERVER['APP_ENV'], otherwise use APP_ENV that I have defined in the .env file locally. Note that on Elastic Beanstalk, you just name the variable APP_ENV as well.

  2. This is a bit of a gotcha. Any of the variables that you set within Elastic Beanstalk will come out as strings. So, of course, you will want to set APP_DEBUG to false when in production. However, this comes out to the string "false", which, if you test it "false" == true. Therefore, this will turn out to be true on the server. In order to get around this, I have used the code above to test if it APP_DEBUG is set to 'false' and if so, I set it to the boolean false

Now, this is only 2 examples within the config/app file. For you, you'll have to do the same for your S3 bucket and Mail servers within their respective config files.

I know that I'm replying super late, but I hope this helps someone else! :)

Related