Sam local invoke results in Error: unhashable type: 'collections.OrderedDict' for CDK created lambda with Imported Value in function name
- Dominant language
- Python
- Stars
- 6.7k
- Forks
- 1.2k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 52
Description
### Description:
We have created a lambda using aws_lambda.Function class in the python CDK. When we try and synth that object, it works, but running it results in an Unhashable type error. I believe this has something to do with the fact that the function name in the generated template does an ImportValue operation.
Here is the aws_lambda.Function cdk generated template:
```.yaml
Metadata:
our_company-cdk-lib: our_company_cdk.our_company_lambda.Function
Resources:
functionlambdaServiceRoleF834821F:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Statement:
- Action: sts:AssumeRole
Effect: Allow
Principal:
Service: lambda.amazonaws.com
Version: "2012-10-17"
ManagedPolicyArns:
- Fn::Join:
- ""
- - "arn:"
- Ref: AWS::Partition
- :iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
Metadata:
aws:cdk:path: user-test-stack/functionlambda/ServiceRole/Resource
functionlambdaEDFDB073:
Type: AWS::Lambda::Function
Properties:
Code:
S3Bucket:
Fn::Sub: cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}
S3Key: bb6e05f01acd0af30f089eed0751198cbff29e3408aab40971d26a9d3cb86e21.zip
Environment:
Variables:
environment:
Fn::ImportValue: our_company-cdk-vars-env
FunctionName:
Fn::Join:
- ""
- - Fn::ImportValue: our_company-cdk-vars-env
- -functionlambda
Handler: app.lambda_handler
MemorySize: 128
Role:
Fn::GetAtt:
- functionlambdaServiceRoleF834821F
- Arn
Runtime: python3.10
Timeout: 30
DependsOn:
- functionlambdaServiceRoleF834821F
Metadata:
aws:cdk:path: user-test-stack/functionlambda/Resource
aws:asset:path: asset.bb6e05f01acd0af30f089eed0751198cbff29e3408aab40971d26a9d3cb86e21
aws:asset:is-bundled: false
aws:asset:property: Code
Parameters:
BootstrapVersion:
Type: AWS::SSM::Parameter::Value
Default: /cdk-bootstrap/hnb659fds/version
Description: Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]
Rules:
CheckBootstrapVersion:
Assertions:
- Assert:
Fn::Not:
- Fn::Contains:
- - "1"
- "2"
- "3"
- "4"
- "5"
- Ref: BootstrapVersion
AssertDescription: CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI.
```
### Steps to reproduce:
Using CDK:
1. create a simple hello world lambda in python using `sam init`
2. delete the generated template file and instead create a cdk stack.py with an aws_lambda.Function construct pointed to the new lambda.
3. for the function_name parameter, import a value into the name, example:
```python
lambdaf = aws_lambda.Function(self, 'functionlambda', function_name = f'{Fn.import_value("our_company-cdk-vars-env")}-function_name', handler='app.lambda_handler', code=aws_lambda.Code.from_asset('hello_world'), runtime=aws_lambda.Runtime.PYTHON_3_10)
```
5. run cdk.synth > template.yaml
6. run `sam local invoke` using newly generated template.yaml
### Observed result:
```.log
% sam local invoke functionlambda -t cdk.out/template.yaml --debug
2023-09-11 12:16:42,991 | No config file found in this directory.
2023-09-11 12:16:42,993 | OSError occurred while reading TOML file: [Errno 2] No such file or directory: '/Users/100668/projects/test-sam-lambda/cdk.out/samconfig.toml'
2023-09-11 12:16:42,994 | Config file location: /Users/100668/projects/test-sam-lambda/cdk.out/samconfig.toml
2023-09-11 12:16:42,994 | Config file '/Users/100668/projects/test-sam-lambda/cdk.out/samconfig.toml' does not exist
2023-09-11 12:16:43,010 | Using config file: samconfig.toml, config environment: default
2023-09-11 12:16:43,011 | Expand command line arguments to:
2023-09-11 12:16:43,011 | --template_file=/Users/100668/projects/test-sam-lambda/cdk.out/template.yaml --function_logical_id=functionlambda --no_event
--layer_cache_basedir=/Users/100668/.aws-sam/layers-pkg --container_host=localhost --container_host_interface=127.0.0.1
2023-09-11 12:16:43,012 | local invoke command is called
2023-09-11 12:16:43,017 | Collected default values for parameters: {'BootstrapVersion': '/cdk-bootstrap/hnb659fds/version'}
2023-09-11 12:16:43,027 | CDK Path for resource functionlambdaServiceRoleF834821F is ['user-test-stack', 'functionlambda', 'ServiceRole', 'Resource']
2023-09-11 12:16:43,027 | CDK Path for resource functionlambdaEDFDB073 is ['user-test-stack', 'functionlambda', 'Resource']
2023-09-11 12:16:43,028 | Unable to resolve property environment: OrderedDict([('Fn::ImportValue', 'our_company-cdk-vars-env')]). Leaving as is.
2023-09-11 12:16:43,029 | Unable to resolve property FunctionName: OrderedDict([('Fn::Join', ['', [OrderedDict([('Fn::ImportValue', 'our_company-cdk-vars-env')]), '-functionlambda']])]). Leaving as is.
2023-09-11 12:16:43,029 | 0 stacks found in the template
2023-09-11 12:16:43,030 | Collected default values for parameters: {'BootstrapVersion': '/cdk-bootstrap/hnb659fds/version'}
2023-09-11 12:16:43,037 | CDK Path for resource functionlambdaServiceRoleF834821F is ['user-test-stack', 'functionlambda', 'ServiceRole', 'Resource']
2023-09-11 12:16:43,038 | CDK Path for resource functionlambdaEDFDB073 is ['user-test-stack', 'functionlambda', 'Resource']
2023-09-11 12:16:43,038 | Unable to resolve property environment: OrderedDict([('Fn::ImportValue', 'our_company-cdk-vars-env')]). Leaving as is.
2023-09-11 12:16:43,039 | Unable to resolve property FunctionName: OrderedDict([('Fn::Join', ['', [OrderedDict([('Fn::ImportValue', 'our_company-cdk-vars-env')]), '-functionlambda']])]). Leaving as is.
2023-09-11 12:16:43,040 | 2 resources found in the stack
2023-09-11 12:16:43,040 | Found Lambda function with name='functionlambdaEDFDB073' and CodeUri='asset.bb6e05f01acd0af30f089eed0751198cbff29e3408aab40971d26a9d3cb86e21'
2023-09-11 12:16:43,041 | --base-dir is not presented, adjusting uri asset.bb6e05f01acd0af30f089eed0751198cbff29e3408aab40971d26a9d3cb86e21 relative to
/Users/100668/projects/test-sam-lambda/cdk.out/template.yaml
2023-09-11 12:16:43,057 | Found one Lambda function with name 'functionlambda'
2023-09-11 12:16:43,057 | Invoking app.lambda_handler (python3.10)
2023-09-11 12:16:43,058 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2023-09-11 12:16:43,143 | Telemetry endpoint configured to be https://aws-serverless-tools-telemetry.us-west-2.amazonaws.com/metrics
2023-09-11 12:16:43,144 | Sending Telemetry: {'metrics': [{'commandRun': {'requestId': 'ac56c003-e30d-446c-9aff-c6836a0cacf1', 'installationId': 'ce4c62a4-85eb-4ecc-981e-64f5d5c045c4', 'sessionId':
'a2785b0e-336b-455e-bd0a-e89a60730006', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.13', 'samcliVersion': '1.97.0', 'awsProfileProvided': False, 'debugFlagProvided': True, 'region':
'', 'commandName': 'sam local invoke', 'metricSpecificAttributes': {'projectType': 'CDK', 'gitOrigin': None, 'projectName': '09c83e3a798ecd647e8b60f88973b478b563355d855d9bb0356eef9d23f44b19',
'initialCommit': None}, 'duration': 47, 'exitReason': 'TypeError', 'exitCode': 255}}]}
2023-09-11 12:16:43,144 | Unable to find Click Context for getting session_id.
2023-09-11 12:16:43,165 | Sending Telemetry: {'metrics': [{'events': {'requestId': 'a62e8cbd-244e-4448-84e7-abe3922008a2', 'installationId': 'ce4c62a4-85eb-4ecc-981e-64f5d5c045c4', 'sessionId':
'a2785b0e-336b-455e-bd0a-e89a60730006', 'executionEnvironment': 'CLI', 'ci': False, 'pyversion': '3.8.13', 'samcliVersion': '1.97.0', 'commandName': 'sam local invoke', 'metricSpecificAttributes':
{'events': [{'event_name': 'SamConfigFileExtension', 'event_value': '.toml', 'thread_id': 'af7cbaa77e164e34954294f495ecb874', 'time_stamp': '2023-09-11 17:16:42.993', 'exception_name': None},
{'event_name': 'UsedFeature', 'event_value': 'CDK', 'thread_id': '6749d0c5062040889640a8a7b0a5404c', 'time_stamp': '2023-09-11 17:16:43.058', 'exception_name': None}]}}}]}
2023-09-11 12:16:43,657 | Telemetry response: 200
2023-09-11 12:16:43,659 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
Error: unhashable type: 'collections.OrderedDict'
Traceback:
File "click/core.py", line 1078, in main
File "click/core.py", line 1688, in invoke
File "click/core.py", line 1688, in invoke
File "click/core.py", line 1434, in invoke
File "click/core.py", line 783, in invoke
File "click/decorators.py", line 92, in new_func
File "click/core.py", line 783, in invoke
File "samcli/lib/telemetry/metric.py", line 184, in wrapped
File "samcli/lib/telemetry/metric.py", line 149, in wrapped
File "samcli/lib/utils/version_checker.py", line 42, in wrapped
File "samcli/cli/main.py", line 95, in wrapper
File "samcli/commands/local/invoke/cli.py", line 101, in cli
File "samcli/commands/local/invoke/cli.py", line 199, in do_cli
File "samcli/commands/local/lib/local_lambda.py", line 140, in invoke
File "samcli/commands/local/lib/local_lambda.py", line 192, in get_invoke_config
File "samcli/commands/local/lib/local_lambda.py", line 283, in _make_env_vars
```
### Expected result:
We expected a hello world response, this works when using the aws_lambda.Function class when we provide a name that is not importing a value.
### Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
```
% sam --info
{
"version": "1.97.0",
"system": {
"python": "3.8.13",
"os": "macOS-13.5.1-arm64-arm-64bit"
},
"additional_dependencies": {
"docker_engine": "24.0.2",
"aws_cdk": "2.95.0 (build cfa7e88)",
"terraform": "Not available"
},
"available_beta_feature_env_vars": [
"SAM_CLI_BETA_FEATURES",
"SAM_CLI_BETA_BUILD_PERFORMANCE",
"SAM_CLI_BETA_TERRAFORM_SUPPORT",
"SAM_CLI_BETA_RUST_CARGO_LAMBDA"
]
}
```
Can anyone assist me in figuring out why this is failing?
Contributor guide
Assessment
This issue has not been assessed yet.