Skip to content

New pattern submission #2554

@kaustavbecs

Description

@kaustavbecs

Use the model template located at https://github.com/aws-samples/serverless-patterns/tree/main/_pattern-model to set up a README, template and any associated code.

Description (mid-length e.g. "Create a Lambda function that sends events to EventBridge.")

This pattern demonstrates how to implement long-running invocations with Amazon Bedrock using AWS AppSync subscriptions and AWS Lambda in Event Mode, following the official AWS AppSync documentation pattern.

Language: (optional e.g. "Python", if you have a Lambda function in your example)

Typescript

YouTube videoId (optional e.g. "VI79XQW4dIM")

Framework (currently we support SAM or CDK)

CDK

Services from/to (e.g. "Lambda to EventBridge)

AppSync to Lambda to Bedrock Streaming

Description (this must include a throughout explanation of the pattern together with details of IAM permissioning)

The pattern implements an asynchronous streaming architecture where:

  1. Client initiates a WebSocket subscription and makes a request to AppSync
  2. AppSync invokes Lambda function in Event mode, enabling asynchronous processing
  3. Lambda function streams responses from Bedrock using ConverseStream
  4. Lambda sends updates via mutations to AppSync
  5. Updates are delivered to client through WebSocket subscription

Key Benefits

  • Asynchronous Processing: AppSync immediately returns a response while Lambda processes the request asynchronously, preventing timeouts for long-running operations
  • Real-time Updates: Clients receive progressive updates through WebSocket subscriptions as the model generates responses
  • Scalable Architecture: Event-driven design allows handling multiple concurrent requests without blocking
  • Enhanced User Experience: Progressive updates enable responsive interfaces even during lengthy AI model invocations

Deployment commands

  1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository:
git clone https://github.com/aws-samples/serverless-patterns
  1. Change directory to the pattern directory:
cd appsync-lambda-bedrock-async-stream-subscription-cdk
  1. Install the required dependencies:
npm install
  1. Deploy the stack to your default AWS account and region:
npm run deploy

Testing

After deployment, you can test the Bedrock streaming integration using the provided test script. The script demonstrates:

  • WebSocket subscription initialization
  • Conversation start with Bedrock
  • Real-time streaming chunks display
  • Graceful cleanup on exit

Run the test script using:

npx tsx test/test.ts  

GitHub PR for template:

#2553

Payload example (e.g. Lambda event payload from source service).

N/A

Additional resources (optional: link and anchor text, up to 5 resources)

https://docs.aws.amazon.com/appsync/latest/devguide/resolver-reference-bedrock-js.html#long-running-invocations

Author bio

Name: Kaustav Dey
Photo URL: https://github.com/kaustavbecs.png
Twitter handle: n/a
LinkedIn: https://www.linkedin.com/in/kaustavbecs/
Description (up to 255 chars): I am a Solutions Architect working for AWS.

GitHub PR for template:

#2553

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions