首页 > 解决方案 > 无服务器离线 + DynamoDb 本地问题

问题描述

我最近一直在玩无服务器离线和本地 dynamodb。当我使用 websockets 时,一切都很好。然后,我决定将协议更改为 http。不知道这有什么关系,但这是我在代码中所做的唯一更改。

现在,我收到来自 dynamodb 的错误(包括 db 命令的输出):

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

我用命令检查了现有表:

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

我看到,该表存在:

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

然后我打印了 dynamodb 客户端,我看到,根据文档,提供的选项似乎是正确的:

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'
}

这就是我创建客户端的方式:

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

更新:添加我的 serverless.yml dynamodb 部分配置:

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

将不胜感激这方面的任何建议。

标签: node.jsserverless-frameworkamazon-dynamodb-localserverless-offline

解决方案


您不应该使用region: 'localhost'.

要么将其排除在外,要么将其更改为适当的区域,例如:

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

推荐阅读