Content:
In this step, you’ll implement a serverless event-driven flow:
(S3 → SQS → Lambda)
Whenever a new .json
quiz file is uploaded to your S3 bucket, it triggers an SQS message. That message then invokes a Lambda function to process the uploaded file.
Amazon S3 (Simple Storage Service): Object storage service for storing any amount of data. It supports event notifications when new objects are created.
Amazon SQS (Simple Queue Service): A fully managed message queuing service that enables you to decouple and scale microservices.
Lambda Trigger: An AWS mechanism that invokes your function in response to events (e.g., from SQS or S3).
S3 Event Notification: A feature that lets S3 notify other services like Lambda, SNS, or SQS when certain events (e.g., file upload) occur in a bucket.
To let S3 send messages to your queue, attach a queue access policy.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Statement1",
"Effect": "Allow",
"Principal": "*",
"Action": [
"sqs:SendMessage"
],
"Resource": "arn:aws:sqs:ap-southeast-1:466992855491:myprojectqueue" //Replace this with your SQS queue ARN
}
]
}
This policy allows S3 to push messages only from your specific bucket.
Configure S3 to send a message to the SQS queue whenever a .json
file is created.
🔎 This creates a direct connection from S3 to SQS, only for
.json
files uploaded to this bucket.
Before creating the Lambda function, you need to give it permission to:
Read files from the S3 bucket
Receive messages from the SQS queue
Write logs to CloudWatch
Create IAM Policy
Go to the IAM Console → Policies → Create policy
Choose JSON and paste the following permissions:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"sqs:DeleteMessage",
"sqs:SendMessage",
"sqs:GetQueueAttributes"
],
"Resource": "arn:aws:sqs:ap-southeast-1:466992855491:myprojectqueue"
}
]
}
Click Next, give it a name like: QuizLambdaSQSAccessPolicy
Click Create policy
Go to IAM Console → Roles → Create role
Trusted entity: Select (Lambda)
Permissions: Attach the policy you just created
Name the role: QuizLambdaExecutionRole
Click Create role
Create a Lambda function to process files triggered by SQS messages.
Paste the following code and deploy:
import { S3Client, GetObjectCommand } from "@aws-sdk/client-s3";
const s3 = new S3Client({});
const BUCKET = "myprojectbucket1290";
export const handler = async (event) => {
console.log("SQS Event:", JSON.stringify(event));
for (const record of event.Records) {
const body = JSON.parse(record.body);
const s3Info = body.Records?.[0]?.s3;
if (!s3Info) continue;
const key = decodeURIComponent(s3Info.object.key.replace(/\+/g, " "));
console.log("New File Uploaded:", key);
const res = await s3.send(new GetObjectCommand({ Bucket: BUCKET, Key: key }));
const data = await res.Body.transformToString();
console.log("File content:", data);
}
return { statusCode: 200 };
};
This means whenever SQS gets a new message, it will automatically trigger this Lambda.
✅ Checkpoint | Description |
---|---|
SQS receives message | Message from S3 triggered |
Lambda is triggered | Logs in CloudWatch confirm it ran |
File name is logged | S3 key is printed to the logs |
File content is printed | Lambda reads and displays file text |