question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

samconfig.toml env_vars are overridden by launch.json

See original GitHub issue

Describe 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.toml are 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 environmentVariables within launch.json instead of samconfig.toml. This is per-function and also VS Code-specific.

To Reproduce

  1. 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"],
            }
        }
    ]
}
  1. 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"
  1. 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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
justinmk3commented, Aug 19, 2021

AWS Toolkit 1.29 includes a fix for this. @danw-mpl thank you for your helpful analysis!

0reactions
danw-mplcommented, Aug 16, 2021

Almost correct. I’m sure the “merge” behaviour could remain as-is. Just don’t send --env-vars when environmentVariables isn’t present. This would mean we can use configuration from samconfig.toml.

Read more comments on GitHub >

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

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found