I am using lambda, API gateway and dynamodb
I am creating a batchWrite function to upload more than 25 items in one single function.
But I find that when I upload 35 items,
the first 25 items are uploaded successfully to dynamodb, but last 10 items fail but no error is logged.
The function is written in node.js
'use strict'
const AWS = require('aws-sdk');
const sha1 = require('sha1');
const documentClient = new AWS.DynamoDB.DocumentClient();
exports.handler = async function (event, context, callback) {
let responseBody = "";
let statusCode = 0;
const { excelObject } = JSON.parse(event.body);
if(excelObject){
await uploadFileByBatch(excelObject)
}
const response = {
statusCode: statusCode,
headers:{
"Content-Type": "application/json",
"access-control-allow-origin": "*"
},
body: responseBody
}
console.log(response)
return response
}
let uploadFileByBatch = async function (payload) {
var items = [];
var params={};
if(payload.length > 25){
console.log("> 25")
let newObj = payload.slice(0,25)
payload.splice(0,25)
console.log("new Payload length")
console.log(payload.length)
newObj.forEach(obj =>{
let hash = sha1(Buffer.from(new Date().toString()+ Math.random()));
items.push(
{
PutRequest:{
Item: {
id: obj.id?obj.id:hash,
organization_EN: obj.organization_EN,
email: obj.email,
isActive: obj.isActive
}
}
}
)
})
params = {
RequestItems:{
"Community": items
}
}
console.log(params)
try{
const data = await documentClient.batchWrite(params).promise();
}catch(err){
console.log(err)
}
uploadFileByBatch(payload)
}else{
console.log("less than 25")
payload.forEach(obj =>{
let hash = sha1(Buffer.from(new Date().toString()+ Math.random()));
items.push(
{
PutRequest:{
Item: {
id: obj.id?obj.id:hash,
organization_EN: obj.organization_EN,
email: obj.email,
isActive: obj.isActive
}
}
}
)
})
params = {
RequestItems:{
"Community": items
}
}
console.log(params)
try{
console.log("first BatchWrite")
const data = await documentClient.batchWrite(params).promise();
console.log("first Data")
}catch(err){
console.log(err)
}
}
}
The log is like below.
>25
New Payload length
10
{
RequestItems: {
'Community': [
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object]
]
}
}
first BatchWrite
Less than 25
{
RequestItems: {
'Community': [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object]
]
}
}
{
statusCode: 0,
headers: {
'Content-Type': 'application/json',
'access-control-allow-origin': '*'
},
body: ''
}