Overview
The AWS Knowledge Base does not refresh information from its data sources automatically. To refresh the information without manually clicking the button, admins need to configure automation.
High Level Steps
Previously created a Knowledge Base and Data Source, both working as expected
Create an AWS Lambda Function
Create an AWS EventBridge
Never think about it again.
Prerequisites
Admins need the following values to complete the configuration.
Admin rights to AWS
Knowledge Base ID (for the KB to refresh)
Data Source ID (for the data source of the KB to refresh)
AWS Lambda Function
Create the Lambda
This function uses variables and can handle multiple different KBs and Data Sources by passing in the appropriate values. Admins pass these values when they create the EventBridge later.
Login to AWS and go to the Lambda screen while in the proper region
Select Functions > Create function
Give the Function name
Change the Runtime to Python 3.xx
Select Create function
Replace existing code in the lamda_function.py with the following
import jsonimport boto3import osimport logging# Set up logginglogger = logging.getLogger()logger.setLevel(logging.INFO)# Initialize AWS client for Amazon Bedrock Knowledge Basesbedrock_agent = boto3.client('bedrock-agent')def lambda_handler(event, context): logger.info(f"Received event: {json.dumps(event)}") try: # Extract KB ID and data source ID from the event # These can be passed from QuickSight schedule kb_id = event.get('kb_id') data_source_id = event.get('data_source_id') # Validate the required parameters if not kb_id or not data_source_id: error_msg = "Missing required parameters. Both 'kb_id' and 'data_source_id' are required." logger.error(error_msg) return { 'statusCode': 400, 'body': json.dumps({'error': error_msg}) } # Start the data source refresh response = bedrock_agent.start_ingestion_job( knowledgeBaseId=kb_id, dataSourceId=data_source_id ) logger.info(f"Started ingestion job: {response}") return { 'statusCode': 200, 'body': json.dumps({ 'message': 'Data source refresh started successfully', 'ingestionJobId': response.get('ingestionJobId'), 'knowledgeBaseId': kb_id, 'dataSourceId': data_source_id }) } except Exception as e: logger.error(f"Error refreshing data source: {str(e)}") return { 'statusCode': 500, 'body': json.dumps({'error': str(e)}) }
Update the Permissions
Select Configuration > Permissions
Open the Role name by clicking it
Select Add permissions > Create inline policy
Select JSON
Paste the following into the Policy editor (replacing everything else) to allow access to the Lambda to refresh the data source.
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "bedrock:StartIngestionJob" ], "Resource": [ "arn:aws:bedrock:us-east-1:905418109363:knowledge-base/*" ] } ] }Scroll down and click Next
Give it a name and click Create policy
The Role should be updated and shown on the screen
Create an AWS EventBridge
The EventBridge sets up a schedule to run the Lambda function and sends it variables for the KB and Data Source. Admins can create multiple EventBridge configurations to refresh all of an organization's KB data sources.
Login to AWS and go to the EventBridge screen while in the proper region
In Getting started select EventBridge Schedule > Create schedule
Create a name and description for the schedule
Select Recurring schedule
Enter information into the cron fields. Shown is a schedule that will run every morning at 1:10AM Pacific time. Every day of the year.
Note the "?" in Day of the week
Avoid setting a frequency under 24 hours without considering how long it might take to ingest and refresh the information.
Scroll down
Verify the desired refresh times using the Next 10 trigger dates.
Adjust the cron times if needed
Select an option in the Flexible time window
Setting means it will run within the defined minutes of the scheduled time
Click Next when ready
Select AWS Lambda Invoke
Select the Lambda function created in the previous steps, in the dropdown
Scroll down to the Payload screen and insert the following into the window.
Replace the two variables in green with the Knowledge Base ID and Data Source ID.
Scroll down and click Next
On the Next screen click Next
On the final screen validate the Schedule details and Target details
When ready click Create schedule
Check the Knowledge Base and the Data Source after the next expected run to confirm it completed successfully.
Admins can adjust the time to trigger a few minutes after setup, so they don't need to wait until the next day to confirm the schedule works.
Adjusting the time this way confirms that timing and permissions are all as expected.
