0

The table in dynamodb has 2 columns: timeStamp(string) and tableName(string) when trying to insert datetime as string, it fails with this error:

"errorMessage": "Parameter validation failed:\nInvalid type for parameter Item.timeStamp, value: 2022-03-17 16:46:46.510210, type: <class 'str'>, valid types: <class 'dict'>

here is the code snippet:

import json
import boto3
from datetime import datetime as dt

def put_data(timeStamp, tableName):
    dynamodb = boto3.client('dynamodb')
    
    
    #data = json.loads(json.dumps({"timeStamp":timeStamp ,"tableName":tableName}))
    
    #print(data)
    
    response = dynamodb.put_item(TableName='dev-data-migration-state', Item= {"timeStamp":timeStamp ,"tableName":tableName})
    
    print(response)


def lambda_handler(event, context):
    
    put_data(json.loads(json.dumps(str(dt.now()))), 'orders')

i have tried just doing str(dt.now) but that didn't work either.

1 Answer 1

1

I found the solution, I'm using the client-API, so I needed to put the data in the DynamoDB-JSON format with the data type:

import json
import boto3
from datetime import datetime as dt

def put_data(timeStamp, tableName):
    dynamodb = boto3.client('dynamodb')

    response = dynamodb.put_item(TableName='dev-data-migration-state', 
    Item= {
        "timeStamp":{'S': timeStamp},
        "tableName":{'S': tableName}
    }
    )
    
    print(response)


def lambda_handler(event, context):
    
    put_data(str(dt.now()), 'orders')

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.