Here is the code used for lambda function. S3 bucket has an event to trigger the following lambda function when a file is added. I want to sync the Knowledgebase with s3 bucket when a new file is added to S3 bucket.
Python 3.12 is being used for this lambda function. The IAM policy has full access to S3 bucket and Knowledgebase as well.
import boto3
import json
def lambda_handler(event, context):
# Initialize the Bedrock client
bedrock = boto3.client('bedrock-agent-runtime')
try:
# Trigger the sync process for your knowledge base
response = bedrock.start_ingestion_job(
knowledgeBaseId='your-knowledgebase-id',
dataSourceId='your-datasource-id'
)
print(f"Sync process started. Job ID: {response['jobId']}")
return {
'statusCode': 200,
'body': json.dumps('Sync process initiated successfully')
}
except Exception as e:
print(f"Error: {str(e)}")
return {
'statusCode': 500,
'body': json.dumps('Error initiating sync process')
}
Error observed in CloudWatch Log stream:
Error: 'AgentsforBedrockRuntime' object has no attribute 'start_ingestion_job'