Multiple attributes as Hash Key in Dynamo DB

Viewed 2058

I am trying to create a Dynamo DB table using CloudFormation. I want two attributes combined as the hash key. For example,

   ExampleTable:
    Type: "AWS::DynamoDB::Table"
    Description: This is an example table
    Properties:
      TableName: { 'Fn::Sub': 'example'}
      StreamSpecification:
        StreamViewType: NEW_AND_OLD_IMAGES
      AttributeDefinitions:
        - AttributeName: "a1"
          AttributeType: "S"
        - AttributeName: "a2"
          AttributeType: "N"
        - AttributeName: "a3"
          AttributeType: "S"
        - AttributeName: "a4"
          AttributeName: "S"
      KeySchema:
        - AttributeName: "a1"
          KeyType: "HASH"
        - AttributeName: "a2"
          KeyType: "HASH"
        - AttributeName: "a3"
          KeyType: "Range"
        - AttributeName: "a4"
          KeyType: "Range"

In this case, I want the combination of both "a1" and "a2" to be hash key and similarly, I want the combination of "a3" and "a4" to be my range key.

  • Hash key: a1, a2
  • Range key: a3, a4

How do I achieve this? I am looking for a solution that doesn't use GSI or LSI.

2 Answers

You can only have one field as each of the two keys, so one HASH and one RANGE key. If you need multiple fields you'll need to create a composite key. For example, create an attribute named a1a2 for the HASH and a3a4 for the range.

I can't see the full images of your plan here. If you can explain more the plan, it would be better.

As Jason explained, we will have max 2 key for dynamodb table, LeadingKey (Hash Key) and or SortKey (Range Key).

Related