Skip to content

Commit 75b1afc

Browse files
committed
feat(infrastructure): Inject API URL into frontend build via CI/CD
- Add build-args to Dockerfile.prod for dynamic API URL injection from GitHub Secrets - Update Bedrock client initialization to use AWS SDK's default credential chain for credential resolution - Enhance client configuration for improved security and flexibility in production environments
1 parent 872fb25 commit 75b1afc

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

.github/workflows/deploy-infrastructure.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ jobs:
178178
tags: |
179179
${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_FRONTEND_REPO }}:${{ github.sha }}
180180
${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_FRONTEND_REPO }}:latest
181+
build-args: |
182+
NEXT_PUBLIC_API_URL=${{ secrets.FASTAPI_BACKEND }}
181183
cache-from: type=gha
182184
cache-to: type=gha,mode=max
183185

backend/routers/voice_simple.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from aws_sdk_bedrock_runtime.client import BedrockRuntimeClient, InvokeModelWithBidirectionalStreamOperationInput
1717
from aws_sdk_bedrock_runtime.models import InvokeModelWithBidirectionalStreamInputChunk, BidirectionalInputPayloadPart
1818
from aws_sdk_bedrock_runtime.config import Config
19-
from smithy_aws_core.identity.environment import EnvironmentCredentialsResolver
2019

2120
from config import settings
2221
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
203202
logger.info(f"[NovaSonic] Initialized with prompt_name={self.prompt_name}, agent_id={agent_id}")
204203

205204
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)
209223

210224
config = Config(
211225
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",
214228
)
215229
self.client = BedrockRuntimeClient(config=config)
216-
logger.info("[NovaSonic] Bedrock client initialized")
230+
logger.info("[NovaSonic] Bedrock client initialized with default credential chain")
217231

218232
async def send_event(self, event_json: str):
219233
"""Send an event to the stream."""

frontend/Dockerfile.prod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ FROM node:20-alpine AS builder
1111
WORKDIR /app
1212
ENV NODE_ENV=production
1313
ENV NEXT_TELEMETRY_DISABLED=1
14-
# Bake the API URL directly into the build
15-
ARG NEXT_PUBLIC_API_URL=https://d3cp7cujulcncl.cloudfront.net
14+
# API URL will be injected via build-arg from CI/CD (GitHub Secrets)
15+
ARG NEXT_PUBLIC_API_URL
1616
ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL
1717
COPY --from=deps /app/node_modules ./node_modules
1818
COPY . .
1919

20-
# Build Next.js app with API URL baked in
20+
# Build Next.js app with API URL baked in from build-arg
2121
RUN npm run build
2222

2323
FROM node:20-alpine AS runner

0 commit comments

Comments
 (0)