It would be great if sam local loaded env vars from a .env file instead of a non-standard --env-vars JSON file.
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):
.env file (format is standardized, @see any link above)sam local invoke|start-api|start-lambda --dotenv <path to .env>--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.
Most helpful comment
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
--dotenvCLI option):.envfile (format is standardized, @see any link above)sam local invoke|start-api|start-lambda --dotenv <path to .env>--dotenvwas specified, sam local runtime loads the contents of the.envfile specified into the execution environment. I know I've seen output from the sam local container at startup, state that it saw a.envand 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
.envfile as they are now in the execution environment - for ex just as thePATHenv var is avail to any lambda regardless of language/runtime.