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

Created on 21 Aug 2019  路  2Comments  路  Source: aws/aws-sam-cli

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.

typfeature

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 --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.

All 2 comments

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

  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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

terrywarwar picture terrywarwar  路  26Comments

kyeljmd picture kyeljmd  路  31Comments

uvaisTrivecta picture uvaisTrivecta  路  40Comments

walkerlangley picture walkerlangley  路  41Comments

chrisfosterelli picture chrisfosterelli  路  31Comments