databases.py
1 """DynamoDB table for WhatsApp message buffering with stream and TTL. 2 3 Uses from_phone as partition key so messages from the same user land in the 4 same shard and are processed together by the tumbling window. 5 6 Based on: https://github.com/aws-samples/sample-whatsapp-end-user-messaging-connect-chat 7 """ 8 9 from constructs import Construct 10 from aws_cdk import ( 11 RemovalPolicy, 12 aws_dynamodb as ddb, 13 ) 14 15 16 class MessageDatabase(Construct): 17 """DynamoDB table with stream for tumbling window message aggregation.""" 18 19 def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None: 20 super().__init__(scope, construct_id, **kwargs) 21 22 self.table = ddb.Table( 23 self, 24 "MessagesTable", 25 partition_key=ddb.Attribute(name="from_phone", type=ddb.AttributeType.STRING), 26 sort_key=ddb.Attribute(name="id", type=ddb.AttributeType.STRING), 27 billing_mode=ddb.BillingMode.PAY_PER_REQUEST, 28 removal_policy=RemovalPolicy.DESTROY, 29 stream=ddb.StreamViewType.NEW_IMAGE, 30 time_to_live_attribute="ttl", 31 )