Conventions to Help Minimize your ENV File

Posted: 2015-06-09 18:35:04

UPDATE

I think we can also do AWS_BUCKET=foo_$APP_ENV directly in the env file, see https://mattstauffer.co/blog/laravel-5.0-environment-detection-and-environment-variables - Andrew Cavanagh

The .env file can be a real pain and get pretty big. And even though there is a tool, I made :), to move it around easily here is a thought on reducing it a bit so it is the same on all environments.

Typically we have dev, stage, production, testing and local. And when you consider all are services we might use it can be pretty large for example

  • Amazon or Iron.io and different queues
  • Database Settings
  • AWS Buckets

So a normal config could look like this on dev for example

APP_ENV=dev

APP_DEBUG=false
APP_KEY=yo

DB_HOST=localhost
DB_DATABASE=db_name_dev
DB_USERNAME=username_dev
DB_PASSWORD=secret

AWS_BUCKET=some-bucket-dev
AWS_ACCESS_KEY=foo
AWS_SECRET_KEY=bar
AWS_REGION=us-east-1

IRON_TOKEN=sometoken
IRON_PROJECT_ID=someid
IRON_QUEUE=QueueNameFooDev
IRON_DIFF_QUEUE=QueueNameBarDev

DYNAMO_RT_TABLE=table_dev
DYNAMO_DIFF_TABLE=table_dev

IRON_WORKER_PROJECT_ID_THUMBNAILS=somekey
IRON_WORKER_TOKEN_ID_THUMBNAILS=sometoken

DYNAMO_OCR_RT_TABLE='some_table_dev'
OCR_IRON_TOKEN='sometoken'
OCR_IRON_PROJECT_ID='somekey'
OCR_IRON_QUEUE='foo_dev'

That is about 24 settings but if we alter our code to do this when getting settings

'some_key' => env('OCR_IRON_QUEUE') . '_' . env('APP_ENV');

Instead of

'some_key' => env('OCR_IRON_QUEUE');

And we name our services and resources as needed _dev or _stage or _production then we can simplify the list above so it is the same for most of the environments.

For example OCR_IRON_QUEUE='foo_dev' is now OCR_IRON_QUEUE='foo' and can be the same on all servers!

APP_ENV=dev

APP_DEBUG=false
APP_KEY=yo

DB_HOST=localhost
DB_DATABASE=db_name
DB_USERNAME=username
DB_PASSWORD=secret

AWS_BUCKET=some_bucket
AWS_ACCESS_KEY=foo
AWS_SECRET_KEY=bar
AWS_REGION=us-east-1

IRON_TOKEN=sometoken
IRON_PROJECT_ID=someid
IRON_QUEUE=QueueNameFoo
IRON_DIFF_QUEUE=QueueNameBar

DYNAMO_RT_TABLE=table
DYNAMO_DIFF_TABLE=table

IRON_WORKER_PROJECT_ID_THUMBNAILS=somekey
IRON_WORKER_TOKEN_ID_THUMBNAILS=sometoken

DYNAMO_OCR_RT_TABLE='some_table'
OCR_IRON_TOKEN='sometoken'
OCR_IRON_PROJECT_ID='somekey'
OCR_IRON_QUEUE='foo'

Convention over configuration so simple yet so easy for me to forget :)


Tags:

laravel php