การผสานรวมกับ Amazon EventBridge
ใช้ประโยชน์จาก Amazon EventBridge เพื่อสร้างบัสเหตุการณ์ กำหนดเส้นทางเหตุการณ์จากแหล่งต่าง ๆ และสร้างการผสานรวมที่ขับเคลื่อนด้วยเหตุการณ์อันทรงพลังภายในบัญชี AWS ของคุณ
การผสานรวมกับ Amazon EventBridge เป็นบทเรียน Serverless AWS Lambda Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Serverless AWS Lambda Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
What is Amazon EventBridge?
Welcome to a deeper dive into event-driven architectures! Today, we'll explore Amazon EventBridge, a powerful service that acts as a central nervous system for your serverless applications.
EventBridge is a serverless event bus that makes it easy to connect applications together using data from your own apps, Software-as-a-Service (SaaS) applications, and AWS services.
EventBridge Core Concepts
EventBridge operates on a few key concepts:
- Event Bus: A pipeline that receives events. There's a Default Event Bus for AWS service events, and you can create Custom Event Buses.
- Rules: Filters that match incoming events based on their content (event patterns).
- Targets: The AWS services or endpoints that an event is sent to when a rule matches.
The Default Event Bus
Every AWS account automatically has a Default Event Bus. This bus receives events from over 200 AWS services, like S3, EC2, Lambda, and CloudTrail.
You don't need to configure these sources; they publish events to the default bus automatically. This allows you to react to changes and activities across your AWS environment without custom code.
Custom Event Buses & Sources
While the default bus handles AWS events, you can create Custom Event Buses to manage events from your own applications or third-party SaaS providers.
This allows you to centralize event routing for your entire ecosystem. Event sources can be:
- AWS Services: (via Default Bus)
- SaaS Partners: (e.g., Zendesk, Shopify)
- Custom Applications: Your own microservices or monoliths.
Creating Event Patterns (Rules)
Rules are how EventBridge knows which events to route where. Each rule has an event pattern, a JSON structure that defines the criteria an event must meet to be matched.
Event patterns can match specific fields, prefixes, or even check for the existence of fields. They are highly flexible for filtering.
Event Pattern Example
Here's an example of an event pattern that matches any PutObject operation on an S3 bucket:
This pattern ensures only S3 object creation events trigger the rule.
{
"source": ["aws.s3"],
"detail-type": ["AWS API Call via CloudTrail"],
"detail": {
"eventSource": ["s3.amazonaws.com"],
"eventName": ["PutObject"]
}
}EventBridge Targets
Once an event matches a rule, EventBridge sends it to one or more targets. Targets are the downstream services that process the event.
Common targets include:
- AWS Lambda functions
- Amazon SQS queues
- Amazon SNS topics
- AWS Step Functions state machines
- Kinesis streams
- and many more!
Lambda as an EventBridge Target
Lambda functions are a very common target for EventBridge. When a rule matches an event, EventBridge invokes your Lambda function, passing the event data as the input.
This allows you to build highly responsive, event-driven microservices that react to changes across your AWS environment or custom applications.
Handling EventBridge Events in Lambda
Let's look at a Python Lambda function designed to receive and process an EventBridge event. The event parameter will contain the full JSON event payload.
We'll simulate an S3 PutObject event for demonstration.
import json
def lambda_handler(event, context):
print("Received event from EventBridge:")
print(json.dumps(event, indent=2))
source = event.get('source')
detail_type = event.get('detail-type')
if source == 'aws.s3' and detail_type == 'AWS API Call via CloudTrail':
bucket_name = event['detail']['requestParameters']['bucketName']
object_key = event['detail']['requestParameters']['key']
print(f"S3 PutObject event detected for bucket: {bucket_name}, key: {object_key}")
else:
print(f"Unknown event source: {source}, detail-type: {detail_type}")
return {
'statusCode': 200,
'body': json.dumps('Event processed successfully!')
}
# Simulate a local invocation for testing
if __name__ == '__main__':
sample_s3_event = {
"version": "0",
"id": "c6183e20-7f51-4091-8656-e23a31c51e0f",
"detail-type": "AWS API Call via CloudTrail",
"source": "aws.s3",
"account": "123456789012",
"time": "2023-10-27T10:00:00Z",
"region": "us-east-1",
"resources": [],
"detail": {
"eventSource": "s3.amazonaws.com",
"eventName": "PutObject",
"requestParameters": {
"bucketName": "my-cool-bucket",
"key": "new-image.jpg"
}
}
}
print("--- Simulating Lambda with EventBridge event ---")
lambda_handler(sample_s3_event, None)
print("--- Simulation complete ---")EventBridge Quick Check
You want to trigger a Lambda function whenever a new user signs up in your custom application. Your application publishes a UserSignedUp event to your custom event bus.
Which of the following event patterns would correctly match this specific event?
Recap: EventBridge Power
In this lesson, we explored Amazon EventBridge, understanding its role as a central event bus for your serverless applications. You learned about:
- Event Buses: Default, Custom, and Partner.
- Rules: Using event patterns to filter events.
- Targets: Sending matched events to services like Lambda.
- How to integrate Lambda to process EventBridge events.
EventBridge is key to building scalable, loosely coupled, and responsive event-driven architectures.
คำถามที่พบบ่อย
บทเรียน “การผสานรวมกับ Amazon EventBridge” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การผสานรวมกับ Amazon EventBridge” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless AWS Lambda Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การผสานรวมกับ Amazon EventBridge”
ใช้ประโยชน์จาก Amazon EventBridge เพื่อสร้างบัสเหตุการณ์ กำหนดเส้นทางเหตุการณ์จากแหล่งต่าง ๆ และสร้างการผสานรวมที่ขับเคลื่อนด้วยเหตุการณ์อันทรงพลังภายในบัญชี AWS ของคุณ คุณปฏิบัติ Serverless AWS Lambda Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless AWS Lambda Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless AWS Lambda Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การผสานรวมกับ Amazon EventBridge” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Serverless AWS Lambda Development นี้ได้ไหม
ได้ บทเรียน Serverless AWS Lambda Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การสร้างไมโครเซอร์วิสขับเคลื่อนด้วยเหตุการณ์
- การผสานรวมกับ Amazon EventBridge
- การประมวลผลแบบเรียลไทม์ด้วย Kinesis
- รูปแบบ Saga สำหรับธุรกรรมแบบกระจาย