neverendingqs / serverless-dotenv-plugin Goto Github PK
View Code? Open in Web Editor NEWPreload Environment Variables with Dotenv into Serverless
License: MIT License
Preload Environment Variables with Dotenv into Serverless
License: MIT License
It would be great if the plugin could be configured to throw if a set of required environment vars are missing, either through an .env
file or any other means of setting environment vars.
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
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:
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
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
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?
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?
README says you can set some options like path, but i don't get how do you add these to the serverless.yaml file
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!
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
Hello,
Thank you for creating an amazing useful library.
Can I set some vars only for one(or more) functions in a project?
Thanks.
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]
Serverless Plugin Error --------------------------------------
Cannot create property 'DYANMODB_TABLE' on string '${file(./serverless-env.yml):environment}'
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
.
BTW, thank you so much for your hard work on this. I was wondering if it works with the python runtime
AWS - Invoke Local has an --env option which can be used as
serverless invoke local --env VAR1=val1 --env VAR2=val2
While this plugin has an option to define environment as a fall back to NODE_ENV with the same flag
serverless invoke local --env production
I think there is a conflict but to be honest, I haven't used either of these.
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
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!
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.
This plugin does require hooks.
From https://serverless.com/framework/docs/providers/aws/guide/plugins/:
Note: Variable references in the serverless instance are not resolved before a Plugin's constructor is called, so if you need these, make sure to wait to access those from your hooks.
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!
There is an error
Cannot read property 'path' of undefined
if no custom configuration for dotenv
is provided. It was fixed with 3391b24
Can you please relese this fix to npm?
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,
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?
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.
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'}
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!
Would be good have an exclude plugin option, allowing you to exclude some environment variables from been used and sent to the end Lambda function.
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?
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
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
testing?
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?
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
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
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
for example this works for me:
path: .env.dev
but this does not
path: .env.${'dev'}
@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
This would make it safer to contribute. If there are no objections, I would like to submit pull requests to add unit tests and a GitHub Actions workflow.
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).
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.
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.
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
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!
Include automatic detection of env files based on environment. For example
.env
.env.dev
.env.prod
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
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..
Would be nice to be able to have these encrypted before sending and not viewable in the web console.
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'.
I notice that .env
is being used when no --env
or --stage
is set. I would expect it to then default to the stage set in serverless.yml
.
provider:
stage: dev
When I do something like sls deploy
it is picking up .env
instead of .env.dev
. I have to explicitly do sls deploy -s dev
to pick up the right file.
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
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}"
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.