Giter Club home page Giter Club logo

serverless-dotenv-plugin's Issues

[FEATURE REQUEST] additional option - exclude

Hi here,

Thanks for creating this awesome plugin mate! Helped me a lot :)

I would like to know your thoughts regarding adding an additional option - exclude
It's helpful in my case since i just want to blacklist a list of ENVs that I don't want to use & push

[Feature request] Exclude stages

Hi there,

Thanks for this awesome plugin.

I am in a situation where I'd like to IGNORE completely .env files from some environments.

My use case is the following:
I am using .env file only in local, when using the offline plugin.
On "real", deployed environments, env vars are loaded via middy through the ssm middleware (see here).

I am not using ssm in local (offline) mode for many reasons, like:

  • network might not be available
  • each dev might need/want a different configuration
  • avoids unnecessary calls to ssm API

So, when offline, I am using this plugin to load them directly in process.env, and it works great.

This means that I do not want .env files to be loaded/used in stages other than local.

So far, I am working this around by having a .env.local file with my custom variables, and an empty default .env file for the rest.

When in local, I just do
sls offline start -s local --env local.

For the other stages, the empty .env file will just be used and it will just do nothing.

My workaround works.
However, I was wondering it it would make sense to add a new stages option in this plugin to select in which stages you want to load the .env file or not.

A bit like the include one, but per stage instead of per variable.

So, im my use case, I could do:

custom:
  dotenv:
    stages:
      - local

I am sure that others will also find this useful in some other use cases.
For example, one might not want to deploy to prod using a wrong .env file by mistake. One could easily type the wrong ---env or NODE_ENV values.
Excluding prod from the "allowed" stages might then be useful.

WDYT?

BTW, this is inspired by https://github.com/99xt/serverless-dynamodb-local that has this option

Fail silently

Locally I have .dotenv files, but the CI/CD pipelines use real environment variables. As a result each build ends up with this in the log

Serverless: DOTENV: Loading environment variables:

 Serverless Plugin Error --------------------------------------

  [serverless-dotenv-plugin] Could not find .env file.

It'd be nice if an option could be provided in serverless.yml to fail silently

Let users define dotenv plugins?

I am porting a project to serverless, where it has a number of dotenv plugins already in place, particularly dotenv-parse-variables we use there.

I see that you already added dotenv-expand plugin in the source code, do you think it's a good idea to expose an array in serverless.yml:custom.dotenv.plugins for users to define their own plugins?

${env:MYVAR} not working in serverless.yml

When I run in offline mode (serverless-offline) I'm unable to access the loaded env variables in my serverless.yml. For example in the config below, the ${env:STAGE} always rolls over to the default.

# serverless.yml
service: serverless-framework-starter

plugins:
  - serverless-dotenv-plugin
  - serverless-webpack
  - serverless-offline #serverless-offline needs to be last in the list

custom:
  webpack:
    webpackConfig: 'webpack.config.js'
    includeModules: true
    packager: 'npm'
  serverless-offline:
    port: 4000
  dotenv:
    path: "./.env.${opt:NODE_ENV, 'development'}"

provider:
  name: aws
  runtime: nodejs8.10
  stage: ${env:STAGE, 'dev'}
  region: ${env:REGION, 'us-east-1'}
  versionFunctions: false
  iamRoleStatements:
  -  Effect: "Allow"
     Action:
       - "s3:*"
     Resource: "*"

functions:
  test-func:
    handler: src/test-func/handler.default
    name: ${self:provider.stage}-${self:service}-upload
    memorySize: 128
    events:
      - http:
          path: hello
          method: get
          cors: true

The test handler has no trouble accessing SOMEVAL and it uses the proper value if I change the NODE_ENV at runtime.

// handler.js
const hello = (event, context, callback) => {
  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: `Go Serverless v1.0! Your function executed successfully! ENV: ${process.env.SOMEVAL}`,
      input: event,
    }),
  };

  callback(null, response);

  // Use this code if you don't use the http event with the LAMBDA-PROXY integration
  // callback(null, { message: 'Go Serverless v1.0! Your function executed successfully!', event });
};

export default hello;

It seems like a timing issue. It seems like the serverless.yml is processed before the serverless-dotenv-plugin is able to load the environment variables into scope. Any idea how I can prove this or whether this is the right concept?

How do you set the options?

README says you can set some options like path, but i don't get how do you add these to the serverless.yaml file

Safe to use in production

Hey,

Great plugin, easy to use!

I'm just wondering if this is secure to use for production? And if so, what makes it safe to use for production?

Cheers!

Unable to get .env with a dynamic path

I am trying to set dynamic .env files based on stage. The serverless is unable to find those .env files.

Here is my serverless.yml file:

