Subchapter 23.20
references/knowledge-bases-setup.mdMarkdown18 KBView on GitHub
Assets
Kb Shim PyDeterministic procedure for creating a Bedrock Knowledge Base with a data source, configuring chunking strategy and vector store, running initial ingestion, and verifying the KB is queryable. This procedure is invoked from the bedrock skill when a user wants to build a RAG application.
s3 | web_crawler | confluence | sharepoint | salesforce | custom — additional types may be available, check aws bedrock-agent create-data-source help for current optionsfixed_size | semantic | hierarchical | none — see Step 2 for guidanceopensearch_serverless | aurora_postgresql | pinecone | redis | mongo_db_atlas | neptune_analytics | opensearch_managed_cluster | s3_vectors — see Step 3 for guidanceamazon.titan-embed-text-v2:0Constraints for parameter acquisition:
kb_name, data_source_type, and data source details) are provided. If any are missing, ask for them upfront in a single prompt.General constraints:
Constraints:
aws bedrock list-foundation-models --region <region>Constraints:
| Strategy | Best For | Tradeoff |
|---|---|---|
fixed_size | FAQs, short articles, uniform documents | Simple but may split semantic units. Chunk size 200-300 tokens, 10-20% overlap. |
semantic | Long-form content, technical docs, reports | Better quality but slower ingestion. |
hierarchical | Structured docs with chapters/sections (manuals, legal) | Best retrieval quality for structured docs but most complex. |
none | Pre-chunked data, documents under 300 tokens | No processing. |
Constraints:
| Vector Store | Best When | Setup Complexity |
|---|---|---|
| S3 Vectors | Simplest setup, AWS-managed, no infrastructure to configure | Low — Bedrock can auto-create |
| OpenSearch Serverless | No existing vector DB, most use cases, need advanced filtering | Medium — create collection + index |
| Aurora PostgreSQL | Already using Aurora, cost-sensitive | Medium — enable pgvector extension |
| Pinecone | Already using Pinecone | Low — create index + store API key in Secrets Manager |
| Redis Enterprise Cloud | Need lowest latency | Medium — create cluster with vector search module |
| MongoDB Atlas | Already using MongoDB | Medium — create vector index + store credentials in Secrets Manager |
| Neptune Analytics | Graph-based RAG use cases | Medium — create graph + configure |
| OpenSearch Managed Cluster | Existing self-managed OpenSearch | Medium — configure domain + index |
Additional vector stores may be available — refer to the latest AWS documentation on KB vector store setup (opens in a new tab) for current options.
vectorBucketArn), not a regular S3 bucketsecretsmanager:GetSecretValue permission on the secret ARN.Constraints:
create-knowledge-basecreate-knowledge-base. If you get an error indicating Bedrock cannot assume the role, retry with exponential backoff up to 3 attempts. IAM role creation is eventually consistent — newly created roles may not be immediately assumable by AWS services (see IAM eventual consistency (opens in a new tab))Trust policy allows bedrock.amazonaws.com to assume the role with confused deputy protection (source: AWS docs — KB trust relationship (opens in a new tab)):
aws iam create-role \
--role-name AmazonBedrockExecutionRoleForKB-<kb_name> \
--assume-role-policy-document '{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"Service": "bedrock.amazonaws.com"},
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {"aws:SourceAccount": "<account-id>"},
"ArnLike": {"aws:SourceArn": "arn:aws:bedrock:<region>:<account-id>:knowledge-base/*"}
}
}]
}'aws iam put-role-policy \
--role-name AmazonBedrockExecutionRoleForKB-<kb_name> \
--policy-name BedrockModelInvocation \
--policy-document '{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["bedrock:ListFoundationModels", "bedrock:ListCustomModels"],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": ["bedrock:InvokeModel"],
"Resource": ["arn:aws:bedrock:<region>::foundation-model/<embedding-model-id>"]
}
]
}'Replace <embedding-model-id> with the chosen embedding model (default: amazon.titan-embed-text-v2:0).
Attach permissions matching the data source type selected in Step 1:
s3:ListBucket and s3:GetObject on the bucketsecretsmanager:GetSecretValue for the credentials secretRefer to AWS docs — KB data source permissions (opens in a new tab) for the exact policy for each data source type.
Attach permissions matching the vector store selected in Step 3:
s3vectors:PutVectors, s3vectors:GetVectors, s3vectors:DeleteVectors, s3vectors:QueryVectors, s3vectors:GetIndex on the vector index ARN (arn:aws:s3vectors:<region>:<account-id>:bucket/<bucket-name>/index/<index-name>)aoss:APIAccessAll on the collection ARNrds:DescribeDBClusters, rds-data:BatchExecuteStatement, rds-data:ExecuteStatement on the cluster ARNRefer to AWS docs — KB service role permissions (opens in a new tab) for the exact policy JSON for each vector store type.
aws bedrock-agent create-knowledge-base \
--name <kb_name> \
--role-arn arn:aws:iam::<account-id>:role/AmazonBedrockExecutionRoleForKB-<kb_name> \
--knowledge-base-configuration '{"type":"VECTOR","vectorKnowledgeBaseConfiguration":{"embeddingModelArn":"arn:aws:bedrock:<region>::foundation-model/<embedding-model-id>"}}' \
--storage-configuration '<storage-config-from-step-3>'amazon.titan-embed-text-v2:0)create-knowledge-base fails with an error indicating Bedrock cannot assume the role, wait and retry with exponential backoff up to 3 attemptsknowledge-base/* with the specific KB IDConstraints:
aws bedrock-agent create-data-source --knowledge-base-id <kb-id> --name <name> --data-source-configuration '...'s3:GetObject and s3:ListBucket on the bucketConstraints:
aws bedrock-agent start-ingestion-job --knowledge-base-id <kb-id> --data-source-id <ds-id>COMPLETE or FAILED: aws bedrock-agent get-ingestion-job --knowledge-base-id <kb-id> --data-source-id <ds-id> --ingestion-job-id <job-id>FAILED, You MUST check:
s3:GetObject + s3:ListBucket)Constraints:
aws bedrock-agent-runtime retrieve --knowledge-base-id <kb-id> --retrieval-query '{"text":"<test-query>"}'aws bedrock-agent-runtime retrieve-and-generate --input '{"text":"<test-query>"}' --retrieve-and-generate-configuration '{"type":"KNOWLEDGE_BASE","knowledgeBaseConfiguration":{"knowledgeBaseId":"<kb-id>","modelArn":"<model-arn>"}}'These are KB-creation-specific security controls. For general Bedrock security, see the parent skill’s Security Considerations section.
Knowledge bases support customer-managed KMS keys at multiple encryption points. For HIPAA/GDPR workloads, You MUST recommend customer-managed KMS for all applicable points:
kms:GenerateDataKey and kms:Decrypt permissions for your KMS key to the KB service rolekms:Decrypt permission with kms:ViaService condition for s3.<region>.amazonaws.comRetrieveAndGenerate session data via --session-configuration '{"kmsKeyArn":"<kms-key-arn>"}' (covered in KB retrieval reference)Amazon Bedrock uses TLS encryption for communication with third-party data source connectors and vector stores where the provider supports TLS. Refer to the latest AWS documentation on KB encryption (opens in a new tab).
Source documents may contain PII/PHI. Once ingested, sensitive data is stored in the vector store and returned in retrieval results.
Constraints:
KB management operations (CreateKnowledgeBase, CreateDataSource, StartIngestionJob) are logged as CloudTrail management events by default. For compliance workloads, You SHOULD recommend setting up CloudWatch alarms on ingestion job failures. Refer to the latest AWS documentation on Bedrock CloudTrail logging (opens in a new tab).