node.jsserverless-frameworkamazon-dynamodb-localserverless-offline

Serverless offline + DynamoDb local issue


I've been playing with serverless offline and dynamodb local lately. When I've used websockets - things worked well. Then, I've decided to change protocol to http. Don't know how this can be related, but it's the only change I've did in the code.

Now, I receive error from dynamodb (including output of db command):

Serverless: [AWS dynamodb 400 0.034s 0 retries] query({
  TableName: 'DEVICE_TABLE_DEV',
  KeyConditionExpression: '#id = :id',
  ExpressionAttributeNames: { '#id': 'deviceId' },
  ExpressionAttributeValues: { ':id': { S: 'a1173b07-af44-450b-b709-902c0b011df2' } }
})
ResourceNotFoundException: Cannot do operations on a non-existent table

I've checked existing tables with command:

aws dynamodb list-tables --endpoint-url http://localhost:8042

And I see, that table exists:

{
    "TableNames": [
        "DEVICE_TABLE_DEV",
        "USER_TABLE_DEV"
    ]
}

Then I've printed dynamodb client, and I see, that options provided seems to be correct according to documentation:

DocumentClient {
  options: {
    region: 'localhost',
    endpoint: 'http://localhost:8042',
    attrValue: 'S6'
  },
  service: Service {
    config: Config {
      credentials: [SharedIniFileCredentials],
      credentialProvider: [CredentialProviderChain],
      region: 'localhost',
      logger: [CLI],
      apiVersions: {},
      apiVersion: null,
      endpoint: 'http://localhost:8042',
      httpOptions: [Object],
      maxRetries: undefined,
      maxRedirects: 10,
      paramValidation: true,
      sslEnabled: true,
      s3ForcePathStyle: false,
      s3BucketEndpoint: false,
      s3DisableBodySigning: true,
      s3UsEast1RegionalEndpoint: 'legacy',
      s3UseArnRegion: undefined,
      computeChecksums: true,
      convertResponseTypes: true,
      correctClockSkew: false,
      customUserAgent: null,
      dynamoDbCrc32: true,
      systemClockOffset: 0,
      signatureVersion: null,
      signatureCache: true,
      retryDelayOptions: {},
      useAccelerateEndpoint: false,
      clientSideMonitoring: false,
      endpointDiscoveryEnabled: undefined,
      endpointCacheSize: 1000,
      hostPrefixEnabled: true,
      stsRegionalEndpoints: 'legacy'
    },
    endpoint: Endpoint {
      protocol: 'http:',
      host: 'localhost:8042',
      port: 8042,
      hostname: 'localhost',
      pathname: '/',
      path: '/',
      href: 'http://localhost:8042/'
    },
    _events: { apiCallAttempt: [Array], apiCall: [Array] },
    MONITOR_EVENTS_BUBBLE: [Function: EVENTS_BUBBLE],
    CALL_EVENTS_BUBBLE: [Function: CALL_EVENTS_BUBBLE],
    _clientId: 1
  },
  attrValue: 'S6'
}

This is how I create a client:

const dynamo = new AWS.DynamoDB.DocumentClient({ region: 'localhost', endpoint: 'http://localhost:8042'});

UPDATE: adding my serverless.yml dynamodb section configuration:

dynamodb:
  # If you only want to use DynamoDB Local in some stages, declare them here
    stages:
      - local
    start:
      port: 8042
      inMemory: true
      heapInitial: 200m
      heapMax: 1g
      migrate: true
      seed: true
      convertEmptyValues: true
    # Uncomment only if you already have a DynamoDB running locally
      noStart: true

Would appreciate any advice in this direction.


Solution

  • You shouldn't init DynamoDB with region: 'localhost'.

    Either leave it out or change it to a proper region, e.g.:

    const dynamo = new AWS.DynamoDB.DocumentClient({ region: 'eu-west-2', endpoint: 'http://localhost:8042'});