custom:
   dotenv:
      path: environment/.env.${self:provider.stage}
provider:
  stage: ${opt:stage, 'dev'}

If the path is static as below, it is working just fine

custom:
       dotenv:
           path: environment/.env.dev

Expected behavior:
DOTENV should resolve path to environment/.env.dev

Current behavior:
Unable to resolve path

URLs ENVs output as [object Object] when using serverless-offline

When I set a URL in my .env.developement file and run using serverless-offline, I get the value as [object Object]

e.g.

ITEMS_TO_PARSE_QUEUE=https://sqs.eu-central-1.amazonaws.com/1234/items-to-parse

console.log(process.env.ITEMS_TO_PARSE_QUEUE); // returns [object Object]

Reference to env vars sitting outside main config yml causes error

Error:

 Serverless Plugin Error --------------------------------------

  Cannot create property 'DYANMODB_TABLE' on string '${file(./serverless-env.yml):environment}'

configs:

serverless.yml

service: my-service

plugins:
  - serverless-dotenv-plugin

provider:
  name: aws
  runtime: nodejs8.10
  environment: ${file(./serverless-env.yml):environment}

serverless-env.yml

environment:
  SLS_AWS_STAGE: ${env:DYANMODB_TABLE, 'xxvvv'}

No errors when moving environment to outside the provider. Verified that it's working perfectly when it's located outside provider.

Cannot create property error

Keep getting this error
Screen Shot 2020-04-02 at 18 52 14
My custom section looks like

custom:
  globals: ${file(../../serverless.globals.yml)}

with serverless.globals.yml also referring to env variables I set in .env (STAGE being one of them)
Serverless successfully deploys the service but not sure how to get rid of this error

"app" value in Serverless.yml not working

Hello,

I tried using Serverless enterprise with the following configuration;
service: ${env:APP_NAME}
...
app: ${env:SLS_APP_NAME}

However, it seems like ${env:SLS_APP_NAME} isn't being replaced by its value. While I replace it manually, everything works fine.
I can confirm it's not due to the variable not being in the .env, or the wrong environment.

Is this a known bug?

Thanks,
Will

1.2.0 breaks serverless interpolation

Hi there,

I love your plugin and I use it for my current pet project. I guess something has broken in the last version. This works in 1.1.5

custom:
  dotenv:
    path: .${self:provider.stage}.env

It will load stage specific files like .dev.env.
In 1.2.0 it can't find the file anymore. envPath is just .${self:provider.stage}.env. I think this related to change when the plugin loads the env file.

Thanks!

Not setting ENV vars on Lambda, v1.1.2

This plugin would read the env file but the env variables would not be set on the AWS lambda function. Maybe it was conflicting with other plugins.
I managed to fix it by adding the hook:
"before:package:initialize": this.loadEnv.bind(this)
to this.hooks in the constructor in index.js.

Maybe this would help anyone.
I'm not convinced this is an universal solution as to create a pull request.

possible bug - environment variable value being cut

I have an ENV variable, say RANDOM_HASH in my .env file

.env

RANDOM_HASH="XXXXX#XXXXX%9^XX$4$FYNHtpPs6uc"

and on
serverless.yml

custom:
  dotenv:
    include:
      - RANDOM_HASH

when i deploy it, what reaches my lambda function only is XXXXX#XXXXX%9^XX
Somehow, it has been cut in the first $

same thing is found on the cloudformation-template-update-stack.json file

        "Environment": {
          "Variables": {
            "RANDOM_HASH": "XXXXX#XXXXX%9^XX"
          }
        }

so i suspect that the library somehow accidentally split the content.
Do you have some work around in mind, so that I can upload the same env variable value?

Thanks in advance!

[QUESTION] Serverless Hooks

Hi,
What's the reason behind the hooks on serverless stages?
If I need the env variables in other hooks rather than the hardcoded in the plugin class, what's the point to have hooks hardcoded?

Why not just to load the env variables regardless of the current serverless stage?

Thanks in advance,

Name resolution stop working when I set `path` and `basePath` options

I configured this plugin in serverless.yml as below:

custom:
  dotenv:
      basePath: config
      path: config/.env

I have some env files under config directory:

config/.env
config/.env.joey

when I deploy with a stage name joey, e.g. sls deploy --stage joey, it still loads file config.env. It doesn't look at config.env.joey. The output is always:

Serverless: DOTENV: Loading environment variables from config/.env:

If I remove the option basePath and path from serverless.yml file and put these env files in the default location which is ./. Everything works fine. Could you support automatic name resolution with customised path?

Does this work for `serverless offline`

I'm trying to use this for testing/offline/deployment. Does this work if I run serverless in offline mode? If I remove the .env file and run serverless offline start it doesn't throw an error. That leads me to believe it isn't being run in offline mode.

