Amazon Bedrock Connectivity
Relevant source files
The following files were used as context for generating this wiki page:
Amazon Bedrock Connectivity serves as the core Large Language Model (LLM) integration layer for the pi-digby Slack bot. It utilizes the @mariozechner/pi-ai provider to interface with Bedrock models, specifically Claude Sonnet 4.6, providing the agent with its reasoning and tool-use capabilities.
The system is designed to run within AWS infrastructure, leveraging IAM roles for secure, credential-less authentication when deployed on ECS Fargate. It supports streaming responses, tool execution loops, and multimodal input (image processing) through the Bedrock API.
Architecture and Integration
The connectivity layer acts as a bridge between the local agent logic and the AWS Bedrock service. It is initialized during the agent setup phase and is consumed by the Agent class to process prompts.
Component Relationship
The following diagram illustrates how Amazon Bedrock integrates with the internal agent components:
The integration is primarily managed in src/agent/setup.ts, where the model is selected and the authentication strategy is defined.
Sources: src/agent/setup.ts:40-45, CLAUDE.md:1-15
Core Components Table
| Component | Responsibility | Source Reference |
|---|---|---|
| Model Provider | amazon-bedrock via @mariozechner/pi-ai | src/agent/setup.ts:42 |
| Model ID | us.anthropic.claude-sonnet-4-6 | src/agent/setup.ts:42 |
| Auth Strategy | ECS Task Role / IAM Credentials | src/agent/setup.ts:47-61 |
| Streaming | Enabled by default for real-time Slack updates | CLAUDE.md:58 |
Authentication and Security
Connectivity to Bedrock is secured through AWS Identity and Access Management (IAM). The application supports multiple credential resolution paths, prioritizing secure environment-based roles over static keys.
Credential Resolution
The getBedrockApiKey function checks for credentials in the following order:
AWS_PROFILEenvironment variable.AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEYpair.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI(automatically provided by ECS Fargate Task Roles).
Sources: src/agent/setup.ts:47-61, CLAUDE.md:50-52
IAM Policy Requirements
For the agent to function, the ECS Task Role (pi-digby-task) must have explicit permissions to invoke Bedrock models.
# Simplified policy from CloudFormation
Policies:
- PolicyName: bedrock-invoke
PolicyDocument:
Statement:
- Effect: Allow
Action:
- bedrock:InvokeModel
- bedrock:InvokeModelWithResponseStream
Resource: "*"
Sources: deploy/cloudformation.yml:195-204
Data Flow and Message Processing
When a user sends a message in Slack, the system prepares a payload for Bedrock that includes system instructions, conversation history, and any relevant media.
Request Orchestration
The following sequence diagram shows the flow of a single request to Amazon Bedrock:
Image Handling
Bedrock connectivity supports multimodal inputs. Images uploaded to Slack are:
- Downloaded to the local
/data/attachmentsdirectory. - Resized via
utils/image-resize.js. - Converted to Base64 and wrapped in
ImageContentobjects before being sent to Bedrock.
Sources: src/agent/setup.ts:223-260, src/main.ts:105-125
Infrastructure Configuration
The connectivity is supported by specific AWS infrastructure settings defined in the deployment templates.
| Parameter | Value / Description | Source |
|---|---|---|
| Region | us-east-1 (Default) | deploy/cloudformation.yml:345 |
| Compute | ECS Fargate (vCPUs: 1024, RAM: 2048) | deploy/cloudformation.yml:333-334 |
| Environment | AWS_DEFAULT_REGION passed to container | deploy/cloudformation.yml:344-345 |
Error Handling and Timeouts
The connectivity layer implements robust error handling to manage the ephemeral nature of API calls.
- Retries: While the Bedrock provider handles low-level retries, the
AgentSessionmanages the tool loop stability. - Timeouts: A run timeout is enforced (configured via
getRunTimeout()) to prevent hanging connections. If a timeout occurs,session.abort()is called to terminate the Bedrock stream. - Exception Mapping: API errors (like
max_tokensor timeout) are caught in therunmethod and translated into user-friendly Slack notifications (e.g., "Ran out of space mid-response").
Sources: src/agent/setup.ts:285-315, src/main.ts:153-158
Conclusion
Amazon Bedrock Connectivity is the foundational intelligence engine of pi-digby. By integrating Claude 3.5 Sonnet through the AWS SDK (via pi-ai), the system achieves a balance of high-reasoning capability and secure, scalable infrastructure execution. The use of streaming and multimodal support allows for a responsive and feature-rich Slack assistant experience.