与 Amazon EventBridge 集成
利用 Amazon EventBridge 构建事件总线,从各种来源路由事件,并在您的 AWS 账户中创建强大的事件驱动集成。
与 Amazon EventBridge 集成 是 CoddyKit 上的免费 Serverless AWS Lambda Development 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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 导师)并解锁 Serverless AWS Lambda Development 课程的其余内容,请升级到 CoddyKit PRO。 Serverless AWS Lambda Development 课程共包含 4 节课。
「与 Amazon EventBridge 集成」这节课中我会学到什么?
利用 Amazon EventBridge 构建事件总线,从各种来源路由事件,并在您的 AWS 账户中创建强大的事件驱动集成。 你通过在浏览器中直接运行的动手代码来练习 Serverless AWS Lambda Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Serverless AWS Lambda Development 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Serverless AWS Lambda Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「与 Amazon EventBridge 集成」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Serverless AWS Lambda Development 课中编写并运行代码吗?
能。每节 Serverless AWS Lambda Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 构建事件驱动的微服务
- 与 Amazon EventBridge 集成
- 使用 Kinesis 进行实时处理
- 分布式事务的 Saga 模式