Content:
Goal
Allow your users or frontend clients to send quiz-related events (such as creating a new quiz) by calling an HTTP endpoint. This request goes through API Gateway, is processed by a Lambda function, published to SNS, and distributed (fan-out) to one or more SQS queues. Each queue then triggers its own Lambda consumer to process the message independently.
Pattern: (API Gateway → Lambda → SNS → SQS → Lambda)
Involved Services:
This step introduces asynchronous event-based processing into your architecture. It allows you to:
Instead of sending data directly to S3 or a Lambda, clients simply call your API Gateway. Everything behind the scenes is handled via events.
Paste this sample handler in the code section:
export const handler = async (event) => {
console.log("SQS Event Received:", JSON.stringify(event));
return { statusCode: 200 };
};
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "sns:Publish",
"Resource": "*"
}
]
}
Paste this sample code:
import { SNSClient, PublishCommand } from "@aws-sdk/client-sns";
const sns = new SNSClient({});
const TOPIC_ARN = "arn:aws:sns:ap-southeast-1:YOUR_ACCOUNT_ID:QuizEventTopic"; //Copy and Paste your SNS Topic ARN here
export const handler = async (event) => {
const body = JSON.parse(event.body || "{}");
const message = {
default: JSON.stringify(body),
};
try {
await sns.send(
new PublishCommand({
TopicArn: TOPIC_ARN,
Message: JSON.stringify(message),
MessageStructure: "json",
})
);
return {
statusCode: 200,
body: JSON.stringify({ message: "Quiz event submitted." }),
};
} catch (err) {
console.error(" Publish failed", err);
return {
statusCode: 500,
body: JSON.stringify({ error: err.message }),
};
}
};
curl -X POST https://your-api-id.amazonaws.com/quiz-event \
-H "Content-Type: application/json" \
-d '{ "quiz_id": "quiz001", "event": "created" }'
Result: