Terraform project to deploy an AWS Bedrock Agent with a connected Knowledge Base backed by S3 and OpenSearch.
The setup includes:
-
Bedrock Agent
-
Bedrock Knowledge Base
-
S3 bucket (for documents)
-
OpenSearch collection + index
-
Configurable chunking strategies
-
AWS CLI configured (
aws configure) -
Permissions to create:
-
S3 buckets
-
Bedrock resources
-
OpenSearch collections
-
IAM roles/policies
-
- Clone the repo
git clone -b dev https://github.com/dmdcl/BedrockTest.git
cd BedrockTest- Initialize Terraform
terraform init- Create a
terraform.tfvarsfile in the root folder with your values:
aws_region = "us-east-1"
project_name = "myproject"
environment = "dev"
kb_s3_bucket_name = "my-kb-bucket" # Optional: if you want to bring your own bucket
agent_name = "my-agent"
agent_model_id = "anthropic.claude-v2"
agent_instruction = "You are a helpful assistant."
agent_description = "Agent for answering KB queries"
kb_name = "my-knowledge-base"
kb_description = "KB with documents"
kb_embedding_model_id = "amazon.titan-embed-text-v1"
opensearch_collection_name = "kb-collection"
opensearch_index_name = "kb-index"
chunking_strategy = "FIXED_SIZE"
fixed_size_max_tokens = 300
fixed_size_overlap_percentage = 10
tags = {
Owner = "me"
}-
modules/s3.tf→ Creates the S3 bucket if you don’t provide your own. -
modules/main.tf→ Core logic: agent, KB, and OpenSearch.
-
By default, the repo creates its own S3 bucket for the KB.
-
If you already have an S3 bucket you want to use:
-
Pass its name via
kb_s3_bucket_nameinterraform.tfvars. -
Uncomment the corresponding lines in
modules/main.tf:
data "aws_s3_bucket" "kb" { bucket = var.aws_s3_bucket }
And comment out the
aws_s3_bucket.kbresource inmodules/s3.tf. -
terraform plan
terraform applyConfirm with yes. Terraform will create the agent, KB, S3 bucket (if enabled), and OpenSearch resources.
To tear everything down:
terraform destroy- Copy your documents to the S3 bucket:
aws s3 cp ./documents s3://<kb-s3-bucket-name>/ --recursive- Start ingestion in Bedrock:
aws bedrock-agent start-ingestion-job \
--knowledge-base-id <knowledge_base_id> \
--data-source-id <data_source_id> \
--region <aws_region>You can get the values from the Terraform outputs:
terraform outputaws bedrock-agent-runtime invoke-agent \
--agent-id <agent_id> \
--session-id test-session \
--input-text "Hello, what can you help me with?" \
--region <aws_region>-
Monitor ingestion and queries via the AWS Console.
-
Adjust chunking strategy, vector dimensions, and OpenSearch settings as needed.
