

# 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](unstructured-query-access.md).

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](https://docs.aws.amazon.com/bedrock/latest/userguide/kb-managed-create.html) in the *Amazon Bedrock User Guide*. To connect a knowledge base in another account, see [When to use cross-account access](cross-account-when.md).
+ 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](https://docs.aws.amazon.com/bedrock/latest/userguide/kb-managed-connect-ds.html) 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](unstructured-acls.md).

**To connect a managed knowledge base**

1. 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.

   ```json
   {
     "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:SourceArn` condition limits ingestion to content from this knowledge base.

   ```json
   {
     "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}}"
       }
     ]
   }
   ```

1. 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.

   ```json
   {
     "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:

     ```json
     {
       "Version": "2012-10-17",
       "Statement": [
         {
           "Effect": "Allow",
           "Principal": {
             "Service": "context.amazonaws.com"
           },
           "Action": "sts:AssumeRole"
         }
       ]
     }
     ```

1. 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`).

1. Create the connection with `CreateConnection`. Set `dataSource.type` to `BEDROCK_KNOWLEDGE_BASE` and `dataSource.location.arn` to the knowledge base ARN. Set the ingestion role in `ingestionConfig`. To use a data access role, set it in `accessConfig`. To use the caller’s credentials, omit `accessConfig`. The following example uses a data access role.

   ```json
   {
     "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_KEY` to `acknowledgements`. This acknowledgement confirms that AWS Context stores the ingested content under AWS owned keys. Without it, `CreateConnection` returns a `ValidationException`.

1. Bind the `ACTIVE` connection into your Agent Context through `connectionMappings`. For more information, see [Create a data mapping](create-a-data-mapping.md).

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](https://docs.aws.amazon.com/bedrock/latest/userguide/kb-managed-sync.html) in the *Amazon Bedrock User Guide*.