arabold / serverless-export-env

Serverless plugin to export environment variables into a .env file
MIT License
102 stars 34 forks source link

Add optional parameter "overwrite" which when set to false, perserves… #30

Closed williamsandonz closed 3 years ago

williamsandonz commented 3 years ago

This PR addresses an issue encountered when using this plugin with the NX framework. NX is a monorepo framework whereby each app exists in a separate directory (/apps/app1). Each of these directories typically has it's own serverless.yml file.

When running locally developers will use the NX CLI to start their projects using 'nx serve app1' or 'nx serve app2'. The NX CLI respects all .env and .local.env files in both the apps/app directories and root (more info here).

The problem is that the values present in apps/app1/.env will not be accessible from app2 (e.g when running nx serve app2). This is because NX deliberately conceals them as they are intended to be of a private scope for that app. Therefore the only work around is to use { pathFromRoot: "../../", filename: .env } options, however we then run into an issue whereby apps are overwriting eachothers values in the root directory .env everytime serverless export-env is run inside an app.

To circumvent this issue, the overwrite: false flag will ensure existing values in the ENV file are preserved thereby making it possible for a service B to have runtime access to the dynamic/SLS variables from service A.

williamsandonz commented 3 years ago

Hey @arabold, any feelings/thoughts on this? Happy to make some tweaks if needed.

williamsandonz commented 3 years ago

I've added another change in which is a "ignoreEnvironmentKeys" keys flag. Sometimes we only want environment vars declared in serverless.yml to be used in the lambda functiona and not have them exported to a .env file

arabold commented 3 years ago

I'm currently working on a new v2.0.0. I've considered all your suggestions in this new version.

The version is not pushed to npm yet but a branch exits: https://github.com/arabold/serverless-export-env/tree/v2.0.0

williamsandonz commented 3 years ago

Hey @arabold, that's really exciting news, thanks for letting me know, I'll keep an eye out! The one other feature that I thought could be nice is that sometimes we need to expose certain ENV vars to lambda at runtime but we don't necessarily want them to be output to an ENV file, because they themseves quite often have already come from a different ENV file so we end up with a key showing up in two files which can be confusing/messy. Unless I'm missing a way to do this? Anyway, thanks for all the hard work keeping this package going, it's great.

arabold commented 3 years ago

To simplify testing the new version, I've published 2.0.0-alpha.0 as alpha release to npm:

npm install serverless-export-env@2.0.0-alpha.0 --save-dev

The new version will not write a .env file if a Lambda is executed locally (i.e. sls invoke local or sls offline start). It will simply act as an AWS variable resolver. The local environment variables at runtime should match the ones of the deployed Lambda. No more, no less. For use cases that need a dedicated .env file, run sls export-env instead. By default, sls export-env will not overwrite any existing .env file.

Hopefully, this resolves your remaining use cases as well. Let me know! 👍

arabold commented 3 years ago

I've pushed 2.0.0. Closing this now.