Carova

Serverless AWS Create DynamoDB Table

Create a DynamoDB Table with your Serverless YAML file. DynamoDB Table with a Time to Live (TTL) attribute and a Global Secondary Index (GSI) to query efficiently by dates.

The following configuration setup is for a HASH - RANGE primary index with a Global Secondary Index called 'date-index' that allows data to be queried by a date with a format of 'YYYY-mm-DD'.

Add the following to the resources section of your Serverless YAML file:

1resources: 2 Resources: 3 DynamoDBTableName: 4 Type: AWS::DynamoDB::Table 5 Properties: 6 TableName: ${self:service}-${opt:stage, self:provider.stage}-table-name 7 BillingMode: PAY_PER_REQUEST 8 AttributeDefinitions: 9 - AttributeName: primaryID 10 AttributeType: S 11 - AttributeName: id 12 AttributeType: S 13 - AttributeName: date 14 AttributeType: S 15 - AttributeName: expiration 16 AttributeType: N 17 KeySchema: 18 - AttributeName: primaryID 19 KeyType: HASH 20 - AttributeName: id 21 KeyType: RANGE 22 TimeToLiveSpecification: 23 AttributeName: expiration 24 Enabled: true 25 StreamSpecification: 26 StreamViewType: NEW_AND_OLD_IMAGES 27 GlobalSecondaryIndexes: 28 - IndexName: date-index 29 KeySchema: 30 - AttributeName: date 31 KeyType: HASH 32 - AttributeName: expiration 33 KeyType: RANGE 34 Projection: 35 ProjectionType: INCLUDE 36 NonKeyAttributes: 37 - primaryID 38 - id 39 - expiration 40 - processing