aws-lambdaamazon-dynamodb

My AWS lambda function is not working with Dynamo DB getItem using aws sdk version 3


I'm unable to get solution for basic dynamodb integration with aws lambda function using aws sdk version 3. Below is my index.mjs code.

    import { DynamoDBClient, GetItemCommand } from "@aws-sdk/client-dynamodb";
//import { marshall, unmarshall } from "@aws-sdk/util-dynamodb";
const dbclient = new DynamoDBClient({region:'us-east-1'});
export const handler = async (event) => {
  const uid=event.id;
  console.log("id from test event",uid);
  // TODO implement
  const params={
       TableName: 'test_cc_auth',
             Key: {
                 id: { S: uid }
                //id:  uid 
            }
          };
   // verify the db has data or not
     console.log("params object is ",params);
   var datafound;
   try{
      datafound= await dbclient.send(new GetItemCommand(params));
     console.log("data is : ",datafound);
   }catch(err){
      console.log(err);
   }
  const response = {
    statusCode: 200,
    body: JSON.stringify(datafound),
  };
  return response;
};

I'm getting below error ."ValidationException: The provided key element does not match the schema", but when I modify code params object like id: uid then I'm getting "TypeError: Cannot read properties of undefined (reading '0')==== without mentioning S". Can some help how to figure this out and what is the difference between adding type S and with adding type. Thank you for your support.


Solution

  • Cannot be sure without knowing the schema of your table, but most likely the test_cc_auth table has both partition key and sort key. In this case you need to provide them both, i.e. something like this:

    const params={
       TableName: 'test_cc_auth',
       Key: {
          id: { S: uid },
          other_id: { S: "value"}
       }
    };
    

    where other_id should be replaced by the name of your sort key.

    Source: DynamoDB documentation