Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set different/separated env vars per function? #35

Closed
delgermurun opened this issue Apr 8, 2019 · 10 comments
Closed

How to set different/separated env vars per function? #35

delgermurun opened this issue Apr 8, 2019 · 10 comments
Labels

Comments

@delgermurun
Copy link

Hello,
Thank you for creating an amazing useful library.

Can I set some vars only for one(or more) functions in a project?

Thanks.

@redlickigrzegorz
Copy link

It's a very good question. I didn't find any possibility to set this with the usage of this library that's why I decided to create my own config.js file to keep all the needed env variables and put them where I want during the work with serverless 😃

@colynb is it possible to do this via your library?

@colynb
Copy link
Collaborator

colynb commented May 7, 2019

Would you mind describing a use-case/scenario so that I can get a better understanding? Thanks

@redlickigrzegorz
Copy link

@colynb Yes, of course, and I believe that it will be a simple example 😉

functions:
  sample-funtion-a:
    handler: src/handler.sampleFunctionA
    environment:
      SAMPLE_A: ${env:SAMPLE_A}
  sample-funtion-b:
    handler: src/handler.sampleFunctionB
    environment:
      SAMPLE_B: ${env:SAMPLE_B}

With the current functionality, we will get SAMPLE_A and SAMPLE_B in the environment section of both functions.

@ChristopheBougere
Copy link

I'm having a similar use case: I want to load env variables in my fargate task definition, but not in my lambda functions.
Your snippet should work if you add this setting:

custom:
  dotenv:
    include: [] # Do not inject env variables into lambdas

Having an option to disable auto injection could be great by the way :)

@tommykamkcm
Copy link

@colynb Yes, of course, and I believe that it will be a simple example 😉

functions:
  sample-funtion-a:
    handler: src/handler.sampleFunctionA
    environment:
      SAMPLE_A: ${env:SAMPLE_A}
  sample-funtion-b:
    handler: src/handler.sampleFunctionB
    environment:
      SAMPLE_B: ${env:SAMPLE_B}

With the current functionality, we will get SAMPLE_A and SAMPLE_B in the environment section of both functions.

Hello everyone, I have additional questions about this scenario:

Assuming there are 10 env vars declared in .env e.g. SAMPLE_A ... SAMPLE_J
What happens to sample-funtion-a if we declare:

  • SAMPLE_C ... SAMPLE_J under custom > dotenv > include, and also

  • SAMPLE_A under sample-function-a

will sample-function-a get all the env vars except SAMPLE_B?

@krlozadan
Copy link

Hi, any updates on this?

@colynb Yes, of course, and I believe that it will be a simple example 😉

functions:
  sample-funtion-a:
    handler: src/handler.sampleFunctionA
    environment:
      SAMPLE_A: ${env:SAMPLE_A}
  sample-funtion-b:
    handler: src/handler.sampleFunctionB
    environment:
      SAMPLE_B: ${env:SAMPLE_B}

With the current functionality, we will get SAMPLE_A and SAMPLE_B in the environment section of both functions.

This will definitely be very helpful

@colynb
Copy link
Collaborator

colynb commented Feb 2, 2020

@krlozadan sorry for the VERY late response. I've been using today to get caught up on this plugin. As I mentioned in a few other issues here recently, I'm extremely nervous about accidentally introducing breaking changes so I erred on the side of simply ignoring these requests.

With that said, this is definitely one of the trickier requests to implement. I'm not sure how I would even approach it.

@neverendingqs
Copy link
Owner

@colynb Yes, of course, and I believe that it will be a simple example wink

functions:
  sample-funtion-a:
    handler: src/handler.sampleFunctionA
    environment:
      SAMPLE_A: ${env:SAMPLE_A}
  sample-funtion-b:
    handler: src/handler.sampleFunctionB
    environment:
      SAMPLE_B: ${env:SAMPLE_B}

With the current functionality, we will get SAMPLE_A and SAMPLE_B in the environment section of both functions.

Hello everyone, I have additional questions about this scenario:

Assuming there are 10 env vars declared in .env e.g. SAMPLE_A ... SAMPLE_J
What happens to sample-funtion-a if we declare:

  • SAMPLE_C ... SAMPLE_J under custom > dotenv > include, and also
  • SAMPLE_A under sample-function-a

will sample-function-a get all the env vars except SAMPLE_B?

I am 90+% sure of the following. Please test my statements below if you would like to be 100% sure.

Environment variables defined with dotenv end up at the project level according to

Object.keys(envVars).forEach((key) => {
this.log('\t - ' + key)
this.serverless.service.provider.environment[key] = envVars[key]
.

In your example, sample-function-a will include SAMPLE_B ... SAMPLE_J from the dotenv file(s) because it pulls it from the project level.

Furthermore, environment variables with the same name defined at the function name takes precedence over ones defined at a project level. This means sample-function-a's SAMPLE_A value is whatever you declare it to be. In your example, because you set it to ${env:SAMPLE_A}, sample-function-a's SAMPLE_A value will be the same as the one in the dotenv file(s).

@neverendingqs
Copy link
Owner

I'm having a similar use case: I want to load env variables in my fargate task definition, but not in my lambda functions.
Your snippet should work if you add this setting:

custom:
  dotenv:
    include: [] # Do not inject env variables into lambdas

Having an option to disable auto injection could be great by the way :)

This is my recommendation as well. Please see #89 for details.

@neverendingqs
Copy link
Owner

I think all questions have been answered. Because there are a variety of questions here, if you have a follow-up question or a related question, please post it in https://github.com/neverendingqs/serverless-dotenv-plugin/discussions/categories/q-a.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants