aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

Bug: Sam deploy Fails TypeError: unhashable type: 'dict' When passing !Ref or !Sub values into AWS::Serverless::Function Environment Variables

Open joeterlecki opened this issue 3 years ago • 3 comments

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

joeterlecki avatar Oct 07 '22 16:10 joeterlecki

I was able to reproduce this, the stack trace is due to the fact that the intrinsics to be resolved in the present in the "Key" section of the environment variables, switching it so that its present in the values makes the error message more direct.

eg:

Error: Failed to create changeset for the stack: myapp-4285, ex: Waiter ChangeSetCreateComplete failed: Waiter encountered a terminal failure state: For expression "Status" we matched expected path: "FAILED" Status: FAILED. Reason: Template format error: Unresolved resource dependencies [Environment] in the Resources block of the template

sriram-mv avatar Oct 20 '22 20:10 sriram-mv

any update on this issue?

AffiTheCreator avatar Feb 06 '24 12:02 AffiTheCreator