Amazon API Gateway to Amazon Bedrock and Amazon DynamoDB vector search

Amazon API Gateway HTTP API → AWS Lambda → Amazon Bedrock → Amazon DynamoDB

Build a semantic search API with API Gateway, Lambda, Bedrock embeddings, and a native DynamoDB vector index.

This pattern deploys a serverless semantic-search API using Amazon API Gateway HTTP API, AWS Lambda, Amazon Bedrock, and Amazon DynamoDB. A single Lambda function handles two POST routes so that the sample remains focused on the service integration. The /documents route accepts text and metadata, invokes Amazon Titan Text Embeddings V2 to generate a normalized 1,024-dimensional embedding, and stores the source document, metadata, and vector together in one DynamoDB item. The /search route embeds a natural-language query with the same model and calls DynamoDB SearchVectors to retrieve semantically similar documents. Keeping operational attributes and vectors together removes the need to copy DynamoDB records to a separate vector database or maintain an external synchronization pipeline.
The on-demand DynamoDB table uses tenantId as its partition key and documentId as its sort key, so tenants can safely reuse document identifiers. The native vector index uses cosine distance, tenantId as its vector partition key, and category as an optional inline equality filter. Every search includes tenantId, limiting the vector space and supporting multi-tenancy. The index projects only title and content to control storage and response size. Results also include table keys and inline filter attributes, plus a cosine-distance score where lower values indicate closer semantic matches. The query accepts a configurable topK from 1 through 100 and defaults to five results.
The vector index is declared natively through the VectorIndexes property on the CloudFormation DynamoDB table resource. The pinned CDK version uses a property override to emit this declaration, and all search-schema attributes are included in the table attribute definitions. CloudFormation manages the table and index lifecycle without custom-resource Lambda functions or application-managed polling. The table explicitly uses an AWS-managed KMS key for encryption at rest. Application permissions remain narrowly scoped: Lambda can put items only in the created table, call SearchVectors only on the created vector-index ARN, and invoke only the selected Bedrock embedding model. The HTTP API is deliberately unauthenticated for concise testing; production adaptations should add authorization and restrict CORS. CDK outputs expose the API endpoint, table, vector index, and function names for the supplied command-line and JSON tests. Stack deletion removes the table, its vector index, and the application Lambda log group.

< 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/apigw-http-api-lambda-bedrock-dynamodb-vector-search/typescript/cdk

Deploy

npm installnpx cdk deploy


Testing

Use POST /documents to embed and store the supplied sample documents, then use POST /search to run the sample semantic query. See the README for curl commands and direct Lambda JSON events.

Cleanup

Delete the stack: npx cdk destroy.

Created by:

Vidit Shah

Vidit Shah

Software Engineer working @ServerlessCreed,making Serverless Courses and workshops

Follow on LinkedIn