CoorpAcademy / serverless-plugins

Collection of serverless plugins :zap:
229 stars 131 forks source link

serverless-offline-dynamodb-streams not working! - ResourceNotFoundException: Invalid ShardId in ShardIterator #164

Open mdrijwan opened 3 years ago

mdrijwan commented 3 years ago

not working with my python project!!

this is my yml file settings:

serverless-offline-dynamodb-streams:
    apiVersion: '2013-12-02'
    endpoint: http://localhost:8000
    region: ddblocal
    accessKeyId: root
    secretAccessKey: root
    skipCacheInvalidation: false
    readInterval: 500

functions:
  offer-changelog:
    handler: functions/offer-changelog/handler.handler
    iamRoleStatements:
      - Effect: "Allow"        
        Action:
          - dynamodb:DescribeStream
          - dynamodb:GetRecords
          - dynamodb:GetShardIterator
          - dynamodb:ListStreams
    events:
      - stream:
          type: dynamodb
          enabled: true
          arn: arn:aws:dynamodb:ddblocal:000000000000:table/dev_offer/stream/2021-01-13T02:32:02.845
          batchSize: 10
          startingPosition: TRIM_HORIZON

this is error i get:



  ResourceNotFoundException: Invalid ShardId in ShardIterator
      at Request.extractError (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/protocol/json.js:52:27)
      at Request.callListeners (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
      at Request.emit (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
      at Request.emit (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/request.js:688:14)
      at Request.transition (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/request.js:22:10)
      at AcceptorStateMachine.runTo (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/state_machine.js:14:12)
      at /Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/state_machine.js:26:10
      at Request.<anonymous> (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/request.js:38:9)
      at Request.<anonymous> (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/request.js:690:12)
      at Request.callListeners (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/sequential_executor.js:116:18)
      at Request.emit (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
      at Request.emit (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/request.js:688:14)
      at Request.transition (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/request.js:22:10)
      at AcceptorStateMachine.runTo (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/state_machine.js:14:12)
      at /Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/state_machine.js:26:10
      at Request.<anonymous> (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/request.js:38:9)
      at Request.<anonymous> (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/request.js:690:12)
      at Request.callListeners (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/sequential_executor.js:116:18)
      at callNextListener (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/sequential_executor.js:96:12)
      at IncomingMessage.onEnd (/Users/rijwan/code/g2g/serverless-offer/node_modules/aws-sdk/lib/event_listeners.js:313:13)
      at IncomingMessage.emit (events.js:327:22)
      at IncomingMessage.EventEmitter.emit (domain.js:483:12)
      at endReadableNT (_stream_readable.js:1220:12)
      at processTicksAndRejections (internal/process/task_queues.js:84:21)

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              12.18.4
     Framework Version:         2.4.0 (local)
     Plugin Version:            4.1.0
     SDK Version:               2.3.2
     Components Version:        3.2.1```
any help please?
rynvelt commented 3 years ago

Same problem here. It occurs from time to time. Using serverless-offline-dynamodb-streams 4.2.0. This is an excerpt from the serverless.yaml:

 serverless-offline-dynamodb-streams:
    apiVersion: '2013-12-02'
    endpoint: http://0.0.0.0:8000
    region: eu-central-1
    accessKeyId: root
    secretAccessKey: root
    skipCacheInvalidation: false
    readInterval: 500

functions:
  stream-lambda:
    timeout: 10
    handler: src/index.streamHandler
    events:
      - stream:
          enabled: true
          type: dynamodb
          arn: ${self:custom.tableArn}/stream/test-stream/1970-01-01T00:00:00.000
          batchSize: 10
          startingPosition: LATEST

I run the DynamoDB in a container with Docker (amazon/dynamodb-local:1.15.0) with the following command: ["-jar", "DynamoDBLocal.jar", "-sharedDb", "-dbPath", "/home/dynamodblocal/storage"] on port 8000. Recreating the database helps, but I assume that this is not expected behavior?

Having a look at https://github.com/CoorpAcademy/serverless-plugins/blob/master/packages/serverless-offline-dynamodb-streams/src/dynamodb-streams.js and at first glance the plugin is not doing much related to shards, but it dispatches to https://github.com/CoorpAcademy/serverless-plugins/blob/master/packages/dynamodb-streams-readable/src/index.js.

To me this looks suspicous:

const shardId = options.shardId
        ? data.StreamDescription.Shards.filter(function (shard) {
            return shard.ShardId === options.shardId;
          }).map(function (shard) {
            return shard.ShardId;
          })[0]
        : data.StreamDescription.Shards[0].ShardId;

Like if the shard ID was not passed, the first best shard is returned. And furthermore the AWS DynamoDB Local documentation (https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.UsageNotes.html) says:

If you're using DynamoDB Streams, the rate at which shards are created might differ. In the DynamoDB web service, shard-creation behavior is partially influenced by table partition activity. When you run DynamoDB locally, there is no table partitioning. In either case, shards are ephemeral, so your application should not be dependent on shard behavior.

Is this maybe an out-of-sync problem with dynamodb-streams-readable not handling this property gracefully?

Searching for the error message "Invalid ShardId in ShardIterator" doesn't lead to anything helpful. Any other ideas?

mdrijwan commented 3 years ago

is there any update on the fix?