Skip to content

Commit d7f4aaf

Browse files
committed
Listen for all object events to keep knowledge base in sync
1 parent 62ab3f0 commit d7f4aaf

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

packages/cdk/constructs/S3LambdaNotification.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ export class S3LambdaNotification extends Construct {
1212
constructor(scope: Construct, id: string, props: S3LambdaNotificationProps) {
1313
super(scope, id)
1414

15-
// Use CDK's built-in S3 notification
16-
props.bucket.addEventNotification(
17-
EventType.OBJECT_CREATED,
18-
new LambdaDestination(props.lambdaFunction)
19-
)
15+
const lambdaDestination = new LambdaDestination(props.lambdaFunction)
16+
17+
// Listen for all object events to keep knowledge base in sync
18+
props.bucket.addEventNotification(EventType.OBJECT_CREATED, lambdaDestination)
19+
props.bucket.addEventNotification(EventType.OBJECT_REMOVED, lambdaDestination)
20+
props.bucket.addEventNotification(EventType.OBJECT_RESTORE_COMPLETED, lambdaDestination)
2021
}
2122
}

packages/syncKnowledgeBaseFunction/app.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def handler(event, context):
1717
"""
1818
Lambda handler that processes S3 events and triggers Bedrock Knowledge Base ingestion.
1919
20-
This function is triggered when documents are uploaded to the S3 bucket and automatically
21-
starts an ingestion job to update the knowledge base with new content.
20+
This function is triggered when documents are created, updated, deleted, or restored in the S3 bucket
21+
and automatically starts an ingestion job to sync the knowledge base with current content.
2222
"""
2323
# Record start time for performance tracking
2424
start_time = time.time()
@@ -88,12 +88,12 @@ def handler(event, context):
8888
},
8989
)
9090

91-
# Start ingestion job for the knowledge base
91+
# Start ingestion job for the knowledge base (handles all event types)
9292
ingestion_start_time = time.time()
9393
response = bedrock_agent.start_ingestion_job(
9494
knowledgeBaseId=knowledge_base_id,
9595
dataSourceId=data_source_id,
96-
description=f"Auto-sync triggered by S3 event: {event_name} on {key}",
96+
description=f"Auto-sync triggered by S3 {event_name} on {key}",
9797
)
9898
ingestion_request_time = time.time() - ingestion_start_time
9999

0 commit comments

Comments
 (0)