Connect a managed knowledge base to AWS Context
To connect a knowledge base, you set up access for two separate purposes, create a connection, and then bind the connection into your Agent Context:
-
Ingestion – Amazon Bedrock sends the knowledge base’s content to AWS Context. For this, you always create an ingestion role, which Amazon Bedrock assumes.
-
Queries – AWS Context reads the knowledge base when an agent queries the Agent Context. For this, you either grant read permissions to the principals that call the Agent Context, or create a second role, the data access role, which AWS Context assumes. For how to choose, see Choose how queries read the knowledge base.
The two roles have different trust and permissions policies. Don’t combine them into one role.
Before you begin, make sure that you have the following:
-
A managed knowledge base in the same AWS account and Region as the Agent Context. For how to create one, see Create a managed knowledge base in the Amazon Bedrock User Guide. To connect a knowledge base in another account, see When to use cross-account access.
-
At least one data source in the knowledge base. For the connector for each source, see the topics that follow and Connect a data source in the Amazon Bedrock User Guide.
-
If you want AWS Context to return documents to each user based on the source’s permissions, turn on ACLs for each data source. For more information, see Document ACLs from your connectors.
To connect a managed knowledge base
-
Create the ingestion role. You need this role with either query option. Its trust policy allows Amazon Bedrock to assume the role, and only on behalf of your knowledge base. In the following trust policy, replace the example account ID, Region, and knowledge base ID with your own values.
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "bedrock.amazonaws.com" }, "Action": "sts:AssumeRole", "Condition": { "StringEquals": { "aws:SourceAccount": "111122223333" }, "ArnLike": { "aws:SourceArn": "arn:aws:bedrock:us-east-1:111122223333:knowledge-base/KB12345678" } } } ] }Attach a permissions policy that allows the role to send the knowledge base’s content into your Agent Context. The
context:SourceArncondition limits ingestion to content from this knowledge base.{ "Version": "2012-10-17", "Statement": [ { "Sid": "IngestFromKnowledgeBase", "Effect": "Allow", "Action": "context:IngestAgentContext", "Resource": "*", "Condition": { "ArnEquals": { "context:SourceArn": "arn:aws:bedrock:us-east-1:111122223333:knowledge-base/KB12345678" } } }, { "Sid": "SendKnowledgeBaseEntities", "Effect": "Allow", "Action": "bedrock:SendKnowledgeBaseEntities", "Resource": "arn:aws:bedrock:us-east-1:111122223333:knowledge-base/KB12345678" } ] } -
Set up the permissions for queries. They’re separate from the ingestion role: don’t add the following policy to the ingestion role. The following permissions policy allows reading from the knowledge base.
{ "Version": "2012-10-17", "Statement": [ { "Sid": "RetrieveFromKnowledgeBase", "Effect": "Allow", "Action": [ "bedrock:Retrieve", "bedrock:GetDocumentContent", "bedrock:GetKnowledgeBase" ], "Resource": "arn:aws:bedrock:us-east-1:111122223333:knowledge-base/KB12345678" } ] }Attach the policy in one of the following ways, depending on the query option that you chose:
-
The caller’s credentials (default) – Attach the policy to the roles of the principals that call the Agent Context, for example in an IAM Identity Center permission set. You don’t create a second role.
-
A data access role – Create a second role, the data access role, and attach the policy to it. The role’s trust policy allows AWS Context to assume it:
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": "context.amazonaws.com" }, "Action": "sts:AssumeRole" } ] }
-
-
Make sure that the principal that creates the connection can pass the ingestion role, and the data access role if you use one (
iam:PassRole). -
Create the connection with
CreateConnection. SetdataSource.typetoBEDROCK_KNOWLEDGE_BASEanddataSource.location.arnto the knowledge base ARN. Set the ingestion role iningestionConfig. To use a data access role, set it inaccessConfig. To use the caller’s credentials, omitaccessConfig. The following example uses a data access role.{ "name": "support-documents", "dataSource": { "type": "BEDROCK_KNOWLEDGE_BASE", "location": { "arn": "arn:aws:bedrock:us-east-1:111122223333:knowledge-base/KB12345678" } }, "accessConfig": { "iamRole": { "roleArn": "arn:aws:iam::111122223333:role/ContextDataAccessRole" } }, "ingestionConfig": { "roleArn": "arn:aws:iam::111122223333:role/ContextIngestionRole" } }If the knowledge base is encrypted with a customer managed AWS KMS key, add
CUSTOMER_MANAGED_KEYtoacknowledgements. This acknowledgement confirms that AWS Context stores the ingested content under AWS owned keys. Without it,CreateConnectionreturns aValidationException. -
Bind the
ACTIVEconnection into your Agent Context throughconnectionMappings. For more information, see Create a data mapping.
After you bind the connection, the knowledge base sends content to AWS Context each time one of its data sources syncs. AWS Context doesn’t schedule the sync. To control when content changes, set a sync schedule on each data source, or start a sync manually. For more information, see Sync a data source in the Amazon Bedrock User Guide.