The --stage parameter ignored when looking for the right .env file

I'm running the serverless script with the next command line:
client deploy --stage prod --region eu-west-1 --aws-profile MyProfile

As you see I'm specifying the --stage parameter, which according to this documentation should use the .env.prod file.

But I'm getting this message:
Serverless: DOTENV: Could not find .env file.

All .env files are in sub-folder envs and here is my serverless config:

service: my-sevice-${opt:stage, self:provider.stage}

frameworkVersion: ">=1.1.0 <2.0.0"

plugins:
  - serverless-finch
  - serverless-dotenv-plugin

custom:
  dotenv:
    basePath: ./envs/
    include:
      - MY_VARIABLE

  ...

provider:
  name: aws
  runtime: nodejs12.x
  stage: ${opt:stage, 'develop'}

Azure support?

Hello!
I'm trying to migrate from AWS to Azure cloud.
DotEnv plugin is amazing, but I cannot figure it out if I can use it or not :(

Anyone can help?
Thank you very much!

Try automatic resolution based on path and not load from the path

So I have this monorepo setup where I want to have my .env.development and .env.production at the root level and reference the required variables in my service folders.
Right now the path variable tries to load it directly from the file, without trying to find the file.

The issue with this approach is that if I hardcode what is my .env right now. I might have to change the path name in each service folder to production and then deploy. Which is cumbersome and feels wrong.

What I would like an option as readFrom and given a path and the whole thing will run on that path instead of the current repo.

What do you guys think around this? Anybody faced same issue? I Might want to put in a PR on this but need suggestions from collaborators, also is there anyway we can currently do this that I might be missing?

Support for local env values or multiple paths

create-react-app has an awesome environment variables resolution pattern:
https://create-react-app.dev/docs/adding-custom-environment-variables/#what-other-env-files-can-be-used
It allows for users to define .env.development (tracked by git) and .env.development.local (not committed to the repo), .env which works on all envs, etc.

Another issue proposed to allow multiple paths, but in another context: #29.

This way, you could perhaps configure the plugin like this (given path supports variables: #52):

custom:
  dotenv:
    path: 
        - .env
        - .env.${self:stage}
        - .env.${self:stage}.local

The plugin would load each path sequentially, overriding envs as it goes.

Alternatively, the plugin would do these resolutions automatically.

This is the implementation on create-react-app. It takes into account several framework-specific things, but the main part, which applies here, is this: https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/config/env.js#L26-L49

EDIT

Apparently, this behaviour was taken from Rails:
https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use

I made the change on a fork of mine

How to *NOT* load into Lambda?

I further suffix env vars I load from files in serverless.yaml, so I don't want the original vars loaded into Lambda. Is there an option to turn that off?

Using serverless-dotenv-plugin increases lambda package size

Hey guys, sorry to bother you, I really enjoyed how easy it is to use the plugin. Nevertheless, I'm having issues trying to deploy bc my package's final size is pretty big, over 270MB. That being said, I don't know if it has anything to do with the plugin, but when I remove it, it goes down to 405KB ๐Ÿ˜ฌ

./node_modules/.bin/sls --version
Framework Core: 2.18.0 (local)
Plugin: 4.4.2
SDK: 2.3.2
Components: 3.4.6

Fragment of my serverless.yml (although there's not much to look there)

service: xxxxx

frameworkVersion: ^2.16.1

plugins:
  - serverless-dotenv-plugin

custom:
  cors:
    allowCredentials: false
    origin: '*'
    headers:
      - Authorization
      - Content-Type
      - X-Amz-Date
      - X-Api-Key
      - X-Amz-Security-Token
      - X-Amz-User-Agent

package:
  excludeDevDependencies: true
  exclude:
    - .vscode
    - docker
    - .git/**

Let me know if it's not an issue with the plugin so I can file another issue. Let me know if I can help with anything

Multiple env files

How do I import both a common env file (without the env suffix), and a env file, e.g. this doesn't seem to work:

custom:
  dotenv:
    path: ../common.env:../site.env.dev

Environment Vars Don't Update In AWS Console

Hi, I am using this plugin to set my environment variables. It works with "serverless offline", but when I deploy none of the environment variables are updated. why is this? thanks

Native support for .env in Serverless Framework

@colynb great thanks for this plugin!

We're currently investigating a possibility of supporting .env file natively in a Framework.

Main reason is that ideally env vars from .env should be loaded at very begin of a process, and resolution via plugin is delayed simply by its nature. So taking into account dotenv popularity I believe it's a good call to make it a first class citizen in the Framework (it's also already supported in Serverless Components)

Also we simply plan to support it it as convenient env vars transport into process.env and nothing more (no automatic leak into lambdas etc.)

We will value your feedback at serverless/serverless#7907

Just to say thanks

Just want to say, great work with this. Was thinking I would have to implement it myself, till I stumbled on this. I'm also willing to volunteer as a maintainer if you ever need help.

Also, what do you think of getting this into the core framework? It's a terribly useful feature because for anyone using version control, managing secrets is hell (copy them out from your serverless.yml before you commit).

Google / GCP Support?

Does this work for GCP? I am using it for AWS Lambdas and it works, but just wondering if it works for other platforms like Google or Azure.

I saw another issue #40 that Azure might not be supported yet.

Create CHANGELOG.md file in project root

Overview

As a user, I would like to see a CHANGELOG.md file in the project root that is up to date with the current release.

I see there is a changelog on your private blog, but it is missing the latest version bump (3.1.0), and it would be nicer for the library if that lived inside the project repo.

Examples

Here are a couple of changelog file examples, I don't think an automatic changelog generator is necessary, but the format is common and easy to read.

https://github.com/github-changelog-generator/Github-Changelog-Generator/blob/master/CHANGELOG.md
https://github.com/olivierlacan/keep-a-changelog/blob/master/CHANGELOG.md

Question: Interpolation inside of .env* files?

Hey,

first of all: Thank you for your work on this plugin. We just started implementing a serverless api and need env-var support. Unfortunately, we need access to Cloudformation-Outputs inside of the env vars, e.g. for a Subnet-ID.
What we did so far is more of a workaround by specifying the env vars multiple times:

custom:
  stage: "${opt:stage,'dev'}",
  env:
    dev:
      dbHost: "127.0.01"
    prod: 
      dbHost: "${cf:Stack.dbHost}"

...
functions:
  test:
  ..
    environment: 
      dbHost: "self:custom.env.${opt.stage}.dbHost

(no guarantees on correct indentation ๐Ÿ˜„ )
We would love to manage all of the env variables in their dedicated files and just have a ${env:dbHost} in the functions section without the ugly repetition in custom . Is there a way to achieve this?

Thanks in advance!

Location of changelog

Hi, I was updating from a previous version to the latest version of this plugin. Is there a link where I can see the changelogs?
Thanks

Does this support stages?

I have env specific var files. I.e. .env.dev, .env.prod. I'd like to use the stage to determine which file to load..

Read issues when value has '$' character

I have observed an issue while reading a value that contains '$' character.
While reading the value using 'process.env', the $ character, as well as the next few characters, are missing.

As an example, consider the below line in .env.local file:

TEST_1=123$456!@#%^&*()

While reading I see that
process.env.TEST_1 = 123!@#%^&*() // A total of four characters are missing from the original value which are '$456'.

Missing custom property in serverless config causes an error

SLS_DEBUG=* sls invoke local -f handler -p event.js

Results in no .env loaded and the following error

Serverless: DOTENV: Loading environment variables:
 Serverless Plugin Error --------------------------------------

  Cannot read property 'dotenv' of undefined

Appears to be caused by

var config = this.serverless.service.custom['dotenv'];

Because I don't have a custom section in my serverless.yml.

Temporary workaround

custom:
  dotenv:
    see: https://github.com/infrontlabs/serverless-dotenv-plugin/issues/5

plugin ignores custom definitions when places in an external file

My serverless.yml references an external file:

custom: ${file(../../serverless.common.yml):custom}

Placing settings inside serverless.common.yml:

custom:
  dotenv:
    path: ../../.env

serverless.dotenv-plugin ignores these settings.
When debugging plugin code, row 13 leaves this.config undefined:

    this.config =
      this.serverless.service.custom && this.serverless.service.custom['dotenv']

because this.serverless.service.custom equals: "${file(../../serverless.common.yml):custom}"

[aws codebuild] dotenv is not working

I am using serverless and serverless dotenv plugin.

And it is working properly.

But, our team move to aws codepipeline and codebuild for ci/cd.

Our project's ignore .env.* files to push github.

So, using codebuild envrionment variales.

But, it is not working.

I run this command in codebuild

- printenv

And injected environment variables showing.

But node.js process.env has not.

How to inject aws codebuild environment variables to node.js process.env using serverless-dotenv-plugin?

I tried below comman in codebuild

- printenv > .env.production

But, it is not working.

build size is just 4.98MB but, lambda upload size too large error occured.

how to do...?

my setting info

node.js : 10.15
aws buildspec : 0.2
serverless framework with serverless-webpack-plugin and serverless-dotenv-plugin ( with node dotenv npm )

And push my .env files to github, it is working properly.

So, I think that serverless-dotenv-plugin is not using aws codebuild env variables

Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.