Invoke a Lambda function from a Amazon DocumentDB trigger
The following code examples show how to implement a Lambda function that receives an event triggered by receiving records from a DocumentDB change stream. The function retrieves the DocumentDB payload and logs the record contents.
- .NET
-
- AWS SDK for .NET
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming a Amazon DocumentDB event with Lambda using .NET.
using Amazon.Lambda.Core; using System.Text.Json; using System; using System.Collections.Generic; using System.Text.Json.Serialization; //Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))] namespace LambdaDocDb; public class Function { /// <summary> /// Lambda function entry point to process Amazon DocumentDB events. /// </summary> /// <param name="event">The Amazon DocumentDB event.</param> /// <param name="context">The Lambda context object.</param> /// <returns>A string to indicate successful processing.</returns> public string FunctionHandler(Event evnt, ILambdaContext context) { foreach (var record in evnt.Events) { ProcessDocumentDBEvent(record, context); } return "OK"; } private void ProcessDocumentDBEvent(DocumentDBEventRecord record, ILambdaContext context) { var eventData = record.Event; var operationType = eventData.OperationType; var databaseName = eventData.Ns.Db; var collectionName = eventData.Ns.Coll; var fullDocument = JsonSerializer.Serialize(eventData.FullDocument, new JsonSerializerOptions { WriteIndented = true }); context.Logger.LogLine($"Operation type: {operationType}"); context.Logger.LogLine($"Database: {databaseName}"); context.Logger.LogLine($"Collection: {collectionName}"); context.Logger.LogLine($"Full document:\n{fullDocument}"); } public class Event { [JsonPropertyName("eventSourceArn")] public string EventSourceArn { get; set; } [JsonPropertyName("events")] public List<DocumentDBEventRecord> Events { get; set; } [JsonPropertyName("eventSource")] public string EventSource { get; set; } } public class DocumentDBEventRecord { [JsonPropertyName("event")] public EventData Event { get; set; } } public class EventData { [JsonPropertyName("_id")] public IdData Id { get; set; } [JsonPropertyName("clusterTime")] public ClusterTime ClusterTime { get; set; } [JsonPropertyName("documentKey")] public DocumentKey DocumentKey { get; set; } [JsonPropertyName("fullDocument")] public Dictionary<string, object> FullDocument { get; set; } [JsonPropertyName("ns")] public Namespace Ns { get; set; } [JsonPropertyName("operationType")] public string OperationType { get; set; } } public class IdData { [JsonPropertyName("_data")] public string Data { get; set; } } public class ClusterTime { [JsonPropertyName("$timestamp")] public Timestamp Timestamp { get; set; } } public class Timestamp { [JsonPropertyName("t")] public long T { get; set; } [JsonPropertyName("i")] public int I { get; set; } } public class DocumentKey { [JsonPropertyName("_id")] public Id Id { get; set; } } public class Id { [JsonPropertyName("$oid")] public string Oid { get; set; } } public class Namespace { [JsonPropertyName("db")] public string Db { get; set; } [JsonPropertyName("coll")] public string Coll { get; set; } } }
- Go
-
- SDK for Go V2
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming a Amazon DocumentDB event with Lambda using Go.
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)) }
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming a Amazon DocumentDB event with Lambda using Java.
import java.util.List; import java.util.Map; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; public class Example implements RequestHandler<Map<String, Object>, String> { @SuppressWarnings("unchecked") @Override public String handleRequest(Map<String, Object> event, Context context) { List<Map<String, Object>> events = (List<Map<String, Object>>) event.get("events"); for (Map<String, Object> record : events) { Map<String, Object> eventData = (Map<String, Object>) record.get("event"); processEventData(eventData); } return "OK"; } @SuppressWarnings("unchecked") private void processEventData(Map<String, Object> eventData) { String operationType = (String) eventData.get("operationType"); System.out.println("operationType: %s".formatted(operationType)); Map<String, Object> ns = (Map<String, Object>) eventData.get("ns"); String db = (String) ns.get("db"); System.out.println("db: %s".formatted(db)); String coll = (String) ns.get("coll"); System.out.println("coll: %s".formatted(coll)); Map<String, Object> fullDocument = (Map<String, Object>) eventData.get("fullDocument"); System.out.println("fullDocument: %s".formatted(fullDocument)); } }
- JavaScript
-
- SDK for JavaScript (v3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming a Amazon DocumentDB event with Lambda using JavaScript.
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)); };
Consuming a Amazon DocumentDB event with Lambda using TypeScript
import { DocumentDBEventRecord, DocumentDBEventSubscriptionContext } from 'aws-lambda'; console.log('Loading function'); export const handler = async ( event: DocumentDBEventSubscriptionContext, context: any ): Promise<string> => { event.events.forEach((record: DocumentDBEventRecord) => { logDocumentDBEvent(record); }); return 'OK'; }; const logDocumentDBEvent = (record: DocumentDBEventRecord): void => { 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)); };
- PHP
-
- SDK for PHP
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming a Amazon DocumentDB event with Lambda using PHP.
<?php require __DIR__.'/vendor/autoload.php'; use Bref\Context\Context; use Bref\Event\Handler; class DocumentDBEventHandler implements Handler { public function handle($event, Context $context): string { $events = $event['events'] ?? []; foreach ($events as $record) { $this->logDocumentDBEvent($record['event']); } return 'OK'; } private function logDocumentDBEvent($event): void { // Extract information from the event record $operationType = $event['operationType'] ?? 'Unknown'; $db = $event['ns']['db'] ?? 'Unknown'; $collection = $event['ns']['coll'] ?? 'Unknown'; $fullDocument = $event['fullDocument'] ?? []; // Log the event details echo "Operation type: $operationType\n"; echo "Database: $db\n"; echo "Collection: $collection\n"; echo "Full document: " . json_encode($fullDocument, JSON_PRETTY_PRINT) . "\n"; } } return new DocumentDBEventHandler();
- Python
-
- SDK for Python (Boto3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming a Amazon DocumentDB event with Lambda using Python.
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
-
- SDK for Ruby
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming a Amazon DocumentDB event with Lambda using Ruby.
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
- Rust
-
- SDK for Rust
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Serverless examples
repository. Consuming a Amazon DocumentDB event with Lambda using Rust.
use lambda_runtime::{service_fn, tracing, Error, LambdaEvent}; use aws_lambda_events::{ event::documentdb::{DocumentDbEvent, DocumentDbInnerEvent}, }; // Built with the following dependencies: //lambda_runtime = "0.11.1" //serde_json = "1.0" //tokio = { version = "1", features = ["macros"] } //tracing = { version = "0.1", features = ["log"] } //tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] } //aws_lambda_events = "0.15.0" async fn function_handler(event: LambdaEvent<DocumentDbEvent>) ->Result<(), Error> { tracing::info!("Event Source ARN: {:?}", event.payload.event_source_arn); tracing::info!("Event Source: {:?}", event.payload.event_source); let records = &event.payload.events; if records.is_empty() { tracing::info!("No records found. Exiting."); return Ok(()); } for record in records{ log_document_db_event(record); } tracing::info!("Document db records processed"); // Prepare the response Ok(()) } fn log_document_db_event(record: &DocumentDbInnerEvent)-> Result<(), Error>{ tracing::info!("Change Event: {:?}", record.event); Ok(()) } #[tokio::main] async fn main() -> Result<(), Error> { tracing_subscriber::fmt() .with_max_level(tracing::Level::INFO) .with_target(false) .without_time() .init(); let func = service_fn(function_handler); lambda_runtime::run(func).await?; Ok(()) }
For a complete list of AWS SDK developer guides and code examples, see Using Lambda with an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.