Bug: Sam deploy Fails TypeError: unhashable type: 'dict' When passing !Ref or !Sub values into AWS::Serverless::Function Environment Variables
- Dominant language
- Python
- Stars
- 6.7k
- Forks
- 1.2k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 52
Description
### Description:
When passing in string substitution for AWS::Serverless::Function into the Environment Variables config SAM init throws a stack trace for YAML helper.
### Steps to reproduce:
sam deploy --resolve-s3 --template-file template.yaml --region us-east-1 --force-upload --config-env dev
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "lambda-${Environment}"
Environment:
Variables:
!Ref DynamoCacheDBTable: "7"
Architectures:
- !Ref LambdaArchitecture
This configuration does not work and also using !Sub throws the same result:
LambdaFunction:
Type: AWS::Serverless::Function
Properties:
FunctionName: !Sub "lambda-${Environment}"
Environment:
Variables:
!Sub "dynamo_cache_table_${Environment": 7
Architectures:
- !Ref LambdaArchitecture
The dynamo db table is defined as:
DynamoCacheDBTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "dynamo_table_${Environment}"
When explicitly setting the values for the environment variable to the table name:
dynamo_table_dev
The build succeeds without issue
There was a similar issue fixed here with the same result:
https://github.com/aws/serverless-application-model/issues/1094
### Observed result:
sam deploy --resolve-s3 --template-file template.yaml --region us-east-1 --force-upload --config-env dev --debug
Traceback (most recent call last):
File "samcli/yamlhelper.py", line 122, in yaml_parse
File "json/__init__.py", line 361, in loads
File "json/decoder.py", line 337, in decode
File "json/decoder.py", line 355, in raw_decode
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "samcli/__main__.py", line 12, in
File "click/core.py", line 829, in __call__
File "click/core.py", line 782, in main
File "click/core.py", line 1257, in invoke
File "click/core.py", line 700, in make_context
File "click/core.py", line 1048, in parse_args
File "click/core.py", line 1630, in handle_parse_result
File "click/core.py", line 123, in invoke_param_callback
File "samcli/commands/_utils/options.py", line 116, in get_or_default_template_file_name
File "samcli/commands/_utils/template.py", line 52, in get_template_data
File "samcli/yamlhelper.py", line 126, in yaml_parse
File "yaml/__init__.py", line 162, in safe_load
File "yaml/__init__.py", line 114, in load
File "yaml/constructor.py", line 51, in get_single_data
File "yaml/constructor.py", line 55, in construct_document
File "yaml/constructor.py", line 100, in construct_object
File "samcli/yamlhelper.py", line 113, in _dict_constructor
File "yaml/constructor.py", line 155, in construct_pairs
File "yaml/constructor.py", line 100, in construct_object
File "samcli/yamlhelper.py", line 113, in _dict_constructor
File "yaml/constructor.py", line 155, in construct_pairs
File "yaml/constructor.py", line 100, in construct_object
File "samcli/yamlhelper.py", line 113, in _dict_constructor
File "yaml/constructor.py", line 155, in construct_pairs
File "yaml/constructor.py", line 100, in construct_object
File "samcli/yamlhelper.py", line 113, in _dict_constructor
File "yaml/constructor.py", line 155, in construct_pairs
File "yaml/constructor.py", line 100, in construct_object
File "samcli/yamlhelper.py", line 113, in _dict_constructor
File "yaml/constructor.py", line 155, in construct_pairs
File "yaml/constructor.py", line 100, in construct_object
File "samcli/yamlhelper.py", line 113, in _dict_constructor
TypeError: unhashable type: 'dict'
[20810] Failed to execute script __main__
### Expected result:
Expected the lambda to deployed with the correct environment variable key/values using substitution or Reference
### Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
1. OS: lambda architecture is x86_64
2. `sam --version`: SAM CLI, version 1.59.0
3. AWS region: us-east-1
`Add --debug flag to command you are running`
sam deploy --resolve-s3 --template-file template.yaml --region us-east-1 --force-upload --config-env dev --debug
Traceback (most recent call last):
File "samcli/yamlhelper.py", line 122, in yaml_parse
File "json/__init__.py", line 361, in loads
File "json/decoder.py", line 337, in decode
File "json/decoder.py", line 355, in raw_decode
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "samcli/__main__.py", line 12, in
File "click/core.py", line 829, in __call__
File "click/core.py", line 782, in main
File "click/core.py", line 1257, in invoke
File "click/core.py", line 700, in make_context
File "click/core.py", line 1048, in parse_args
File "click/core.py", line 1630, in handle_parse_result
File "click/core.py", line 123, in invoke_param_callback
File "samcli/commands/_utils/options.py", line 116, in get_or_default_template_file_name
File "samcli/commands/_utils/template.py", line 52, in get_template_data
File "samcli/yamlhelper.py", line 126, in yaml_parse
File "yaml/__init__.py", line 162, in safe_load
File "yaml/__init__.py", line 114, in load
File "yaml/constructor.py", line 51, in get_single_data
File "yaml/constructor.py", line 55, in construct_document
File "yaml/constructor.py", line 100, in construct_object
File "samcli/yamlhelper.py", line 113, in _dict_constructor
File "yaml/constructor.py", line 155, in construct_pairs
File "yaml/constructor.py", line 100, in construct_object
File "samcli/yamlhelper.py", line 113, in _dict_constructor
File "yaml/constructor.py", line 155, in construct_pairs
File "yaml/constructor.py", line 100, in construct_object
File "samcli/yamlhelper.py", line 113, in _dict_constructor
File "yaml/constructor.py", line 155, in construct_pairs
File "yaml/constructor.py", line 100, in construct_object
File "samcli/yamlhelper.py", line 113, in _dict_constructor
File "yaml/constructor.py", line 155, in construct_pairs
File "yaml/constructor.py", line 100, in construct_object
File "samcli/yamlhelper.py", line 113, in _dict_constructor
File "yaml/constructor.py", line 155, in construct_pairs
File "yaml/constructor.py", line 100, in construct_object
File "samcli/yamlhelper.py", line 113, in _dict_constructor
TypeError: unhashable type: 'dict'
[20810] Failed to execute script __main__
Contributor guide
Assessment
This issue has not been assessed yet.