How to properly manage .env files in a microservices architecture

Viewed 1350

I have been working for some time on a project with a microservices architecture where each service has its own environment variables which are handled with a .env file for each service/repository.

A great part of these variables are related to other services IPs and external resources keys which are different in each environment: Development, Staging, and Production so the .env is not a simple one.

Our development pace is fast and most of the time these variables change with new features and or changes implemented by teammates working on issues related to that service. This causes that almost every time others want to work with a service they get blocked and have to update the .env file before. Therefore we end up requesting and sharing .env files with each other all the time and there is a lack of a "Source of truth" for all the .env files.

I was wondering if someone else has had this problem or a similar one before and what approaches has followed to solve it or improve it.

Is there any application or framework for sharing and managing .env files in a team in an automated way?

Thanks in advance!

EDIT

Just to be clear, these are not being added to source control and they are properly handled on CI/CD.

I was talking more about local development, setting up services locally, and keeping the .env local files up to date in an easy way.

2 Answers

As a summary of all the feedback provided by some coworkers and the community in both: r/SoftwareEngineering and r/softwaredevelopment (Thank you all for it) some of the most useful resources are:

And I want to quote what u/nickthemagicman commented which I think is an important point to take in mind:

But due to the fact that ya'll are still using .env files for this long and it's been this chaotic and no one has fixed this by now, it sounds like your biggest hurdle is going to be to get the team buy in, since it sounds like there's no centralized management either.

Not sure what stack you're using but we're solving this with Infisical.

It provides a source of truth for your environment variables and supports different environments (development, staging, and production). Your team can either automatically inject those variables into your local process or manually pull back environment variables to update your .env file — whichever you're comfortable with most; it's end-to-end encrypted.

We ran into the same issues you're outlining and are finally solving them.

Related