aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.51k stars 1.17k forks source link

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

Open rynop opened 5 years ago

rynop commented 5 years ago

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.

sanathkr commented 5 years ago

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

rynop commented 5 years ago

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.

bitcod3r commented 3 years ago

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

schematis commented 2 years ago

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

rez0n commented 1 year ago

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

itoyama commented 1 year ago

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.

andrewasfa commented 5 months ago

can't believe this is still open.

paul-uz commented 2 months ago

5 years and still nothing? .env support should be a no brainer.