var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'})
AWS.config.update({region: 'eu-west-1'})
var docClient = new AWS.DynamoDB.DocumentClient()
const params = {
TableName: 'TC_QUESTIONS',
Item: {
'questionId' : {S: '001'},
'questionText' : {S: 'Richard Roe'}
}
}
var putItemPromise = docClient.put(params).promise()
putItemPromise.then(function(data) {
console.log("Added item:", JSON.stringify(data, null, 2));
}).catch(function(err) {
console.error("Unable to add item. Error JSON:", JSON.stringify(err, null, 2));
});
await putItemPromise
No error is returned. No success callback. When I use the wrong column name I get an error back from Dynamo. I've tried running the it locally and on lambda. It executes, and exits. No rows get added to the Table. What am I doing wrong?
Edit: Here is my CF for the DynamoDB table:
QuestionsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: 'TC_QUESTIONS'
AttributeDefinitions:
- AttributeName: questionId
AttributeType: S
- AttributeName: questionText
AttributeType: S
KeySchema:
- AttributeName: questionId
KeyType: HASH
- AttributeName: questionText
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: "5"
WriteCapacityUnits: "5"
We have successfully created our DynamoDB table, lets now make our lambda function. On the AWS Lamba dashboard click “Create function”. Select “Author from scratch” and name the function “WriteMessage”, make sure Node.js be selected for the runtime.
Navigate to you DynamoDB and go to the items tab, here you will see your message from the AWS Lambda Function. Repeat the steps above to create a function and name this lambda function ReadMessage.
DynamoDB table – The DynamoDB table to read records from. Batch size – The number of records to send to the function in each batch, up to 10,000. Lambda passes all of the records in the batch to the function in a single call, as long as the total size of the events doesn't exceed the payload limit for synchronous invocation (6 MB).
Lambda polls shards in your DynamoDB stream for records at a base rate of 4 times per second. When records are available, Lambda invokes your function and waits for the result. If processing succeeds, Lambda resumes polling until it receives more records.
'use strict';
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: 'eu-west-1'});
exports.handler = async (event) => {
const params = {
TableName: 'TC_QUESTIONS',
Item: {
'questionId': '001',
'questionText' : 'Richard Roe'
}
}
try {
await docClient.put(params).promise()
} catch (e) {
console.log(e.message)
}
};
If your table exists and questionId is your partition key, the code above works.
I think you were mixing things up a little bit, because since you're using DocClient you don't need to specify DynamoDB's types. This was the problem on your code. Just specify the raw values (like I did above, passing the String itself) and it will work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With