This implementation uses a Lambda Function URL with response streaming. The Node.js handler forwards Bedrock AgentCore chunks to the client as they arrive, enabling real-time output.
Client (curl) -> Lambda Function URL -> Bedrock AgentCore
^
| Streams chunks immediately
✅ Pros:
- Real-time streaming for long responses
- Lower latency for the first tokens
- Lower cost (no API Gateway)
- Simpler architecture
❌ Cons:
- Function URLs lack API Gateway features
- Auth must be handled via IAM or in-code logic
cd option2-lambda-url
sam build
sam deploy --guided --parameter-overrides AgentRuntimeArn=YOUR_AGENT_ARNNote: sam build installs dependencies from package.json.
The Lambda execution role automatically gets:
{
"Effect": "Allow",
"Action": "bedrock-agentcore:InvokeAgentRuntime",
"Resource": "arn:aws:bedrock-agentcore:REGION:ACCOUNT:runtime/*"
}AGENT_RUNTIME_ARN: Set via CloudFormation parameter during deployment
curl -X POST YOUR_FUNCTION_URL --no-buffer \
-H "Content-Type: application/json" \
-d '{"prompt": "Tell me a story"}'curl -X POST YOUR_FUNCTION_URL --no-buffer \
-H "Content-Type: application/json" \
-d '{
"prompt": "What is the weather today?",
"session_id": "my-custom-session-id-that-is-at-least-33-chars"
}'- Responses are streamed as they arrive from Bedrock AgentCore.
- The function sets
Content-Type: text/event-streamby default. - The session ID is returned in the
X-Session-Idresponse header.
cd option2-lambda-url
sam deleteOr via CloudFormation:
aws cloudformation delete-stack --stack-name bedrock-streaming-stackindex.js- Lambda handler that streams chunks immediatelypackage.json- Node.js dependencies (AWS SDK v3)template.yaml- SAM template with Function URL response streamingtest_event.json- Sample test event for Lambda consoleREADME.md- This file