Serverless Offline & DynamoDB 抛出 Local UnknownEndpoint: Inaccessible host: localhost at port 8000 Service may not available in localhost region
Serverless Offline & DynamoDB throws Local UnknownEndpoint: Inaccessible host: localhost at port 8000 Service may not be available in localhost region
我正在玩无服务器,但我没有运气让 serverless-offline
与 serverless-dynamodb-local
一起工作。
我的设置非常简单,重现需要 3 分钟,我是这样做的:
- 生成一个 TS Serverless 项目,如 -
sls create -t aws-nodejs-typescript --path folder-name
- 安装依赖项
npm i
- 添加 DynamoDB 本地
npm install --save serverless-dynamodb-local
- 添加无服务器离线
npm install serverless-offline --save-dev
- 安装dynamodb
sls dynamodb install
现在我更新 serverless.ts
文件如
- 以正确的顺序包含已安装的插件
plugins: [
'serverless-esbuild',
'serverless-dynamodb-local',
'serverless-offline'
],
- 在
custom
中添加DynamoDB配置如
custom: {
esbuild: { ... },
dynamodb: {
stages: ['dev'],
start: {
migrate: true
}
}
- 最后一步是在资源中添加 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
}
}
}
}
}
现在我尝试通过 运行 sls offline start -s dev
将其全部旋转起来并在下面抛出错误(执行不会停止)
(此错误是如果我从 serverless.ts
)
中删除 migrate: true
逻辑上不会抛出
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": []
}
除上述代码外,我没有更改任何其他代码。我猜这将是一些非常基本的东西,比如我缺少一些配置或某处的东西,但我在文档或其他帖子中找不到任何有用的东西。
欢迎任何想法。
...因为我知道过去有些版本有问题这里是 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"
},
这里是完整的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
}
}
}
}
}
};
原来这很可能是我的 MacBook 的环境问题。我的朋友在他的计算机上尝试了完全相同的代码,并且对他有效。
如果有人知道为什么会发生这种情况,请告诉我。
编辑:
结果是我的节点版本有问题,我是 运行 v17.3.1
。切换到 v16.4.0
后,它就像一个魅力。
我正在玩无服务器,但我没有运气让 serverless-offline
与 serverless-dynamodb-local
一起工作。
我的设置非常简单,重现需要 3 分钟,我是这样做的:
- 生成一个 TS Serverless 项目,如 -
sls create -t aws-nodejs-typescript --path folder-name
- 安装依赖项
npm i
- 添加 DynamoDB 本地
npm install --save serverless-dynamodb-local
- 添加无服务器离线
npm install serverless-offline --save-dev
- 安装dynamodb
sls dynamodb install
现在我更新 serverless.ts
文件如
- 以正确的顺序包含已安装的插件
plugins: [
'serverless-esbuild',
'serverless-dynamodb-local',
'serverless-offline'
],
- 在
custom
中添加DynamoDB配置如
custom: {
esbuild: { ... },
dynamodb: {
stages: ['dev'],
start: {
migrate: true
}
}
- 最后一步是在资源中添加 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
}
}
}
}
}
现在我尝试通过 运行 sls offline start -s dev
将其全部旋转起来并在下面抛出错误(执行不会停止)
(此错误是如果我从 serverless.ts
)
migrate: true
逻辑上不会抛出
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": []
}
除上述代码外,我没有更改任何其他代码。我猜这将是一些非常基本的东西,比如我缺少一些配置或某处的东西,但我在文档或其他帖子中找不到任何有用的东西。
欢迎任何想法。
...因为我知道过去有些版本有问题这里是 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"
},
这里是完整的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
}
}
}
}
}
};
原来这很可能是我的 MacBook 的环境问题。我的朋友在他的计算机上尝试了完全相同的代码,并且对他有效。
如果有人知道为什么会发生这种情况,请告诉我。
编辑:
结果是我的节点版本有问题,我是 运行 v17.3.1
。切换到 v16.4.0
后,它就像一个魅力。