AWS AppSync and Amazon Cognito to Amazon Bedrock via AWS Lambda

AWS AppSync → Amazon Cognito → AWS Lambda → Amazon Bedrock

Create an AWS AppSync GraphQL API protected by Amazon Cognito that invokes Amazon Bedrock through an AWS Lambda resolver.

This sample project demonstrates how to secure an AWS AppSync GraphQL API with Amazon Cognito User Pools and route an authenticated mutation to an AWS Lambda resolver.
The Lambda function uses the AWS SDK for JavaScript v3 to invoke an Amazon Bedrock foundation model and returns the generated response back through AppSync to the caller.
This pattern deploys a Cognito User Pool and client, an AppSync API, a Lambda data source and resolver, and the IAM permissions required for Bedrock model invocation.

< 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/cognito-appsync-bedrock

Deploy

Change to the CDK project folder: <code>cd cognito-appsync-bedrock/cdk</code> and install dependencies with <code>npm install</code>.Verify the active AWS account and region used by your AWS CLI, and bootstrap the environment if needed: <code>aws sts get-caller-identity</code>, <code>aws configure get region</code>, <code>./node_modules/.bin/cdk bootstrap aws://YOUR_ACCOUNT_ID/YOUR_AWS_REGION</code>.Build and deploy with <code>npm run build</code> and <code>npm run deploy</code>.Verify that <code>cdk-outputs.json</code> was created because the integration tests depend on that file.


Testing

Run the full test suite from the CDK project folder with npm run test.
When cdk-outputs.json exists, the live Cognito-AppSync-Bedrock integration test runs; otherwise only the local transport unit test runs.

Cleanup

Delete the stack from the CDK project folder with cd cognito-appsync-bedrock/cdk and ./node_modules/.bin/cdk destroy AppsyncBedrockCognitoStack.
Confirm the destroy prompt, then verify in CloudFormation that AppsyncBedrockCognitoStack no longer exists.
Local files such as cdk-outputs.json and shared CDK bootstrap resources are not removed automatically.

Created by:

Vidit Shah

Vidit Shah

Software Engineer working @ServerlessCreed,making Serverless Courses and workshops

Follow on LinkedIn