Ensure that:
FIFO stands for First-In, First-Out
A FIFO queue guarantees:
In distributed systems, events may arrive (out of order) or get retried. Without ordering guarantees, a later update (e.g., (quiz deletion)) may be processed before an earlier one (e.g., (quiz creation)), leading to incorrect state or data corruption.
FIFO queues solve this by:
Because SNS Standard does not support direct delivery to FIFO queues.
Modify your (SubmitQuizEvent) Lambda function:
import {
SQSClient,
SendMessageCommand
} from "@aws-sdk/client-sqs";
const sqs = new SQSClient({});
const QUEUE_URL = "https://sqs.ap-southeast-1.amazonaws.com/YOUR_ID/QuizFifoQueue.fifo";
export const handler = async (event) => {
const body = JSON.parse(event.body || "{}");
const params = {
QueueUrl: QUEUE_URL,
MessageBody: JSON.stringify(body),
MessageGroupId: "quiz-events", // Required
// Optional: MessageDeduplicationId
// MessageDeduplicationId: body.id || Date.now().toString(),
};
try {
await sqs.send(new SendMessageCommand(params));
return {
statusCode: 200,
body: JSON.stringify({ message: "Event sent to FIFO queue." }),
};
} catch (err) {
console.error("Send failed", err);
return {
statusCode: 500,
body: JSON.stringify({ error: err.message }),
};
}
};
Create the Consumer Lambda
Go to AWS Lambda → Create function
Name it: ProcessQuizFifoEvent
Attach role with sqs access
Runtime: Node.js 20.x or Node.js 22.x
Trigger: QuizFifoQueue.fifo
Paste the code below:
export const handler = async (event) => {
console.log(" FIFO Event:", JSON.stringify(event));
return { statusCode: 200 };
};