Serverless: dynamodb delete where condition

1.3k Views Asked by At

I am trying, and failing, to delete a record with a condition. I keep getting this error: The provided key element does not match the schema.

This is my definition in the yml:

resources:
  Resources:
    vuelosTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Delete
      Properties:
        AttributeDefinitions:
          -
            AttributeName: id
            AttributeType: S
          -
            AttributeName: vuelta
            AttributeType: S
        KeySchema:
          -
            AttributeName: id
            KeyType: HASH
          -
            AttributeName: vuelta
            KeyType: RANGE
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
        TableName: ${self:provider.environment.DYNAMODB_TABLE}

These are the params when trying to delete:

params = {
    RequestItems: {
      [process.env.DYNAMODB_TABLE]: [{
        DeleteRequest: {
          Key: {
            "vuelta": "2017-09-09"
          }
        }
      }]
    }
  };

I know it's something I am not getting, but I don't know what. Any ideas?

1

There are 1 best solutions below

4
On

Your table key is both id and vuelta but you're only providing vuelta in the delete request. Modify the key in your delete request so it contains both the id and vuelta.

Also, depending on your client library may need to specify

Key: {
  id: { S: "some value" },
  vuelta: { S: "some value" }
}