1

so I'm trying to place a dictionary into a dynamodb table, yet I keep getting the error:

ERROR TypeError: cannot pickle '_thread.lock' object

The code is below:

def send_to_dynamo():

    message = receive_from_queue()

    database = boto3.resource('dynamodb')
    table = database.Table("Email_Service")

    logger.info(message)
    logger.info(type(message))

    table.put_item(
        TableName = table,
        Item= message)

The message in question is :

dict = {'subject': {"S": "Test email output"},
            'recipients': {"S":"email@email.com"},
            'main_body': {"S":"This is to test"},
            'send_time': {"S":"17:30"},
            SORT_KEY : {"N":202211101157},
            PARTITION_KEY : {"S":"servicename1"}}

I'm struggling a lot to figure out how to add a dictionary into a dynamodb table : (

Any help would be super appreciated!

Tyche
  • 11
  • 4
  • The item needs to be a simple dictionary of name/value pairs. It looks like it's more than that. Remove non-simple attributes (like threading-related context) from the item. – jarmod Nov 13 '22 at 18:04
  • Please share the value of `message` and then we can provide guidance. – Leeroy Hannigan Nov 13 '22 at 18:21
  • @LeeHannigan I added the message to the original post ! Sorry if there's a clear mistake somewhere, I'm only a student : ( ty ! – Tyche Nov 13 '22 at 19:13

1 Answers1

0

Looking at your issue it seems to be caused by a mix match of the JSON and the client you use. Your dict is using DynamoDB JSON, whereas you use Resource client which takes native JSON. Try save this:

dict=  {
            'subject': "Test email output",
            'recipients': "email@email.com",
            'main_body': "This is to test",
            'send_time': "17:30",
            'SORT_KEY': 202211101157,
            'PARTITION_KEY' : "servicename1"
        }
Leeroy Hannigan
  • 11,409
  • 3
  • 14
  • 31
  • This gets farther! However I get the error - ([ERROR] TypeError: unhashable type: 'dict') – Tyche Nov 13 '22 at 19:40
  • Can you hard code what I shared into the `Item` and just add your partition and sort key. I don't believe your issue is with saving the item. – Leeroy Hannigan Nov 13 '22 at 19:56
  • I get the following returned to me after hard coding the dict as the item: [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter Item.subject, value: Test email output, type: , valid types: Invalid type for parameter Item.recipients, value: email@email.com, type: , valid types: etc. etc. with the rest – Tyche Nov 13 '22 at 20:34
  • It looks as though you are somehow mixing up your clients. Can you update your code to what you're currently trying to do, including the code calling the function. – Leeroy Hannigan Nov 13 '22 at 20:52