Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dotenv support #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

haddowg
Copy link

@haddowg haddowg commented Nov 1, 2024

Lambda environment variables have a default 4 KB service quota that can't be increased.

If using a large enough number of runtime-loaded secrets it is easy to hit this limit. We did so at around 77 env vars, your mileage may vary depending on the length of env names and ssm paths.

To support an arbitrary number of variables this PR adds dotenv support to the secrets loader.

The loader will gather env from the system environment as usual and then load and merge variables from a dotenv file before retrieving and replacing any secret values necessary from ssm.

Dotenv is used in its immutable mode meaning values in the dotenv file will not replace/overwrite existing variables.

Since this is also the default/suggested behavior when dotenv used in popular frameworks like laravel and symfony there is no risk of conflict if a framwork later reloads the same dotenv file, the retrieved secret values will not be replaced with the ssm paths again.

By default it will look for dotenv files in the LAMBDA_TASK_ROOT path, however, you can define BREF_ENV_PATH to change this.
The file loaded will be determined by looking for an environment name in BREF_ENV, falling back to the popular APP_ENV. if a value is found we will attempt to load .env.{environment name} and fallback to .env else it will attempt to load .env only, missing files will be safely ignored.

These BREF_ENV_PATH, BREF_ENV and APP_ENV values would need to remain defined on the lambda itself if required and not in a dotenv file, but all other environment values could be moved to a dotenv.

N.B. due to a change required in my related PR on the bref repo unless/until that change is merged you would also need at least one environment variable defined on the lambda itself that has a bref-ssm:... value in order to trigger the secrets loading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant