Skip to content

Support loading env variables from .env files #7907

Closed

Description

@medikoo

Use case description

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

// set the options and commands which were processed by the CLI

Therefore at this point:

if useDotenv was set to true
  1. Populate process.env with any variables in eventual .env file
  2. Populate process.env with any variables in eventual .env.${stage} we can resolve stage via:
this.processedInput.options || _.get(this.pluginManager.serverlessConfigFile, 'provider', 'stage') || 'dev'
if useDotenv was not set

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

Activity

  1. self-assigned this
    on Jul 6, 2020
  2. herebebogans commented on Jul 6, 2020

    @herebebogans
    Contributor
  3. medikoo commented on Jul 6, 2020

    @medikoo
    ContributorAuthor

    @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.

  4. herebebogans commented on Jul 7, 2020

    @herebebogans
    Contributor

    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?

  5. medikoo commented on Jul 7, 2020

    @medikoo
    ContributorAuthor

    I'm not sure if you'd plan to use the same .env.${STAGE} autoloading feature that the plugin does?

    At first I was purely thinking about just loading .env but I think support for .env.${stage} makes big sense as well

  6. sjortiz commented on Jul 18, 2020

    @sjortiz

    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 ?

  7. medikoo commented on Jul 20, 2020

    @medikoo
    ContributorAuthor

    I would like to work on this issue :)

    @sjortiz great to hear that!

    Also would you like that in the case .env.${stage} doesn't exist make it try to load the .env as default ?

    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

  8. colynb commented on Jul 25, 2020

    @colynb

    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.

  9. colynb commented on Jul 25, 2020

    @colynb

    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.

  10. medikoo commented on Jul 27, 2020

    @medikoo
    ContributorAuthor

    Great thanks @colynb for jumping in!

    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

  11. removed their assignment
    on Oct 7, 2020
  12. added
    deprecation
    on Oct 7, 2020
  13. pgrzesik commented on Oct 7, 2020

    @pgrzesik
    Contributor

    Hello there 👋 I'd love to give it a try over the weekend if that's okay 🙌

  14. medikoo commented on Oct 8, 2020

    @medikoo
    ContributorAuthor

    That'll be great @pgrzesik ! I believe it's one of the most valuable additions currently to consider

  15. pgrzesik commented on Oct 12, 2020

    @pgrzesik
    Contributor

    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.

    1. Injecting variables into Lambda (or another) environment - https://github.com/colynb/serverless-dotenv-plugin 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?
    2. Configuration - plugin mentioned below offers configuration options (ref: https://github.com/colynb/serverless-dotenv-plugin#plugin-options) - should framework also support such configuration options (support for include/exclude depends on the answer to first question). If yes, how should it be configured? Below is my proposal, based on plugin config approach
    dotenv:
      enabled: true 
      path: path/to/my/.env 
      basePath: path/to/ 
    

    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 🙌

  16. medikoo commented on Oct 13, 2020

    @medikoo
    ContributorAuthor

    @pgrzesik that's a very good questions

    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.

  17. pgrzesik commented on Oct 13, 2020

    @pgrzesik
    Contributor

    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? They are used to control how .env file will be resolved. I noticed that in components there's an approach to look for .env files in up to two parent directories. See https://github.com/serverless/components/blob/master/src/cli/index.js#L57-L62

    Should a similar approach be used here to provide consistent behavior?

  18. medikoo commented on Oct 14, 2020

    @medikoo
    ContributorAuthor

    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

  19. pgrzesik commented on Oct 15, 2020

    @pgrzesik
    Contributor

    Thanks a lot for the explanation - I believe everything that I wasn't sure about is clear now 👍

  20. shadrech commented on Mar 10, 2022

    @shadrech
    Contributor

    @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

  21. mnapoli commented on Mar 10, 2022

    @mnapoli
    Contributor

    @shadrech we are tracking this feature request in #10641, you can add a 👍 in that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

cat/aws-authcat/clideprecationenhancementgood first issuehelp wanted

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions