1

using boto3 I am trying to get a object in a dynamodb table.

following this stack overflow post the correct syntax is

client = boto3.client('dynamodb')

response = client.get_item(TableName='Garbage_collector_table', Key={'topic':{'S':str(my_topic)}})

Not able to get_item from AWS dynamodb using python?

http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html

I have tried various iterations to get proper syntax my current is this

 if event['hcpcs_codes'] != None:
        # codes must be in numerical, alphabetical order
        # multi codes should be seperated by a comma with no spaces
        client = boto3.client('dynamodb')
        payload = {
            "HCPCSCodeSet": event['hcpcs_codes']
        }
        response = client.get_item(TableName="mce-hcpcs-associations-table-dev",
                                   Key={'HCPCSCodeSet':{'S': payload}})
        print('here comes the tacos brah')
        print(response)

Im not sure what this thing wants. What is the proper syntax?

Invalid type for parameter Key.HCPCSCodeSet, value: tacoman, type: <class 'str'>, valid types: <class 'dict'>
Traceback (most recent call last):
  File "/var/task/lambdafile.py", line 18, in lambda_handler
    Key= payload)
  File "/var/task/botocore/client.py", line 415, in _api_call

the primary key name for the dynamodb database is

Partition key
HCPCSCodeSet (String)
  • 1
    Primary and sort keys cannot be non-scalar types such as dict/map. They must be scalar i.e. string, number, or binary. Also, where does `tacoman` come from? – jarmod May 09 '22 at 17:32
  • Taco man comes from the post request this lambda is attached to – corporate chris May 09 '22 at 17:58
  • @jarmod your comment makes no sense when we look at the error. Please clarify – corporate chris May 09 '22 at 18:26
  • The `payload` variable is not a scalar value so `Key={'HCPCSCodeSet':{'S': payload}}` is not providing the key in a valid way. Perhaps you meant `Key={'HCPCSCodeSet':{'S': event['hcpcs_codes']}}` assuming that `event['hcpcs_codes']` is actually a string. – jarmod May 09 '22 at 18:31

1 Answers1

4

This code:

        payload = {
            "HCPCSCodeSet": event['hcpcs_codes']
        }
        response = client.get_item(TableName="mce-hcpcs-associations-table-dev",
                                   Key={'HCPCSCodeSet':{'S': payload}})

Ends up trying to send the following to DynamoDB:

Key={'HCPCSCodeSet':{'S': {
            "HCPCSCodeSet": event['hcpcs_codes']
        }}}

Which is obviously not correct. It's unclear why you are building the payload object at all. You could just do this:

        response = client.get_item(TableName="mce-hcpcs-associations-table-dev",
                                   Key={'HCPCSCodeSet':{'S': event['hcpcs_codes']}})
Mark B
  • 183,023
  • 24
  • 297
  • 295