พื้นฐานสถาปัตยกรรมที่ขับเคลื่อนด้วยเหตุการณ์
ทำความเข้าใจหลักการของสถาปัตยกรรมที่ขับเคลื่อนด้วยเหตุการณ์ และวิธีที่ฟังก์ชัน Lambda ตอบสนองต่อเหตุการณ์ที่บริการอื่นสร้างขึ้น
พื้นฐานสถาปัตยกรรมที่ขับเคลื่อนด้วยเหตุการณ์ เป็นบทเรียน Serverless AWS Lambda Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Serverless AWS Lambda Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is Event-Driven?
Imagine a restaurant where orders are taken, food is cooked, and bills are paid. In a traditional setup, each step might wait for the previous one to finish.
Event-Driven Architecture (EDA) is different. It's like having many independent stations that react to things happening around them, without direct instructions.
Understanding Events
At its core, an event is simply a signal that "something happened." It's a record of a state change or an action that occurred in a system.
- User clicks a button
- A file is uploaded
- A new order is placed
- A sensor detects motion
These are all examples of events!
Key Parts of EDA
Event-Driven Architecture involves three main components:
- Event Source: The system or service that generates an event (e.g., a user interface, a database change).
- Event: The data package describing what happened.
- Event Consumer/Processor: A service that listens for and reacts to specific events (e.g., an AWS Lambda function).
Events often travel through an Event Bus or Queue, which helps route them to the right consumers.
Benefits of Event-Driven
EDA offers powerful advantages for modern applications:
- Loose Coupling: Components don't directly "know" about each other, only about the events. This makes systems easier to update and maintain.
- Scalability: Event consumers can scale independently based on the event load.
- Responsiveness: Systems can react to changes in real-time, leading to a more dynamic user experience.
- Resilience: If one component fails, others can often continue processing events.
Lambda & Event Responders
AWS Lambda is a perfect fit for Event-Driven Architecture. It acts as an event consumer or event processor.
Think of Lambda functions as tiny, specialized workers that are "asleep" until an event wakes them up. Once the event is processed, they go back to sleep.
Invoking Lambda with Events
A key concept with Lambda is that you don't manually "start" it in the traditional sense. Instead, other AWS services or custom applications publish events.
When an event matching a Lambda function's configuration occurs, Lambda automatically invokes (runs) that function. It's fully automated!
Inside an Event (JSON)
Events are typically structured data, often in JSON format. They contain details about what happened, when, and where.
Here's a simplified example of what an event might look like:
{
"version": "0",
"id": "abc-123-def-456",
"detail-type": "MyCustomEvent",
"source": "my.application",
"time": "2023-10-27T12:34:56Z",
"detail": {
"userId": "user123",
"action": "itemAddedToCart",
"itemId": "product789"
}
}Event Flow in Action
Let's trace a simple event flow:
- A user adds an item to their shopping cart in your app (Event Source).
- Your app publishes a "
itemAddedToCart" Event. - An AWS Lambda function (Event Consumer) is configured to listen for this specific event.
- The Lambda function automatically runs, perhaps to update inventory or send a personalized recommendation.
The app doesn't need to know how inventory is updated; it just publishes the event.
Lambda Event Handler
When Lambda is invoked, it passes the event data to your function. In Java, this data often comes as a Map or a custom object.
Try running this simple Java Lambda function. It prints the incoming event data:
import java.util.HashMap;
import java.util.Map;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class MyEventHandler implements RequestHandler<Map<String, Object>, String> {
@Override
public String handleRequest(Map<String, Object> event, Context context) {
// Lambda receives the event as input
System.out.println("Received event:");
event.forEach((key, value) -> System.out.println(" " + key + ": " + value));
// You can access specific event data, e.g., from a "detail" field
if (event.containsKey("detail") && event.get("detail") instanceof Map) {
Map<String, Object> detail = (Map<String, Object>) event.get("detail");
System.out.println("Detail action: " + detail.get("action"));
}
return "Event processed successfully!";
}
// Main method for local testing (not executed by Lambda runtime)
public static void main(String[] args) {
Map<String, Object> testEvent = new HashMap<>();
testEvent.put("version", "0");
testEvent.put("id", "test-id-123");
testEvent.put("detail-type", "UserAction");
testEvent.put("source", "my.test.app");
Map<String, Object> detailMap = new HashMap<>();
detailMap.put("userId", "testUser");
detailMap.put("action", "login");
testEvent.put("detail", detailMap);
MyEventHandler handler = new MyEventHandler();
String result = handler.handleRequest(testEvent, null);
System.out.println("--- Local Test Result ---");
System.out.println(result);
}
}Event-Driven Quiz
Which of the following best describes the role of an AWS Lambda function in an Event-Driven Architecture?
Recap & Next Steps
Great job! You've learned the fundamentals of Event-Driven Architecture and how AWS Lambda fits into this powerful paradigm.
- Events are signals that something happened.
- EDA uses events for loosely coupled, scalable systems.
- Lambda functions are perfect event consumers, automatically invoked by events.
Next, we'll dive into specific AWS services that can trigger your Lambda functions!
คำถามที่พบบ่อย
บทเรียน “พื้นฐานสถาปัตยกรรมที่ขับเคลื่อนด้วยเหตุการณ์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “พื้นฐานสถาปัตยกรรมที่ขับเคลื่อนด้วยเหตุการณ์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless AWS Lambda Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “พื้นฐานสถาปัตยกรรมที่ขับเคลื่อนด้วยเหตุการณ์”
ทำความเข้าใจหลักการของสถาปัตยกรรมที่ขับเคลื่อนด้วยเหตุการณ์ และวิธีที่ฟังก์ชัน Lambda ตอบสนองต่อเหตุการณ์ที่บริการอื่นสร้างขึ้น คุณปฏิบัติ Serverless AWS Lambda Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless AWS Lambda Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless AWS Lambda Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “พื้นฐานสถาปัตยกรรมที่ขับเคลื่อนด้วยเหตุการณ์” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Serverless AWS Lambda Development นี้ได้ไหม
ได้ บทเรียน Serverless AWS Lambda Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นฐานสถาปัตยกรรมที่ขับเคลื่อนด้วยเหตุการณ์
- การเรียกใช้ Lambda ด้วย API Gateway
- ตัวกระตุ้นเหตุการณ์ S3 และ SQS
- การเรียกใช้ตามกำหนดเวลาด้วย EventBridge