Streaming AI agent chat with AWS Lambda Web Functions and Amazon Bedrock AgentCore Runtime

Amazon CloudFront → AWS Lambda Web Functions → AWS Secrets Manager → Amazon Bedrock AgentCore Runtime → Amazon Bedrock

Serve a chat UI from an AWS Lambda Web Function behind Amazon CloudFront and stream a Strands agent's answer from AgentCore Runtime token by token.

This pattern deploys a fully serverless chat app for an AI agent. An Express server running as an AWS Lambda Web Function serves the chat UI and calls a Strands agent hosted on Amazon Bedrock AgentCore Runtime, which calls Claude Haiku 5.5 on Amazon Bedrock. The answer streams back as server-sent events, token by token, from the model through AgentCore Runtime and the Web Function to the browser.
A Web Function runs a real, long-lived server process, so the server holds each chat response open and writes every chunk the moment it arrives. Amazon CloudFront is the app's single public URL. It adds a secret origin header that the server checks, so the Web Function endpoint cannot be used to bypass CloudFront, and it is where you would attach a custom domain or AWS WAF.
The agent is deployed to AgentCore Runtime as a .zip file with direct code deployment, so there is no container image, Docker or Amazon ECR repository. AgentCore Runtime runs each session in its own microVM, and every sign-in gets its own session, so every user gets a private, multi-turn conversation.
The app asks for a login secret that Terraform generates and stores in AWS Secrets Manager, then issues a signed session token that the browser sends with every message. Prompts are capped at 100 characters and answers at 200 output tokens. Both compute services scale to zero when idle and bill for the CPU your code actually uses rather than time spent waiting for the model.

< Back to all patterns

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/lambda-web-agentcore/nodejs/terraform

Deploy

terraform initterraform apply -var "aws_region=us-east-1"


Testing

Print the login secret: aws secretsmanager get-secret-value --secret-id "$(terraform output -raw login_secret_arn)" --region us-east-1 --query SecretString --output text, using the Region you deployed to. Open the app_url output in a browser, enter the login secret and chat with the agent. See the GitHub repo for detailed testing instructions.

Cleanup

Delete the resources: terraform destroy -var "aws_region=us-east-1", using the Region you deployed to.

Created by:

Manish S

Manish S

AWS Support Engineer, trying to build things

Anton Aleksandrov

Anton Aleksandrov

Anton Aleksandrov is a Principal Solutions Architect for AWS Serverless and Event-Driven architectures.