无服务器离线和 DynamoDB 抛出 Local UnknownEndpoint: Inaccessible host: localhost at port 8000 Service may not be available in localhostregion

Pau*_*uli 2 typescript serverless-framework serverless serverless-offline amazon-dynamodb-local

我正在尝试无服务器,但我没有运气能够serverless-offline使用serverless-dynamodb-local. 我的设置非常少,需要 3 分钟才能重现,我是这样做的:

  1. 生成一个 TS Serverless 项目,例如 -sls create -t aws-nodejs-typescript --path folder-name
  2. 安装依赖项npm i
  3. 添加 DynamoDB 本地npm install --save serverless-dynamodb-local
  4. 添加无服务器离线npm install serverless-offline --save-dev
  5. 安装dynamodbsls dynamodb install


现在我更新serverless.ts文件

  1. 以正确的顺序包含已安装的插件
  plugins: [
    'serverless-esbuild',
    'serverless-dynamodb-local',
    'serverless-offline'
  ],
Run Code Online (Sandbox Code Playgroud)
  1. 添加customDynamoDB 配置,例如
  custom: {
    esbuild: { ... },
    dynamodb: {
      stages: ['dev'],
      start: {
        migrate: true
      }
    }
Run Code Online (Sandbox Code Playgroud)
  1. 最后一步是在资源中添加 DynamoDB
  resources: {
    Resources: {
      usersTable: {
        Type: 'AWS::DynamoDB::Table',
        Properties: {
          TableName: 'firstTable',
          AttributeDefinitions: [{
            AttributeName: 'id',
            AttributeType: 'S',
          }],
          KeySchema: [{
            AttributeName: 'id',
            KeyType: 'HASH'
          }],
          ProvisionedThroughput: {
            ReadCapacityUnits: 1,
            WriteCapacityUnits: 1
          }
        }
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

现在我尝试通过运行来旋转它sls offline start -s dev,它会在下面抛出错误(执行不会停止) (如果我从 中
删除,则逻辑上不会抛出此错误)migrate: trueserverless.ts

Dynamodb Local Started, Visit: http://localhost:8000/shell
UnknownEndpoint: Inaccessible host: `localhost' at port `8000'. This service may not be available in the `localhost' region.: DynamoDB - Error - 
Environment: darwin, node 17.3.1, framework 3.12.0 (local), plugin 6.2.1, SDK 4.3.2
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues

Error:
UnknownEndpoint: Inaccessible host: `localhost' at port `8000'. This service may not be available in the `localhost' region.
at Request.ENOTFOUND_ERROR (...node_modules/aws-sdk/lib/event_listeners.js:529:46)
at Request.callListeners (...node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at Request.emit (...node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (...node_modules/aws-sdk/lib/request.js:686:14)
at error (...node_modules/aws-sdk/lib/event_listeners.js:361:22)
at ClientRequest.<anonymous> (...node_modules/aws-sdk/lib/http/node.js:99:9)
at ClientRequest.emit (node:events:390:28)
at ClientRequest.emit (node:domain:475:12)
at Socket.socketErrorListener (node:_http_client:442:9)
at Socket.emit (node:events:390:28)
at Socket.emit (node:domain:475:12)
at emitErrorNT (node:internal/streams/destroy:164:8)
at emitErrorCloseNT (node:internal/streams/destroy:129:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21)
Run Code Online (Sandbox Code Playgroud)

即使抛出错误,我仍然能够成功执行

> aws dynamodb list-tables --endpoint-url http://localhost:8000 --region localhost
{
    "TableNames": []
}
Run Code Online (Sandbox Code Playgroud)



除了上面描述的之外,我没有更改任何其他代码。我猜这将是非常基本的东西,就像我在某个地方缺少一些配置或某些东西,但我在文档或其他帖子中找不到任何有帮助的内容。欢迎任何想法。

...因为我知道某些版本过去遇到过麻烦,所以package.json我正在运行

  "engines": {
    "node": ">=14.15.0"
  },
  "dependencies": {
    "@middy/core": "^2.5.3",
    "@middy/http-json-body-parser": "^2.5.3",
    "serverless-dynamodb-local": "^0.2.40"
  },
  "devDependencies": {
    "@serverless/typescript": "^3.0.0",
    "@types/aws-lambda": "^8.10.71",
    "@types/node": "^14.14.25",
    "esbuild": "^0.14.11",
    "json-schema-to-ts": "^1.5.0",
    "serverless": "^3.0.0",
    "serverless-esbuild": "^1.23.3",
    "serverless-offline": "^8.5.0",
    "ts-node": "^10.4.0",
    "tsconfig-paths": "^3.9.0",
    "typescript": "^4.1.3"
  },
Run Code Online (Sandbox Code Playgroud)

serverless.ts如果有帮助的话这里已经满了

const serverlessConfiguration: AWS = {
  service: 'aws-onboarding-api',
  frameworkVersion: '3',
  plugins: [
    'serverless-esbuild',
    'serverless-dynamodb-local',
    'serverless-offline'
  ],
  provider: {
    name: 'aws',
    runtime: 'nodejs14.x',
    apiGateway: {
      minimumCompressionSize: 1024,
      shouldStartNameWithService: true,
    },
    environment: {
      AWS_NODEJS_CONNECTION_REUSE_ENABLED: '1',
      NODE_OPTIONS: '--enable-source-maps --stack-trace-limit=1000',
    },
  },
  // import the function via paths
  functions: { hello },
  package: { individually: true },
  custom: {
      esbuild: {
      bundle: true,
      minify: false,
      sourcemap: true,
      exclude: ['aws-sdk'],
      target: 'node14',
      define: { 'require.resolve': undefined },
      platform: 'node',
      concurrency: 10,
    },
    dynamodb: {
      stages: ['dev'],
      start: {
        migrate: true
      }
    }
  },
  resources: {
    Resources: {
      usersTable: {
        Type: 'AWS::DynamoDB::Table',
        Properties: {
          TableName: 'firstTable',
          AttributeDefinitions: [{
            AttributeName: 'id',
            AttributeType: 'S',
          }],
          KeySchema: [{
            AttributeName: 'id',
            KeyType: 'HASH'
          }],
          ProvisionedThroughput: {
            ReadCapacityUnits: 1,
            WriteCapacityUnits: 1
          }
        }
      }
    }
  }
};
Run Code Online (Sandbox Code Playgroud)

Pau*_*uli 9

结果发现这是我的节点版本的问题,我正在运行v17.3.1。切换到v16.4.0它之后,它就像一个魅力。

来自@EdwinWong评论:
在无服务器custom.dynamodb.start.host声明的节点 18 中127.0.0.1,它应该可以工作(我没有测试这个)

  • 这次真是万分感谢!我运行的是 Node 18,遇到了和你一样的问题。降级到 16 就成功了。 (3认同)
  • 我认为降级不应该是一个有效的解决方案,而只是一种解决方法。解决方案应该识别问题并提供在新版本中解决该问题的方法。就我而言,我使用的是 Node 18 和 fetch API,因此降级到 v16 将是开发过程中的一个倒退。 (2认同)