I've got a lambda function which is fired via API Gateway (this works).
The lambda then adds some data to the DynamoDB (this works)
Then I call a function I've created which sends data to the SQS
export const AddToQueue = async vehicleId => {
const QueueParams = {
DelaySeconds: process.env.QUEUE_DELAY,
MessageAttributes: {},
};
QueueParams.MessageAttributes.vehicleId = {
DataType: 'String',
StringValue: vehicleId,
};
QueueParams.QueueUrl = 'my-queue-url';
QueueParams.MessageBody = `vehicle: ${vehicleId} - ${new Date().toISOString()}`;
try {
await sqs.sendMessage(QueueParams).promise();
console.log(`SQS:SUCCESS - AddToQueue - ${vehicleId}`);
} catch (error) {
console.log(`SQS:ERROR - AddToQueue - ${error}`);
}
};
The first time this happens the message doesn't get to the SQS, but any other subsequent message is added to the queue and is handled. I've tried a whole bunch of things IAM permissions, settings on the queue, recreating the queue and none of this has helped.
I don't receive a success or error message so have no way to debug any issue.
I'm no expert in this but I've experienced this sort of behaviour before, where some asynchronous code in the first invocation either appears to not be executed or its execution appears to be delayed (e.g. actions performed by subsequent invocations appear to be based on data from previous call).
In all cases it was due to the asynchronous code not being awaited correctly. I don't know enough about both javascript and Lambda to explain the behaviour, especially the delays - can promises resolve after the handler has returned but while the container is still running? But what I do know is, if the lambda returns before the promises (async functions return promises) have been resolved (and therefore before the asynchronous code has completed running, which causes the observed behaviour of code being delayed or not executed), then they haven't been awaited correctly.