Hello friends,

I'm Yogesh Galav and currently working as Technical Product Manager at InstaWP, Recently I came across a problem of manually managing env file for multiple environments so I tried to came up with cost effective solution to it, I hope you read it with open mind.
The topic of this blog could also be:

How I managed to decrease the size of env with 400+ keys to just 40 keys.

Like most of you at first I faced hurdle from my team mates of opposing dynamism or quick change culture, but after application was still running smoothly in develop/staging env I was able to gain their trust, hence looking forward to gain yours too.

Some of you might have heard of solution to this problem like Infisical, Vault, AWS Parameter Store, Google Secret Manager but they involves cost in some form like money or work or maintenance.
Hence I tried to came up with solution which involves one time change of one person. It was like setting up a new culture. I know setting into a new culture is hard but Software development is all about it.

Let's now move directly to the point. Pros and Cons of ENV file management

Pros:
Env file is not shared with version control like git.
Cons:
Manual management is too much risky and keys sharing is still needed.

Pros:
Env keys are kept as secret.
Cons:
Developers add keys which are not even a secret like urls.

Pros:
Env allows to make quick change like Christmas discount.
Cons:
Quick change sometime can break things in unknown way.

If you only sees pros mentioned above and not the cons then you may purchase solutions in market. No hard feelings.

But if you also understand the cons mentioned above then your brain might be searching for solution and strategy to implement it.
Here's what I did in step by step manner.

  1. First of all you need intermediate files like const.js/const.php which will provide all the keys to your application as destination and also will have fallback value if not present in source env file. If you are using framework like laravel it already provide config files which can use env keys and fallbacks.
export const DISCOUNT = env.DISCOUNT || '5%'
  1. Replace env values with const values imported from intermediate file. You can transfer some of the logic to that file as well. Most values in this file will be decided by app environment i.e. local, staging, production etc
import { DISCOUNT } from 'const.js'
let userDiscount = DISCOUNT
  1. Categorize ENV keys, with help of your team you can categorize your ENV keys into Unused/depreciated, Unique values, Same value for all env, different value only for prod. I personally used slack canvas for it.

  2. Delete Unused/depreciated ENV keys.

  3. Delete ENV keys which holds insensitive values like urls or text. And hardcode it in intermediate file. In most of the cases this values will never change neither they are sensitive which abuse dev can make use of,
    hence it's better to reduce load.

export const TERM_URL = '/term'
  1. If you are using same values for local and prod, how is that a secret?
    Hence you can hardcode that in intermediate file as well.

  2. Last remaining items will be Unique values. Your local env will be smallest then dev or stage, Prod env will still hold much key values because they are secrets which can't be shared.

By reaching here, you and your team has removed lot of pain by reducing ENV size and changing the culture.

BTW If you depend on too much services, It might increase your cost so try to reduce those services as well.

Thanks for the read,
Yogesh Galav