You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support loading env variables from .env files #7907
Support .env files for environment variables resolution (as resolved via dotenv package).
Proposed solution
To not collide with serverless-dotenv-plugin in initial implementatin this functionality should be hidden behind an option (I propose top level: useDotenv, which should default to false).
That means any variables saturation is possible only after first service config read at
If we detect the .env or .env.${stage} file in service folder, we should communicate via deprecation message that native support for dotenv was baked in, and with v2, environment variables from those files will be loaded by default. and that to hide this message user should either set useDotenv to true (to load vars), or remove .env` vars
@herebebogans great thanks for info, I forgot about that one.
What's your opinion on having that in core?
One issue with a plugin is, that by nature of plugins it won't be loaded at begin of a process, but at some point of plugin initialization, which is after some env vars were already read by application code.
Hi, I developed the dotenv plugin which I created because it wasn't part of the core framework, so I'm happy to deprecate it if you move in this direction. The plugin does quite a bit to allow for some flexibility so just keep that in mind. Though if you don't want to have feature parity, the plugin could still be available to developers who need those extra features. Note, I JUST released a new major version last night that changes the way env files are resolved so if you're looking to do feature parity, please check that out.
Also, keep in mind that a LOT of developers are using this plugin. If you move forward with this change it should probably be an opt in feature since it could potentially conflict with their existing setup. Just something to keep in mind.
Note, I JUST released a new major version last night that changes the way env files are resolved so if you're looking to do feature parity, please check that out.
We're definitely look into that
Also, keep in mind that a LOT of developers are using this plugin. If you move forward with this change it should probably be an opt in feature since it could potentially conflict with their existing setup.
Yes, I thought about that, and I think we definitely should hide it behind an option in v1. I've added a point about that in main description
dotenv would be a top-level setting, enabled could be used to control if it should be used or not.
In my opinion, support for .env files should offer the same features as currently offered by the plugin in order to make it possible for people to migrate from it to the plain framework. If we only add loading variables for local resolution I'm afraid a lot of users will still depend on the plugin to inject variables into function environment.
Let me know what do you think, feedback will be much appreciated 🙌
I think dotenv plugin treats .env more as source of env vars for lambdas. While in this case we want to match behavior we already have implemented in Serverless Components. So .env simply allows us to fill process.env for deployment or other serverless command usage purposes. I believe it's also the more intended usage by dotenv package authors.
As a side note: it's the step towards unification with Serverless Components (altough that'll be a long journey)
I see it also as replacement for our --profile CLI and provider.profile, provider.credentials settings, which appeared to be problematic
So that means, that our implementation will mismatch serverless-dotenv-plugin and that's intended, but we also should clearly document it as such, as I believe many users will be coming from a plugin, and without clear explanation will definitely be confused.
What's your opinion on that? Have you been using dotenv plugin?
in addition to loading variables from .env files, also injects all of them (with include/exclude option for configuration) into deployed functions' environment. Should Serverless Framework support that?
I'd say that if we want some env vars to go into lambdas, then we should explicitly configure them in env, it's how it works now, and this is also approach we're taking in Serverless Components (which have dotenv support built-in)
should framework also support such configuration options
I don't think we should support any options (following above arguments).
I'd treat a dotenv as a convenient and straightforward process.env transport, through which we can load different env vars depending on context we're in, and that's it.
Hey @medikoo - thanks a lot for your responses. I've just reviewed the implementation in Serverless Components to have a better context. I totally agree with proper messaging to users that the feature-set between built-in env support will be different than the one provided by the plugin.
What's your opinion on that? Have you been using dotenv plugin?
I've been using it for some time and I also used it to inject env to deployed Lambda functions. I'm okay with not including that functionality as it's still possible to explicitly pass env variables via environment. I believe it's always a possibility to reconsider supporting that in the future if there are a lot of requests from the community to do such thing.
I don't think we should support any options (following above arguments).
What is your opinion on basePath and path settings?
I would stick just to search in current working directory (same as we do not search for eventual service context in parent directories).
In components it's done that way, as there's a concept of multi components, where at root of a project you don't have serverless.yml, but there are folders with different components each having serverless.yml, and when deploying those, current working directory is changed to its folders (see PR that added support for that: https://github.com/serverless/components/pull/606) There's no such concept in the Framework
@medikoo the path would have been really useful to be honest 🙈. Would you accept a PR adding that feature if I were to submit?
This feature would really be useful in case such as a monorepo with multiple serverless apps that all can share same .env file
Activity
herebebogans commentedon Jul 6, 2020
There's a plugin that does this well already.
https://www.npmjs.com/package/serverless-dotenv-plugin
medikoo commentedon Jul 6, 2020
@herebebogans great thanks for info, I forgot about that one.
What's your opinion on having that in core?
One issue with a plugin is, that by nature of plugins it won't be loaded at begin of a process, but at some point of plugin initialization, which is after some env vars were already read by application code.
herebebogans commentedon Jul 7, 2020
Yes I'd like to see it in core.
Projects using dotenv and dotenv-expand are pretty common in the node ecosystem.
I'm not sure if you'd plan to use the same .env.${STAGE} autoloading feature that the plugin does?
medikoo commentedon Jul 7, 2020
At first I was purely thinking about just loading
.envbut I think support for.env.${stage}makes big sense as wellsjortiz commentedon Jul 18, 2020
I would like to work on this issue :).
Also would you like that in the case .env.${stage} doesn't exist make it try to load the .env as default ?
medikoo commentedon Jul 20, 2020
@sjortiz great to hear that!
I've updated Proposed solution in main description, with spec on how I believe it should work. Let me know if you have any concerns
colynb commentedon Jul 25, 2020
Hi, I developed the dotenv plugin which I created because it wasn't part of the core framework, so I'm happy to deprecate it if you move in this direction. The plugin does quite a bit to allow for some flexibility so just keep that in mind. Though if you don't want to have feature parity, the plugin could still be available to developers who need those extra features. Note, I JUST released a new major version last night that changes the way env files are resolved so if you're looking to do feature parity, please check that out.
colynb commentedon Jul 25, 2020
Also, keep in mind that a LOT of developers are using this plugin. If you move forward with this change it should probably be an opt in feature since it could potentially conflict with their existing setup. Just something to keep in mind.
medikoo commentedon Jul 27, 2020
Great thanks @colynb for jumping in!
We're definitely look into that
Yes, I thought about that, and I think we definitely should hide it behind an option in v1. I've added a point about that in main description
pgrzesik commentedon Oct 7, 2020
Hello there 👋 I'd love to give it a try over the weekend if that's okay 🙌
medikoo commentedon Oct 8, 2020
That'll be great @pgrzesik ! I believe it's one of the most valuable additions currently to consider
pgrzesik commentedon Oct 12, 2020
Hello 👋 I've started implementation and there are a few issues that I'm not sure about and I would love to clarify them below.
.envfiles, also injects all of them (withinclude/excludeoption for configuration) into deployed functions' environment. Should Serverless Framework support that?include/excludedepends on the answer to first question). If yes, how should it be configured? Below is my proposal, based on plugin config approachdotenvwould be a top-level setting,enabledcould be used to control if it should be used or not.In my opinion, support for
.envfiles should offer the same features as currently offered by the plugin in order to make it possible for people to migrate from it to the plain framework. If we only add loading variables for local resolution I'm afraid a lot of users will still depend on the plugin to inject variables into function environment.Let me know what do you think, feedback will be much appreciated 🙌
medikoo commentedon Oct 13, 2020
@pgrzesik that's a very good questions
I think dotenv plugin treats
.envmore as source of env vars for lambdas. While in this case we want to match behavior we already have implemented in Serverless Components. So.envsimply allows us to fillprocess.envfor deployment or otherserverlesscommand usage purposes. I believe it's also the more intended usage bydotenvpackage authors.As a side note: it's the step towards unification with Serverless Components (altough that'll be a long journey)
I see it also as replacement for our
--profileCLI andprovider.profile,provider.credentialssettings, which appeared to be problematicSo that means, that our implementation will mismatch
serverless-dotenv-pluginand that's intended, but we also should clearly document it as such, as I believe many users will be coming from a plugin, and without clear explanation will definitely be confused.What's your opinion on that? Have you been using dotenv plugin?
I'd say that if we want some env vars to go into lambdas, then we should explicitly configure them in
env, it's how it works now, and this is also approach we're taking in Serverless Components (which have dotenv support built-in)I don't think we should support any options (following above arguments).
I'd treat a
dotenvas a convenient and straightforwardprocess.envtransport, through which we can load different env vars depending on context we're in, and that's it.pgrzesik commentedon Oct 13, 2020
Hey @medikoo - thanks a lot for your responses. I've just reviewed the implementation in Serverless Components to have a better context. I totally agree with proper messaging to users that the feature-set between built-in
envsupport will be different than the one provided by the plugin.I've been using it for some time and I also used it to inject env to deployed Lambda functions. I'm okay with not including that functionality as it's still possible to explicitly pass env variables via
environment. I believe it's always a possibility to reconsider supporting that in the future if there are a lot of requests from the community to do such thing.What is your opinion on
basePathandpathsettings? They are used to control how.envfile will be resolved. I noticed that in components there's an approach to look for.envfiles in up to two parent directories. See https://github.com/serverless/components/blob/master/src/cli/index.js#L57-L62Should a similar approach be used here to provide consistent behavior?
medikoo commentedon Oct 14, 2020
I would stick just to search in current working directory (same as we do not search for eventual service context in parent directories).
In components it's done that way, as there's a concept of multi components, where at root of a project you don't have
serverless.yml, but there are folders with different components each havingserverless.yml, and when deploying those, current working directory is changed to its folders (see PR that added support for that: https://github.com/serverless/components/pull/606) There's no such concept in the Frameworkpgrzesik commentedon Oct 15, 2020
Thanks a lot for the explanation - I believe everything that I wasn't sure about is clear now 👍
shadrech commentedon Mar 10, 2022
@medikoo the
pathwould have been really useful to be honest 🙈. Would you accept a PR adding that feature if I were to submit?This feature would really be useful in case such as a monorepo with multiple serverless apps that all can share same
.envfilemnapoli commentedon Mar 10, 2022
@shadrech we are tracking this feature request in #10641, you can add a 👍 in that issue.