samconfig.toml env_vars are overridden by launch.json
See original GitHub issueDescribe the bug
When the extension is used to launch a SAM Lambda function using a launch.json configuration, environment variables for the Lambda function are overridden even if env_vars is configured in samconfig.toml.
Workarounds
-
Run the SAM commands manually, bypassing the toolkit. Environment variables configured in
samconfig.tomlare passed to Lambda as expected. This workaround gives up the ability to debug since we’re not using the toolkit.sam build --template Template.yaml --use-container # --config-file is required when samconfig.toml is not within the same directory as template.yaml sam local invoke MyFunction -t .aws-sam/build/template.yaml --config-file ../../samconfig.toml -
Configure
environmentVariableswithinlaunch.jsoninstead ofsamconfig.toml. This is per-function and also VS Code-specific.
To Reproduce
- Create
.vscode/launch.json:
{
"configurations": [
{
"type": "aws-sam",
"request": "direct-invoke",
"name": "project:MyFunction",
"invokeTarget": {
"target": "template",
"templatePath": "${workspaceFolder}/Template.yaml",
"logicalId": "MyFunction"
},
"lambda": {
"payload": {},
},
"sam": {
"localArguments": ["--config-file", "${workspaceFolder}/samconfig.toml"],
}
}
]
}
- Create
samconfig.toml
version = 0.1
[default.build.parameters]
# use_container required to avoid spurious errors such as
# Error: PythonPipBuilder:ResolveDependencies - list index out of range
use_container = "true"
[default.local_invoke.parameters]
# env_vars required to pass environment variables to the
# Lambda function image container when debugging locally.
env_vars = "env-vars.json"
- Create
env-vars.json
{
"Parameters": {
"S3_ENDPOINT_URL": "http://localstack:4566",
"SNS_ENDPOINT_URL": "http://localstack:4566",
"DYNAMODB_ENDPOINT": "http://localstack:4566"
}
}
Behaviour using Toolkit
$:~/repositories/project$ cat /tmp/aws-toolkit-vscode/vsctkG3cdmX/env-vars.json
{"MyFunction":{}}
print(os.environ.get('S3_ENDPOINT_URL'))
''
Behaviour using SAM CLI directly
print(os.environ.get('S3_ENDPOINT_URL'))
'http://localstack:4566'
Expected behavior
The Toolkit should not override environment variables set by samconfig.toml.
Desktop
- OS: Linux x64 5.4.72-microsoft-standard-WSL2
- Visual Studio Code Version: 1.59.0
- AWS Toolkit Version: 1.28.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
SAM deploy doesn't set environment variables #1163 - GitHub
Works fine for me when I run sam local invoke --env-vars env.json . --env-vars seems to override the parameters.
Read more >How should I inject env vars with sam build - Stack Overflow
I am using AWS SAM. I have created a samconfig.toml file with the following entry:
Read more >sam local invoke - AWS Serverless Application Model
-n , --env-vars PATH, The JSON file that contains values for the Lambda ... The default value is " samconfig.toml " in the...
Read more >sam local start-api - Amazon Serverless Application Model
The JSON file that contains values for the Lambda function's environment variables. --parameter-overrides, Optional. A string that contains Amazon ...
Read more >An Introduction to AWS Serverless Application Model
Running 'sam deploy' for future deployments will use the parameters saved above. The above parameters can be changed by modifying samconfig.toml.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

AWS Toolkit 1.29 includes a fix for this. @danw-mpl thank you for your helpful analysis!
Almost correct. I’m sure the “merge” behaviour could remain as-is. Just don’t send
--env-varswhenenvironmentVariablesisn’t present. This would mean we can use configuration fromsamconfig.toml.