通过 Amazon DocumentDB 触发器调用 Lambda 函数
以下代码示例演示了如何实现一个 Lambda 函数,该函数接收通过接收来自 DynamoDB 更改流的记录而触发的事件。该函数检索 DocumentDB 有效负载,并记录下记录内容。
- Go
-
- 适用于 Go V2 的 SDK
-
注意
在 GitHub 上查看更多内容。在无服务器示例
存储库中查找完整示例,并了解如何进行设置和运行。 使用 Go 将 Amazon DocumentDB 事件与 Lambda 结合使用。
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 package main import ( "context" "encoding/json" "fmt" "github.com/aws/aws-lambda-go/lambda" ) type Event struct { Events []Record `json:"events"` } type Record struct { Event struct { OperationType string `json:"operationType"` NS struct { DB string `json:"db"` Coll string `json:"coll"` } `json:"ns"` FullDocument interface{} `json:"fullDocument"` } `json:"event"` } func main() { lambda.Start(handler) } func handler(ctx context.Context, event Event) (string, error) { fmt.Println("Loading function") for _, record := range event.Events { logDocumentDBEvent(record) } return "OK", nil } func logDocumentDBEvent(record Record) { fmt.Printf("Operation type: %s\n", record.Event.OperationType) fmt.Printf("db: %s\n", record.Event.NS.DB) fmt.Printf("collection: %s\n", record.Event.NS.Coll) docBytes, _ := json.MarshalIndent(record.Event.FullDocument, "", " ") fmt.Printf("Full document: %s\n", string(docBytes)) }
- JavaScript
-
- 适用于 JavaScript 的 SDK (v3)
-
注意
在 GitHub 上查看更多内容。在无服务器示例
存储库中查找完整示例,并了解如何进行设置和运行。 使用 JavaScript 将 Amazon DocumentDB 事件与 Lambda 结合使用。
console.log('Loading function'); exports.handler = async (event, context) => { event.events.forEach(record => { logDocumentDBEvent(record); }); return 'OK'; }; const logDocumentDBEvent = (record) => { console.log('Operation type: ' + record.event.operationType); console.log('db: ' + record.event.ns.db); console.log('collection: ' + record.event.ns.coll); console.log('Full document:', JSON.stringify(record.event.fullDocument, null, 2)); };
- Python
-
- SDK for Python(Boto3)
-
注意
在 GitHub 上查看更多内容。在无服务器示例
存储库中查找完整示例,并了解如何进行设置和运行。 使用 Python 将 Amazon DocumentDB 事件与 Lambda 结合使用。
import json def lambda_handler(event, context): for record in event.get('events', []): log_document_db_event(record) return 'OK' def log_document_db_event(record): event_data = record.get('event', {}) operation_type = event_data.get('operationType', 'Unknown') db = event_data.get('ns', {}).get('db', 'Unknown') collection = event_data.get('ns', {}).get('coll', 'Unknown') full_document = event_data.get('fullDocument', {}) print(f"Operation type: {operation_type}") print(f"db: {db}") print(f"collection: {collection}") print("Full document:", json.dumps(full_document, indent=2))
- Ruby
-
- 适用于 Ruby 的 SDK
-
注意
在 GitHub 上查看更多内容。在无服务器示例
存储库中查找完整示例,并了解如何进行设置和运行。 使用 Ruby 将 Amazon DocumentDB 事件与 Lambda 结合使用。
require 'json' def lambda_handler(event:, context:) event['events'].each do |record| log_document_db_event(record) end 'OK' end def log_document_db_event(record) event_data = record['event'] || {} operation_type = event_data['operationType'] || 'Unknown' db = event_data.dig('ns', 'db') || 'Unknown' collection = event_data.dig('ns', 'coll') || 'Unknown' full_document = event_data['fullDocument'] || {} puts "Operation type: #{operation_type}" puts "db: #{db}" puts "collection: #{collection}" puts "Full document: #{JSON.pretty_generate(full_document)}" end
有关 Amazon SDK 开发人员指南和代码示例的完整列表,请参阅 将 Lambda 与 Amazon SDK 配合使用。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。
通过 DynamoDB 触发器调用 Lambda 函数
通过 Amazon S3 触发器调用 Lambda 函数