|
16 | 16 | from aws_sdk_bedrock_runtime.client import BedrockRuntimeClient, InvokeModelWithBidirectionalStreamOperationInput |
17 | 17 | from aws_sdk_bedrock_runtime.models import InvokeModelWithBidirectionalStreamInputChunk, BidirectionalInputPayloadPart |
18 | 18 | from aws_sdk_bedrock_runtime.config import Config |
19 | | -from smithy_aws_core.identity.environment import EnvironmentCredentialsResolver |
20 | 19 |
|
21 | 20 | from config import settings |
22 | 21 | from services.agent_service import AgentService |
@@ -203,17 +202,32 @@ def __init__(self, voice_prompt: str, agent_id: str, user_id: str, session_id: s |
203 | 202 | logger.info(f"[NovaSonic] Initialized with prompt_name={self.prompt_name}, agent_id={agent_id}") |
204 | 203 |
|
205 | 204 | def _initialize_client(self): |
206 | | - """Initialize the Bedrock client.""" |
207 | | - # Config with environment credentials (no IMDS for local dev) |
208 | | - credentials_resolver = EnvironmentCredentialsResolver() |
| 205 | + """Initialize the Bedrock client with proper credential resolution. |
| 206 | + |
| 207 | + Uses AWS SDK's default credential chain which automatically handles: |
| 208 | + - Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) for local dev |
| 209 | + - ECS Task Role credentials via container metadata endpoint for production |
| 210 | + - IAM instance profile for EC2 |
| 211 | + """ |
| 212 | + from smithy_aws_core.identity.chain import create_default_chain |
| 213 | + from smithy_http.aio.aiohttp import AIOHTTPClient |
| 214 | + |
| 215 | + # Create HTTP client for credential resolution (needed by ContainerCredentialsResolver) |
| 216 | + http_client = AIOHTTPClient() |
| 217 | + |
| 218 | + # Create default credential chain that tries in order: |
| 219 | + # 1. Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) |
| 220 | + # 2. ECS container credentials (via AWS_CONTAINER_CREDENTIALS_RELATIVE_URI) |
| 221 | + # 3. EC2 instance metadata service (IMDS) |
| 222 | + credentials_resolver = create_default_chain(http_client) |
209 | 223 |
|
210 | 224 | config = Config( |
211 | 225 | region=settings.AWS_REGION, |
212 | | - aws_credentials_identity_resolver=EnvironmentCredentialsResolver(), |
213 | | - endpoint_uri=f"https://bedrock-runtime.{settings.AWS_REGION}.amazonaws.com", |
| 226 | + aws_credentials_identity_resolver=credentials_resolver, |
| 227 | + endpoint_uri=f"https://bedrock-runtime.{settings.AWS_REGION}.amazonaws.com", |
214 | 228 | ) |
215 | 229 | self.client = BedrockRuntimeClient(config=config) |
216 | | - logger.info("[NovaSonic] Bedrock client initialized") |
| 230 | + logger.info("[NovaSonic] Bedrock client initialized with default credential chain") |
217 | 231 |
|
218 | 232 | async def send_event(self, event_json: str): |
219 | 233 | """Send an event to the stream.""" |
|
0 commit comments