aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

feat(init): load .env file at startup of sam local

Open rynop opened this issue 5 years ago • 8 comments

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.

rynop avatar Aug 21 '19 19:08 rynop

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

sanathkr avatar Aug 22 '19 23:08 sanathkr

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):

  1. Create a .env file (format is standardized, @see any link above)
  2. Run sam local invoke|start-api|start-lambda --dotenv <path to .env>
  3. 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.

rynop avatar Aug 23 '19 02:08 rynop

Hello. Please, do you have any plan or temptative date to implement this new feature? Thanks in advance 🙂

bitcod3r avatar Apr 23 '21 08:04 bitcod3r

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

schematis avatar Dec 08 '21 00:12 schematis

This feature must have to keep secrets separate (locally) and be able push all project files to repository without them.

rez0n avatar Mar 25 '23 19:03 rez0n

Creating a --dotenv option would be a great idea, but how about trying to parse files passed to the --env-vars option as .env format if they're not in json format?

This way, there's no need to establish a new option. You should be able to just incorporate this code here and do some error handling.

itoyama avatar Jun 30 '23 00:06 itoyama