Skip to content

feat: add .env file format support for --env-vars option#8746

Merged
licjun merged 5 commits intodevelopfrom
feat-dotenv
Mar 6, 2026
Merged

feat: add .env file format support for --env-vars option#8746
licjun merged 5 commits intodevelopfrom
feat-dotenv

Conversation

@licjun
Copy link
Copy Markdown
Contributor

@licjun licjun commented Mar 6, 2026

Which issue(s) does this change fix?

#1355

How does it address the issue?

The --env-vars option now auto-detects the file format. When a file is provided:

  1. It first attempts to parse the file as JSON (existing behavior, backward compatible)
  2. If JSON parsing fails, it falls back to .env format using the python-dotenv library
  3. Variables from .env files are treated as global parameters (wrapped in {"Parameters": {...}} structure)
  4. If the file is neither valid JSON nor valid .env (no key-value pairs found), an error is raised

Example usage:
bash

JSON still works as before

sam local invoke MyFunction --env-vars env.json

.env files now also work

sam local invoke MyFunction --env-vars .env

Note: .env files only support global variables (applied to all functions). For function-specific variables, continue using JSON format.

What side effects does this change have?

  • Adds python-dotenv~=1.0.0 as a new dependency
  • Files that previously failed as invalid JSON may now succeed if they are valid .env format. This is intentional and more permissive behavior.
  • No breaking changes — existing JSON files work exactly as before

Mandatory Checklist

PRs will only be reviewed after checklist is complete

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@licjun licjun requested a review from a team as a code owner March 6, 2026 17:47
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I personally think this needs an integration test. I'd be satisfied with unit tests confirming the behavior is the same across file formats. Open to what others think, though. This is not a blocking comment.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. The integration test exercises the same code path as the global JSON env vars test — the only difference is the file parsing, which is already covered by unit tests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That being said, I could see the value in it. If another reviewer feels strongly, I'd approve either way.

@licjun licjun added this pull request to the merge queue Mar 6, 2026
Merged via the queue into develop with commit 4302fae Mar 6, 2026
46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants