Search code examples
amazon-web-servicesamazon-dynamodbserverless

DynamoDB Encountered unsupported property ProjectionType - Serverless


I'm struggling to understand how to create a SecondaryGlobalIndex in DynamoDB using serverless. Here is what I got so far:

resources:
  Resources:
    ConnectionsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        AttributeDefinitions:
          - AttributeName: connectionId
            AttributeType: S
          - AttributeName: sessionId
            AttributeType: S
          - AttributeName: clientType
            AttributeType: S
        BillingMode: PAY_PER_REQUEST
        KeySchema:
          - AttributeName: connectionId
            KeyType: HASH
          - AttributeName: sessionId
            KeyType: HASH
          - AttributeName: clientType
            KeyType: HASH
        GlobalSecondaryIndexes:
          - IndexName: ConnectionIdIndex
            KeySchema:
              - AttributeName: connectionId
                KeyType: HASH
            Projection:
              NonKeyAttributes:
                - clientType
                - sessionId
            ProjectionType: INCLUDE
        SSESpecification:
          SSEEnabled: true
        TimeToLiveSpecification:
          AttributeName: ttl
          Enabled: true
    SessionHistoryTable:
      Type: AWS::DynamoDB::Table
      Properties:
        AttributeDefinitions:
          - AttributeName: sessionId
            AttributeType: S
        BillingMode: PAY_PER_REQUEST
        KeySchema:
          - AttributeName: sessionId
            KeyType: HASH
        SSESpecification:
          SSEEnabled: true
        TimeToLiveSpecification:
          AttributeName: ttl
          Enabled: true
    SessionsTable:
      Type: AWS::DynamoDB::Table
      Properties:
        AttributeDefinitions:
          - AttributeName: sessionId
            AttributeType: S
        BillingMode: PAY_PER_REQUEST
        KeySchema:
          - AttributeName: sessionId
            KeyType: HASH
        SSESpecification:
          SSEEnabled: true
        TimeToLiveSpecification:
          AttributeName: ttl
          Enabled: true

But when I try to deploy I get this error:

  An error occurred: ConnectionsTable - Encountered unsupported property ProjectionType.

The projection type as documented here can be either ALL | KEYS_ONLY | INCLUDE

My question is, is there something else I'm doing wrong in the dynamoDB yaml?


Solution

  • ProjectionType and NonKeyAttributes are properties of Projection, However ProjectionType is aligned incorrectly along side GlobalSecondaryIndexes

      GlobalSecondaryIndexes:
        - IndexName: ConnectionIdIndex
          KeySchema:
            - AttributeName: connectionId
              KeyType: HASH
          Projection:
            NonKeyAttributes:
              - clientType
              - sessionId
            ProjectionType: INCLUDE.  <-- aligned incorrect
    

    Second concern is KeySchema should contain only HASH and RANGE keys, can't have multiple HASH keys.

    Overall assuming that connectionId is HASH and sessionId is RANGE key.

      ConnectionsTable:
        Type: AWS::DynamoDB::Table
        Properties:
          AttributeDefinitions:
            - AttributeName: connectionId
              AttributeType: S
            - AttributeName: sessionId
              AttributeType: S
          BillingMode: PAY_PER_REQUEST
          KeySchema:
            - AttributeName: connectionId
              KeyType: HASH
            - AttributeName: sessionId
              KeyType: RANGE
          GlobalSecondaryIndexes:
            - IndexName: ConnectionIdIndex
              KeySchema:
                - AttributeName: connectionId
                  KeyType: HASH
              Projection:
                NonKeyAttributes:
                  - clientType
                  - sessionId
                ProjectionType: INCLUDE
          SSESpecification:
            SSEEnabled: true
          TimeToLiveSpecification:
            AttributeName: ttl
            Enabled: true