Bedrock Agent Terraform Module
This module creates an AWS Bedrock Agent with full configuration flexibility including action groups, knowledge base associations, and orchestration settings. It supports both foundation models and inference profiles with automatic detection.
Features
- Flexible Agent Configuration: Custom instructions, model selection, and naming
- Inference Profile Support: Automatic detection and support for cross-region inference profiles
- IAM Management: Automatic IAM role and policy creation with least-privilege access including multi-region inference profile permissions
- Action Groups: Support for Lambda-based action groups with API/function schemas
- Knowledge Base Integration: Associate multiple knowledge bases with flexible configuration
- Advanced Orchestration: Custom prompt templates with file-based template support
- Security: KMS encryption support and configurable session timeouts
- Modular Design: Clean separation of concerns across multiple files
Usage
Basic Usage
module "bedrock_agent" {
source = "../../modules/bedrock-agent"
name = "my-agent"
namespace = "mycompany"
environment = "prod"
attributes = ["ai", "assistant"]
# Agent configuration
instructions = "You are a helpful AI assistant that can perform various tasks."
foundation_model_id = "eu.anthropic.claude-sonnet-4-20250514-v1:0" # Inference profile (auto-detected)
idle_session_timeout_seconds = 1800
# Knowledge base associations
knowledge_base_associations = [
{
knowledge_base_id = "KB123456789"
state = "ENABLED"
description = "General knowledge base"
},
{
knowledge_base_id = "KB987654321"
state = "ENABLED"
description = "Technical documentation"
}
]
tags = {
Project = "AI Assistant"
Owner = "AI Team"
}
}
Advanced Usage with Action Groups and File-based Templates
module "bedrock_agent" {
source = "../../modules/bedrock-agent"
name = "advanced-agent"
namespace = "mycompany"
environment = "prod"
# Agent configuration
instructions_file = "./templates/instructions.txt"
foundation_model_id = "anthropic.claude-3-7-sonnet-20250219-v1:0" # Foundation model
code_interpreter = true
user_input = true
# Action groups
action_groups = {
calculator = {
description = "Mathematical calculations"
lambda_function_arn = aws_lambda_function.calculator.arn
function_schema = {
functions = [
{
name = "calculate"
description = "Perform mathematical calculations"
parameters = {
type = "object"
required = ["expression"]
properties = [
{
name = "expression"
description = "Mathematical expression to evaluate"
type = "string"
required = true
}
]
}
}
]
}
}
}
# File-based orchestration templates
orchestration = {
use_file_templates = true
template_base_path = "./templates"
pre_processing = {
enabled = true
template = "pre_processing.json"
inference_configuration = {
temperature = 0.1
top_p = 0.9
max_length = 2048
}
}
orchestration = {
enabled = true
template = "orchestration.json"
inference_configuration = {
temperature = 0.2
top_p = 0.9
max_length = 4096
}
}
}
# Security
kms_key_arn = aws_kms_key.agent_encryption.arn
tags = {
Project = "Advanced AI Assistant"
Owner = "AI Team"
}
}
Inference Profile vs Foundation Model
The module automatically detects whether you're using an inference profile or foundation model based on the ID format:
- Inference Profile:
eu.anthropic.claude-sonnet-4-20250514-v1:0(has region prefix) - Foundation Model:
anthropic.claude-sonnet-4-20250514-v1:0(no region prefix)
Inference profiles provide:
- Cross-region capacity optimization
- Automatic failover between regions
- Better availability and performance
- Multi-region IAM permissions automatically configured
Requirements
| Name | Version |
|---|---|
| terraform | >= 1.12.1 |
| aws | ~> 6.0.0 |
| awscc | >= 1.43.0 |
| null | >= 3.2.4 |
| time | >= 0.13.1 |
Inputs
Core Configuration
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| name | The name of the agent | string |
n/a | yes |
| namespace | Namespace for resource naming | string |
n/a | yes |
| environment | Environment (dev, prod, etc.) | string |
n/a | yes |
| attributes | Additional attributes for naming | list(string) |
[] |
no |
| tags | Additional tags | map(string) |
{} |
no |
Agent Configuration
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| agent_name | Custom name for the agent | string |
null |
no |
| description | Description of the agent | string |
null |
no |
| instructions | Instructions for the agent | string |
null |
no |
| instructions_file | Path to file containing instructions | string |
null |
no |
| foundation_model_id | Foundation model ID or inference profile ID | string |
"eu.anthropic.claude-sonnet-4-20250514-v1:0" |
no |
| code_interpreter | Enable code interpreter capability | bool |
false |
no |
| user_input | Allow user input during agent execution | bool |
true |
no |
| idle_session_timeout_seconds | Idle session timeout in seconds | number |
3600 |
no |
| enable_session_summarization | Enable session summarization | bool |
false |
no |
| kms_key_arn | KMS key ARN for encryption | string |
null |
no |
Knowledge Base Configuration
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| knowledge_base_associations | List of knowledge base association configurations | list(object) |
null |
no |
Action Groups Configuration
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| action_groups | Map of action group configurations | map(object) |
{} |
no |
Orchestration Configuration
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| orchestration | Orchestration configuration with custom prompt templates | object |
null |
no |
Outputs
| Name | Description |
|---|---|
| agent_id | The ID of the Bedrock agent |
| agent_arn | The ARN of the Bedrock agent |
| agent_name | The name of the Bedrock agent |
| agent_role_arn | The ARN of the agent's IAM role |
| action_groups | Map of created action groups |
| knowledge_base_associations | Map of knowledge base associations |
Modules
| Name | Source | Version |
|---|---|---|
| label | cloudposse/label/null | 0.25.0 |
Inputs
| Name | Description | Type | Default | Required |
|---|---|---|---|---|
| action_groups | Map of action group configurations | map(object({ |
{} |
no |
| agent_collaboration | Agent collaboration mode - will be automatically set based on collaborator presence | string |
"DISABLED" |
no |
| agent_name | Custom name for the agent. If not provided, will be generated from name-environment-agent | string |
null |
no |
| attributes | Additional attributes | list(string) |
[] |
no |
| available_lambdas | Map of available lambda modules for ARN resolution | any |
{} |
no |
| code_interpreter | Enable code interpreter capability | bool |
false |
no |
| description | Description of the agent | string |
null |
no |
| enable_session_summarization | Enable session summarization | bool |
false |
no |
| environment | Environment | string |
n/a | yes |
| foundation_model_id | Foundation model ID or inference profile ID for the agent (automatically detected based on prefix) | string |
"eu.anthropic.claude-sonnet-4-20250514-v1:0" |
no |
| guardrail_configuration | Guardrail configuration for the agent | object({ |
null |
no |
| has_collaborators | Whether this agent will have collaborators (used to determine if it can be prepared and have aliases) | bool |
false |
no |
| idle_session_timeout_seconds | Idle session timeout in seconds | number |
3600 |
no |
| instructions | Instructions for the agent | string |
null |
no |
| instructions_file | Path to file containing instructions for the agent | string |
null |
no |
| kms_key_arn | KMS key ARN for encryption | string |
null |
no |
| knowledge_base_associations | List of knowledge base association configurations | list(object({ |
null |
no |
| knowledge_bases_map | Map of knowledge bases from the knowledge base module | any |
{} |
no |
| memory_configuration | Memory configuration for the agent | object({ |
{ |
no |
| name | The name of the agent | string |
n/a | yes |
| namespace | Namespace | string |
n/a | yes |
| orchestration | Orchestration configuration with custom prompt templates (can use file paths) | object({ |
null |
no |
| tags | Additional tags | map(string) |
{} |
no |
| user_input | Allow user input during agent execution | bool |
true |
no |
Outputs
| Name | Description |
|---|---|
| action_groups | Map of created action groups |
| agent_alias_arn | The ARN of the agent alias |
| agent_alias_id | The ID of the agent alias |
| agent_arn | The ARN of the Bedrock agent |
| agent_id | The ID of the Bedrock agent |
| agent_name | The name of the Bedrock agent |
| agent_role_arn | The ARN of the agent's IAM role |
| agent_role_name | The name of the agent's IAM role |
| agent_version | The version of the Bedrock agent |
| alias_stabilization_complete | Reference to alias stabilization completion for dependent modules |
| knowledge_base_associations | Map of knowledge base associations |