无服务器离线和分布式DynamoDB 抛出 Local UnknownEndpoint: Inaccessible host: localhost at port 8000 服务可能在 localhost 区域不可用

发布于 2025-01-19 23:52:14 字数 5518 浏览 5 评论 0原文

我正在与无服务器一起玩,我没有运气获得>无服务器dynamodb-local一起使用。 我的设置非常最小,复制需要3分钟的时间,我这样做了:

  1. 生成一个TS无服务的项目,例如 - sls create -t​​ aws-nodejs-typescript -path folder-name
  2. install distrancies <代码> npm i
  3. 添加dynamodb local npm install-save lesserless dynamodb-local
  4. 添加无服务器脱机npm install seltir naster seltir serverless offless-offline -save-dev
  5. instain sls dynamodb install


现在我更新server> serverless.ts文件,例如

  1. 订单中添加插件
  plugins: [
    'serverless-esbuild',
    'serverless-dynamodb-local',
    'serverless-offline'
  ],
  1. code> custom> custom添加dynamodb config的
  custom: {
    esbuild: { ... },
    dynamodb: {
      stages: ['dev'],
      start: {
        migrate: true
      }
    }
  1. 正确 是在资源
  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
          }
        }
      }
    }
  }

中添加dynamoDB,现在我尝试通过运行sls offline start -s dev ,然后在下面抛出错误(执行不会停止)
(此错误如果我删除迁移: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)

即使删除错误,我仍然能够成功执行

> aws dynamodb list-tables --endpoint-url http://localhost:8000 --region localhost
{
    "TableNames": []
}



除了上述所述,我没有更改任何其他代码。我想这将是真正的基础,例如我缺少某些配置或某个地方的东西,但是在文档或其他帖子中找不到任何有帮助的内容。 欢迎任何想法。

...而且由于我知道过去有些版本在这里遇到麻烦

  "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"
  },

。 /代码>如果有帮助

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

I'm playin around with Serverless and I have no luck getting serverless-offline to work with serverless-dynamodb-local.
I have very minimal setup, it takes 3minutes to reproduce, I did it like this:

  1. Generate a TS Serverless project like - sls create -t aws-nodejs-typescript --path folder-name
  2. install dependencies npm i
  3. Add DynamoDB local npm install --save serverless-dynamodb-local
  4. Add serverless offline npm install serverless-offline --save-dev
  5. Install dynamodb sls dynamodb install

Now I update serverless.ts file like

  1. Include installed plugins in the correct order
  plugins: [
    'serverless-esbuild',
    'serverless-dynamodb-local',
    'serverless-offline'
  ],
  1. In custom add DynamoDB config like
  custom: {
    esbuild: { ... },
    dynamodb: {
      stages: ['dev'],
      start: {
        migrate: true
      }
    }
  1. Last step is to add DynamoDB in resources
  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
          }
        }
      }
    }
  }

Now I try to spin it all up by running sls offline start -s dev and it throws error below (execution won't stop)
(this error is logically not thrown if I remove migrate: true from serverless.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)

Even though error is thrown, I'm still able to successfully execute

> aws dynamodb list-tables --endpoint-url http://localhost:8000 --region localhost
{
    "TableNames": []
}

I didn't change any other code than described above. I'd guess it will be something really basic, like I'm missing some config or something somewhere, but I can't find anything in docs or other posts that would help.
Any ideas welcome.

...and since I know some version had troubles in the past here is package.json I'm running with

  "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"
  },

here's full serverless.ts if it helps

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羞稚 2025-01-26 23:52:14

因此,事实证明这是我的节点版本的问题,我正在运行v17.3.1。切换到v16.4.0之后,它的工作就像魅力一样。

So turned out it was a problem with my node version, I was running v17.3.1. After switching to v16.4.0 it works like a charm. ????

From @EdwinWong comment:
In node 18 in your serverless custom.dynamodb.start.host declare 127.0.0.1 and it should work
(I didn't test this)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文