0Pricing
Serverless AWS Lambda Development · บทเรียน

ตัวกระตุ้นเหตุการณ์ S3 และ SQS

เรียนรู้การกระตุ้นฟังก์ชัน Lambda เพื่อตอบสนองต่อเหตุการณ์ออบเจ็กต์ใน Amazon S3 เช่น การอัปโหลด และข้อความจากคิว Amazon SQS เพื่อรองรับกระบวนการประมวลผลข้อมูล

ตัวกระตุ้นเหตุการณ์ S3 และ SQS เป็นบทเรียน Serverless AWS Lambda Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Serverless AWS Lambda Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Events Drive Serverless

Serverless functions, like AWS Lambda, don't just run on their own. They wait for something to happen! This "something" is called an event.

An event is a signal from another service that tells your Lambda function to execute. Think of it like a doorbell ringing for your function to answer.

S3 Bucket Events

Amazon S3 (Simple Storage Service) is a popular cloud storage service. It can generate events when objects are created, deleted, or modified in your buckets.

  • Object Created (PUT): An image is uploaded.
  • Object Deleted: A file is removed.
  • Object Restored: An archived item is brought back.

These events can trigger a Lambda function to process the changes.

Connecting S3 to Lambda

To link an S3 bucket to a Lambda function, you define a trigger. This tells S3 which events to listen for and which Lambda function to invoke.

  • You specify the S3 bucket.
  • You choose the event types (e.g., s3:ObjectCreated:*).
  • You can filter by object prefix or suffix (e.g., only trigger for images/ folder or .jpg files).

What S3 Sends to Lambda

When an S3 event triggers your Lambda function, the function receives a JSON object containing details about the event. This object is the "event" parameter in your function handler.

Key information includes:

  • Bucket Name: Where the event occurred.
  • Object Key: The full path and name of the file affected.
  • Event Name: The specific action (e.g., ObjectCreated:Put).

Processing an S3 Upload

Here's a Python Lambda function that processes an S3 object creation event. It extracts and prints the bucket name and the key of the uploaded object.

Try uploading a file to an S3 bucket configured to trigger this function!

import json

def lambda_handler(event, context):
    for record in event['Records']:
        bucket_name = record['s3']['bucket']['name']
        object_key = record['s3']['object']['key']
        event_name = record['eventName']
        print(f"Event: {event_name}")
        print(f"Bucket: {bucket_name}")
        print(f"Object Key: {object_key}")
    return {
        'statusCode': 200,
        'body': json.dumps('S3 event processed!')
    }

SQS Queue Messages

Amazon SQS (Simple Queue Service) is a message queuing service. It allows different parts of your application to communicate asynchronously without direct interaction.

When messages are sent to an SQS queue, they can trigger a Lambda function. This is great for tasks that can be processed later or in batches, like sending emails or processing orders.

Connecting SQS to Lambda

Setting up an SQS trigger for Lambda is straightforward. Lambda continuously polls the SQS queue for new messages.

  • You specify the SQS queue.
  • You define a batch size (1 to 10,000 messages). This determines how many messages Lambda tries to process in a single invocation.
  • Lambda deletes messages from the queue only after successful processing.

What SQS Sends to Lambda

Similar to S3, an SQS event passed to your Lambda function is a JSON object. It contains an array of Records, each representing a message from the queue.

Each record includes:

  • Message Body: The actual content of the message.
  • Message Attributes: Optional metadata about the message.
  • Receipt Handle: Used internally by SQS for message deletion.

Processing SQS Messages

This Python Lambda function processes messages from an SQS queue. It iterates through the batch of messages received and prints their content.

Imagine each message contains a task to perform!

import json

def lambda_handler(event, context):
    for record in event['Records']:
        message_body = record['body']
        print(f"Received message: {message_body}")
        # Add your processing logic here
    return {
        'statusCode': 200,
        'body': json.dumps('SQS messages processed!')
    }

S3 vs. SQS Triggers

You've learned about two powerful event sources for AWS Lambda: S3 and SQS. They serve different purposes but both enable event-driven architectures.

Which of the following statements are TRUE regarding S3 and SQS Lambda triggers?

Recap: S3 & SQS Triggers

Great job! You've explored how AWS Lambda functions can be triggered by events from Amazon S3 and SQS.

  • S3 triggers are perfect for reacting to file changes, like image uploads or document processing.
  • SQS triggers enable asynchronous processing of messages, allowing for decoupled and scalable workflows.

These event sources are fundamental to building powerful, reactive serverless applications!

คำถามที่พบบ่อย

บทเรียน “ตัวกระตุ้นเหตุการณ์ S3 และ SQS” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ตัวกระตุ้นเหตุการณ์ S3 และ SQS” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless AWS Lambda Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ตัวกระตุ้นเหตุการณ์ S3 และ SQS”

เรียนรู้การกระตุ้นฟังก์ชัน Lambda เพื่อตอบสนองต่อเหตุการณ์ออบเจ็กต์ใน Amazon S3 เช่น การอัปโหลด และข้อความจากคิว Amazon SQS เพื่อรองรับกระบวนการประมวลผลข้อมูล คุณปฏิบัติ Serverless AWS Lambda Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless AWS Lambda Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless AWS Lambda Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “ตัวกระตุ้นเหตุการณ์ S3 และ SQS” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Serverless AWS Lambda Development นี้ได้ไหม

ได้ บทเรียน Serverless AWS Lambda Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. พื้นฐานสถาปัตยกรรมที่ขับเคลื่อนด้วยเหตุการณ์
  2. การเรียกใช้ Lambda ด้วย API Gateway
  3. ตัวกระตุ้นเหตุการณ์ S3 และ SQS
  4. การเรียกใช้ตามกำหนดเวลาด้วย EventBridge
← กลับไปที่ Serverless AWS Lambda Development