Create a serverless API with AWS Lambda and DynamoDB using single table design. I need to model Users, Orders, and Products in one table with proper partition and sort keys so I can fetch all orders for a user and all items in an order with single queries.
Assistant
Designing single-table with PK/SK for users, orders, products
The design uses a DynamoDB table with a composite primary key (PK, SK) to model Users, Orders (and their Items), and Products in one table. Users and their orders share partition keys like USER#{userId}, with orders sorted by date for quick retrieval (SK=ORDER#{orderDate}#{orderId}). Order items are stored separately under PK=ORDER#{orderId}, SK=ITEM#{lineNumber} so fetching all items for an order is a simple query by order partition. The order header is duplicated under both partitions to enable fetching all orders for a user and all details of an order in single queries. Products use their own partition key PRODUCT#{productId} with a GSI to list all products. Sample AWS SAM YAML creates the table with composite keys and a GSI, while example Node.js lambda code demonstrates querying orders for a user and items of an order.
Below is a simple single‑table design and a minimal “serverless” skeleton (API Gateway + Lambda + DynamoDB) you can adapt.
Access patterns (what you said you need)
Get a user by id.
Get all orders for a user (one query).
Get all items in an order (one query).
Get a product by id (and optionally list all products).
Table shape (single table)
Use a single table with generic keys PK (partition key) and SK (sort key). You can add a GSI to list products, if needed.
Item types and key conventions:
Entity
PK
SK
Notes
User profile
USER#{userId}
PROFILE
name, email, etc.
Order header
USER#{userId}
ORDER#{orderDate}#{orderId}
Lets you query all orders for a user, sorted by date. Store orderId and userId.
Order header
ORDER#{orderId}
HEADER
Duplicate header in the order partition so you can fetch header + items together.
Order item
ORDER#{orderId}
ITEM#{lineNumber}
productId, qty, price, etc. Query by ORDER#... to get all items.
Product
PRODUCT#{productId}
PROFILE
name, price, etc.
(Optional) list of products
PRODUCTS
# or name
If you want to query all products without a scan, put them under a constant PK or use a GSI.
Optional GSI for listing products:
GSI1PK = 'PRODUCT'
GSI1SK = productName (or productId)
Queries
All orders for a user: Query PK = USER#{userId} AND SK BEGINS_WITH 'ORDER#'
All items (and header) for an order: Query PK = ORDER#{orderId} (you get the HEADER item plus all ITEM# rows).
Get product by id: GetItem PK = PRODUCT#{productId}, SK = PROFILE
List products (if using GSI): Query GSI1PK = 'PRODUCT'
Example AWS SAM (YAML)
yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Single-table serverless API
Globals:
Function:
Runtime: nodejs18.x
MemorySize: 128
Timeout: 10
Environment:
Variables:
TABLE_NAME: ShopTable
Resources:
ShopTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: ShopTable
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: PK
AttributeType: S
- AttributeName: SK
AttributeType: S
- AttributeName: GSI1PK
AttributeType: S
- AttributeName: GSI1SK
AttributeType: S
KeySchema:
- AttributeName: PK
KeyType: HASH
- AttributeName: SK
KeyType: RANGE
GlobalSecondaryIndexes:
- IndexName: GSI1
KeySchema:
- AttributeName: GSI1PK
KeyType: HASH
- AttributeName: GSI1SK
KeyType: RANGE
Projection:
ProjectionType: ALL
GetUserOrdersFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/
Handler: handlers.getUserOrders
Policies:
- DynamoDBReadPolicy:
TableName: !Ref ShopTable
Events:
ApiEvent:
Type: Api
Properties:
Path: /users/{userId}/orders
Method: get
GetOrderItemsFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/
Handler: handlers.getOrderItems
Policies:
- DynamoDBReadPolicy:
TableName: !Ref ShopTable
Events:
ApiEvent:
Type: Api
Properties:
Path: /orders/{orderId}/items
Method: get