0
votes

Iam trying to deploy my lambda function to AWS using serverless. When executing

serverless deploy --verbose

Iam getting the following error every time:

Serverless Error ---------------------------------------

An error occurred: mainTable - Invalid KeySchema: The first > KeySchemaElement is not a HASH key type (Service: AmazonDynamoDBv2;Status Code: 400; Error Code: ValidationException; Request ID: EACEH0RDMBR36TR0DDBGODTRT3VV4KQNSO5AEMVJF66Q9ASUAAJG).

My serverless.yml looks as following:

service: backend-1 # NOTE: update this with your service name


provider:
  name: aws
  runtime: nodejs10.x
  stage: dev
  region: eu-central-1


functions:
  graphql:
    handler: src/handler.graphql
    events:
    - http:
        path: graphql
        method: post
        cors: true

plugins:
- serverless-webpack
- serverless-offline

custom:
  webpack:
    webpackCOnfig: 'webpack.config.js'
    includeModules: true
    packager: 'npm'
  stage: ${opt:stage, self:provider.stage}

resources:
  Resources:
    mainTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: main_${self:custom.stage}
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
          - AttributeName: sort
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
          - AttributeName: sort
            KeyType: RANGE
        BillingMode: PAY_PER_REQUEST
        GlobalSecondaryIndexes:
        - IndexName: spinned-primary
          KeySchema:
          - AttributeName: id
            KeyType: RANGE
          - AttributeName: sort
            KeyType: HASH
          Projection:
            ProjectionType: ALL
    labelTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: labels_${self:custom.stage}
        AttributeDefinitions:
          - AttributeName: sort
            AttributeType: S
          - AttributeName: label
            AttributeType: S
        KeySchema:
          - AttributeName: sort
            KeyType: HASH
          - AttributeName: label
            KeyType: RANGE
        BillingMode: PAY_PER_REQUEST
        GlobalSecondaryIndexes:
        - IndexName: spinned-primary
          KeySchema:
          - AttributeName: sort
            KeyType: RANGE
          - AttributeName: label
            KeyType: HASH
          Projection:
              ProjectionType: ALL
    logTable:
      Type: AWS::DynamoDB::Table
      Properties:
        TableName: logs_${self:custom.stage}
        AttributeDefinitions:
          - AttributeName: id
            AttributeType: S
          - AttributeName: sort
            AttributeType: S
        KeySchema:
          - AttributeName: id
            KeyType: HASH
          - AttributeName: sort
            KeyType: RANGE
        BillingMode: PAY_PER_REQUEST

Can someone of you help?

Cheers!

1

1 Answers

3
votes

When using KeySchemaElements, the HASH keytype must come before the RANGE keytype.

In your YAML, on you GSI for spinned-primary, you have to put the HASH keytype before the RANGE keytype; switch them around so that the HASH is the first keytype in that element.