As I wrote in this and this articles, we try to not use hardcoded variables and credentials and try to reduce at a minimum the .env files ( which we never push anyway).
The problem still, is that many times we need to rely on environment variables to run some tests or to bundle in the frontend.
In such cases, I need to somehow retrieve some configurations from the Exports/Outputs in CloudFormation, or from SecretsManager / Parameter Store and save them to a .env file that is being loaded by my tests or by the package bundler.
Honestly - I still haven't found a satisfying solution for this ( if you have some suggestions, feel free to drop it in the comments)
This is what happened to me last week, I wanted to share this, as a reminder for my future self and for all the devs old and new: Evil is in the details.
During the first phases of the project, we had a bunch of vars in the .env file. Then we decided to dynamically generate that file after the deployment of the serverless stack was completed, filling it with the values exported from Cloud Formation.
Just a matter of a
npm script postdeploy: generateEnvs
and a very short node Js file which uses AWS-SDK and grabs some values from our Cloud Formation Stacks and writes them to file.
I run the script, and the file is there, with all the necessary stuff.
Therefore I bundle my front-end app using that file ... and nothing works. All the env variables are undefined.
At first, I thought I might have written the file in the wrong way, maybe some space, some typo.. but no: everything looked exactly as it should.
After many attempts, I also tried back and forth the original file and the generated - identical - one, but no. I was not working. I even asked a colleague for another pair of eyes, but he agreed:
- they looked identical
- they seemed exactly the same
they looked... but for some reason, they were NOT!
these words bounced in my brain until I had an idea: let´s see the whitespace and non-printable characters!!
And... yes. the two files were not the same anymore.
The working one was using \n
(newline) while in the generated one I was using a \r
(carriage return) (and all the variables where actually inlined...)
I don´t know why I instinctively use that and not a new line (maybe because I actually learned typing on a typewriter...),
but I did not know it could make such a difference - as soon as I changed my script to use the new line my script started working as intended.
Quite relieving even though quite disappointing having wasted so much time in something so stupid.
Top comments (0)