AWS Lambda可以同时订阅多个事件源类型。您只需在Lambda函数的触发器配置中添加更多的事件源即可。以下是一个使用Node.js的示例代码,在Lambda函数中订阅多个事件源类型:
exports.handler = async (event) => {
console.log("Received event:", JSON.stringify(event, null, 2));
if (event.Records) {
for (const record of event.Records) {
switch (record.eventSource) {
case "aws:s3":
// Process S3 event
console.log("S3 event:", JSON.stringify(record, null, 2));
break;
case "aws:dynamodb":
// Process DynamoDB event
console.log("DynamoDB event:", JSON.stringify(record, null, 2));
break;
case "aws:kinesis":
// Process Kinesis event
console.log("Kinesis event:", JSON.stringify(record, null, 2));
break;
default:
console.log(`Unknown event source: ${record.eventSource}`);
break;
}
}
}
return "Done";
};
以上示例中,Lambda函数同时订阅了S3、DynamoDB和Kinesis的事件源类型。在事件处理函数中,根据事件源类型来决定如何处理每个事件。如果Lambda函数订阅了多个事件源类型,对于每个事件源类型,只要有新事件产生,Lambda函数就会自动触发并处理。