I have a lambda function that uses xstate to perform certain tasks sequentially and one of the steps is to save data to dynamo db. But my lambda ends execution as soon as the below line is being executed.
const response = await new DynamoDB.DocumentClient().put(params).promise();
my code :
import {Handler} from "aws-lambda";
import {interpret} from 'xstate';
import { Machine} from "xstate";
import {PutItemInput} from "aws-sdk/clients/dynamodb";
import {DynamoDB} from "aws-sdk";
export const recordProcessor: Handler = async (event) => {
console.log('records size----->', event.Records.length);
for (const record of event.Records) {
const body = JSON.parse(record.body);
console.log('body ----->', body);
interpret(Machine({id:'test',
context:body,
initial: 'start',
states:{
start: {
invoke: {
src: context => initiate(context),
onDone: {
target: 'success'
}
}
},
success: {
type: 'final'
}
}
})).onTransition(state => {
if (state.changed) {
console.log('state ----> ', state.value);
}
}).onDone(() => console.log('done--->')).start();
}
async function initiate(context: any) {
console.log('DbDynamoImpl ::: insert ::: start :::');
let params: PutItemInput = {
TableName: 'test',
Item: context
};
try {
const response = await new DynamoDB.DocumentClient().put(params).promise();
console.log('DbDynamoImpl ::: insert ::: response :::', response);
return true;
} catch (e) {
console.log("DynamoDb insert error", e);
return false;
}
}
};