Skip to content

Conversation

@kaustavbecs
Copy link
Contributor

Issue #, if available:

Description of changes:

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.

How it works

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 Instructions

  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  

You should see output similar to:

Starting subscription...
Starting conversation...
StartConversation response: {
data: {
startConversation: {
conversationId: '123e4567-e89b-12d3-a456-426614174000',
status: 'STARTED'
}
}
}
Received chunk: {
conversationId: '123e4567-e89b-12d3-a456-426614174000',
chunk: "Here's a joke for you: Why don't scientists trust atoms? Because they make"
}
Received chunk: {
conversationId: '123e4567-e89b-12d3-a456-426614174000',
chunk: 'up everything!'
}

Cleanup

  1. Delete the stack
cdk destroy --all

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copy link
Contributor

@marcojahn marcojahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @kaustavbecs,
I've checked your PR and added a few requests for changes. Additionally I was not able to deploy you pattern (checked twice) due to failures (missing mutation, pls see comments for details).

Can you please add the file example.pattern.json (template here: https://github.com/aws-samples/serverless-patterns/tree/main/_pattern-model)?

Will re-check your PR after changes made.

@kaustavbecs
Copy link
Contributor Author

Hi @marcojahn
Thanks for checking this. Updated the code with your suggestions. Please review

Copy link
Contributor

@marcojahn marcojahn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested your example and was not able to run it due to errors. Maybe just some hard coded account IDs left. Made a few more recommendations as well.

My issue is
"logType": "RequestMapping",

User: arn:aws:sts::***:assumed-role/AppsyncLambdaBedrockAsync-InvocationHandlerServiceR-PMaxGzynGwhQ/AppsyncLambdaBedrockAsync-InvocationHandlerF4117C8-rYm0jSr942Jk is not authorized to perform: bedrock:InvokeModelWithResponseStream on resource: arn:aws:bedrock:us-west-2:***:inference-profile/us.anthropic.claude-3-5-sonnet-20241022-v2:0 because no identity-based policy allows the bedrock:InvokeModelWithResponseStream action

I think I did fix this error on my deployment. But then I was stuck running the test, not receiving chunks. Did check logs but did not found any obvious error. Can you check your PR in another AWS account and another region than us-east-1?

@kaustavbecs
Copy link
Contributor Author

@marcojahn : Can you please check now

@marcojahn
Copy link
Contributor

Hello @kaustavbecs, thank you for your contribution. Your PR will be merged to serverlessland.com by a DA soon.

@julianwood julianwood merged commit 8f4d800 into aws-samples:main Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants