I am saving my .env
content dynamically from the AWS secrets manager, but I want to save all values just once the server starts. What should be the approach?
I am using TypeScript:
getSecrets("key").then((keys: any) => {
const originalKeys = JSON.parse(keys);
for (const key in originalKeys) {
if (originalKeys.hasOwnProperty(key)) {
appendFileSync(
__dirname + "/.env",
`${key}='${originalKeys[key]}'n`
);
}
}
2
Answers
You could use a boolean to remember whether the code has been executed or not. Something like this:
Why do you want to save them in
.env
? You can save them in the config object and you can reuse them where ever you need them.In your files you can use this:
You can check here for more details.