aws-sam-cli
aws-sam-cli copied to clipboard
feat(init): load .env file at startup of sam local
Describe your idea/feature/enhancement
It would be great if sam local
loaded env vars from a .env
file instead of a non-standard --env-vars
JSON file.
Proposal
Create an option --use-dotenv
that will tell the docker container to load env vars from a .env
file prior to launching the lambda. Vars defined in .env
will apply to ALL lambda functions defined in the sam template.yml
This will eliminate the need to dual maintenance a .env
and a sam-env.json
file. A .env
file is needed for local dev that does not need to run through a lambda simulator (for more rapid development iterations).
--env-vars
is still useful for variables that are specific to an individual lambda.
Thanks for the feature request.
The .env
files look like a docker-compose thing, but SAM CLI doesn't use docker-compose. How do you envision this --use-dotenv
flag to work? It would be good if you can describe with a more detailed example
Thanks. dotenv (aka .env
) is not a Docker thing. It is a pretty common practice across most popular languages: node, go, python etc.
Flow of how it would work (I propose a shorter --dotenv
CLI option):
- Create a
.env
file (format is standardized, @see any link above) - Run
sam local invoke|start-api|start-lambda --dotenv <path to .env>
- Upon launch of sam docker container, because
--dotenv
was specified, sam local runtime loads the contents of the.env
file specified into the execution environment. I know I've seen output from the sam local container at startup, state that it saw a.env
and to install some python module to read it - so I'm guessing sam local is using python under the covers. If so, just leverage https://pypi.org/project/python-dotenv/
Code running inside sam local container, can now access any of the variables defined in the .env
file as they are now in the execution environment - for ex just as the PATH
env var is avail to any lambda regardless of language/runtime.
Hello. Please, do you have any plan or temptative date to implement this new feature? Thanks in advance 🙂
I'm coming from the serverless framework and they've implemented this so that you can do ${env:ENV_VAR}
in the template and it will pull from either dotenv or an environment variable. This is a standard methodology for keeping secrets out of code. Another example of this are react apps created by create react app or any other js app. https://www.npmjs.com/package/dotenv
This feature must have to keep secrets separate (locally) and be able push all project files to repository without them.