离线 DynamoDB + Serverless + Lambda ResourceNotFoundException
Offline DynamoDB + Serverless + Lambda ResourceNotFoundException
我正在尝试使用无服务器和 DynamoDB 组合一个非常简单的 AWS Lambda。
我创建项目的代码是:
dynamoDb: DocumentClient
constructor() {
this.dynamoDb = new DynamoDB.DocumentClient({region: 'us-east-1'})
}
saveFiles(files: File[]): Promise<boolean> {
return new Promise<boolean>((resolve, reject ) => {
files.forEach(file => {
const tableName = process.env.DYNAMODB_TABLE
this.dynamoDb.put({
TableName: tableName,
Item: {
downloaded: {N : `${file.downloaded ? 1 : 0}`},
location: {S: `${file.location}`}
}
}, (error, result) => {
if (!!error) {
reject(error)
return
}
console.debug(`DB Save result: ${JSON.stringify(result)}`)
resolve(true)
})
})
})
}
错误是:
handler.ts:41
code:"ResourceNotFoundException"
message:"Requested resource not found"
name:"ResourceNotFoundException"
requestId:"2ST4DCE3NJ85UE32OAD6PUMTBJVV4KQNSO5AEMVJF66Q9ASUAAJG"
retryable:false
retryDelay:23.64284067311807
stack:"ResourceNotFoundException: Requested resource not found\n at Request.extractError (/Users/michael/Development/javascript/data-export-lambda/.webpack/service/webpack:/node_modules/aws-sdk/lib/protocol/json.js:51:1)\n at Request.callListeners (/Users/michael/Development/javascript/data-export-lambda/.webpack/service/webpack:/node_modules/aws-sdk/lib/sequential_executor.js:106:1)\n at Request.emit (/Users/michael/Development/javascript/data-export-lambda/.webpack/service/webpack:/node_modules/aws-sdk/lib/sequential_executor.js:78:1)\n at Request.emit (/Users/michael/Development/javascript/data-export-lambda/.webpack/service/webpack:/node_modules/aws-sdk/lib/request.js:683:1)\n at Request.transition (/Users/michael/Development/javascript/data-export-lambda/.webpack/service/webpack:/node_modules/aws-sdk/lib/request.js:22:1)\n at AcceptorStateMachine.runTo (/Users/michael/Development/javascript/data-export-lambda/.webpack/service/webpack:/node_modules/aws-sdk/lib/state_machine.js:14:1)\n at /Users/mi...
statusCode:400
当我查询 DynamoDB 以列出我的表时,我得到:
aws dynamodb list-tables --endpoint-url http://localhost:8000
{
"TableNames": [
"data-export-scheduler-dev"
]
}
Table 名称绝对匹配:
我的serverless.yaml是:
service:
name: data-export-scheduler
# Add the serverless-webpack plugin
plugins:
- serverless-webpack
- serverless-dynamodb-local
- serverless-offline
# serverless offline config
custom:
serverless-offline:
port: 4000
babelOptions:
presets: ["es2015"]
dynamodb:
stages:
- dev
start:
migrate: true
provider:
name: aws
region: us-east-1
runtime: nodejs8.10
environment:
DYNAMODB_TABLE: ${self:service}-${opt:stage, self:provider.stage}
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"
functions:
exportAPI:
handler: handler.exportAPI
events:
- http:
method: post
path: export
resources:
Resources:
ExportDB:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
AttributeDefinitions:
- AttributeName: location
AttributeType: S
KeySchema:
- AttributeName: location
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:provider.environment.DYNAMODB_TABLE}
有谁知道我可能做错了什么?表就在那里,我可以从 DynamoDB CLI 查询它,但是当我尝试从代码查询它时,我总是收到 ResourceNotFoundException。
更新 1: 当我从 CLI 运行 查询时,我得到了同样的错误:
aws dynamodb query --table-name data-export-scheduler-dev --key-condition-expression "downloaded = :v1" --expression-attribute-values '{":v1": { "S" : "0"}}' $LOCAL
An error occurred (ResourceNotFoundException) when calling the Query operation: Requested resource not found
默认情况下,AWS SDK 在云中查找资源。因此,当尝试连接到您的 DynamoDB 实例时,它会根据您的配置尝试连接到位于 us-east-1
的名为 data-export-scheduler-dev
的 DynamoDB table。
您必须通过在其构造函数中定义 endpoint
属性来告诉 SDK 查找本地 DynamoDB 实例。
变化:
constructor() {
this.dynamoDb = new DynamoDB.DocumentClient({region: 'us-east-1'})
}
到
constructor() {
this.dynamoDb = new DynamoDB.DocumentClient({region: 'us-east-1', endpoint: 'http://localhost:8000'})
}
我正在尝试使用无服务器和 DynamoDB 组合一个非常简单的 AWS Lambda。
我创建项目的代码是:
dynamoDb: DocumentClient
constructor() {
this.dynamoDb = new DynamoDB.DocumentClient({region: 'us-east-1'})
}
saveFiles(files: File[]): Promise<boolean> {
return new Promise<boolean>((resolve, reject ) => {
files.forEach(file => {
const tableName = process.env.DYNAMODB_TABLE
this.dynamoDb.put({
TableName: tableName,
Item: {
downloaded: {N : `${file.downloaded ? 1 : 0}`},
location: {S: `${file.location}`}
}
}, (error, result) => {
if (!!error) {
reject(error)
return
}
console.debug(`DB Save result: ${JSON.stringify(result)}`)
resolve(true)
})
})
})
}
错误是:
handler.ts:41
code:"ResourceNotFoundException"
message:"Requested resource not found"
name:"ResourceNotFoundException"
requestId:"2ST4DCE3NJ85UE32OAD6PUMTBJVV4KQNSO5AEMVJF66Q9ASUAAJG"
retryable:false
retryDelay:23.64284067311807
stack:"ResourceNotFoundException: Requested resource not found\n at Request.extractError (/Users/michael/Development/javascript/data-export-lambda/.webpack/service/webpack:/node_modules/aws-sdk/lib/protocol/json.js:51:1)\n at Request.callListeners (/Users/michael/Development/javascript/data-export-lambda/.webpack/service/webpack:/node_modules/aws-sdk/lib/sequential_executor.js:106:1)\n at Request.emit (/Users/michael/Development/javascript/data-export-lambda/.webpack/service/webpack:/node_modules/aws-sdk/lib/sequential_executor.js:78:1)\n at Request.emit (/Users/michael/Development/javascript/data-export-lambda/.webpack/service/webpack:/node_modules/aws-sdk/lib/request.js:683:1)\n at Request.transition (/Users/michael/Development/javascript/data-export-lambda/.webpack/service/webpack:/node_modules/aws-sdk/lib/request.js:22:1)\n at AcceptorStateMachine.runTo (/Users/michael/Development/javascript/data-export-lambda/.webpack/service/webpack:/node_modules/aws-sdk/lib/state_machine.js:14:1)\n at /Users/mi...
statusCode:400
当我查询 DynamoDB 以列出我的表时,我得到:
aws dynamodb list-tables --endpoint-url http://localhost:8000
{
"TableNames": [
"data-export-scheduler-dev"
]
}
Table 名称绝对匹配:
我的serverless.yaml是:
service:
name: data-export-scheduler
# Add the serverless-webpack plugin
plugins:
- serverless-webpack
- serverless-dynamodb-local
- serverless-offline
# serverless offline config
custom:
serverless-offline:
port: 4000
babelOptions:
presets: ["es2015"]
dynamodb:
stages:
- dev
start:
migrate: true
provider:
name: aws
region: us-east-1
runtime: nodejs8.10
environment:
DYNAMODB_TABLE: ${self:service}-${opt:stage, self:provider.stage}
iamRoleStatements:
- Effect: Allow
Action:
- dynamodb:Query
- dynamodb:Scan
- dynamodb:GetItem
- dynamodb:PutItem
- dynamodb:UpdateItem
- dynamodb:DeleteItem
Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"
functions:
exportAPI:
handler: handler.exportAPI
events:
- http:
method: post
path: export
resources:
Resources:
ExportDB:
Type: 'AWS::DynamoDB::Table'
DeletionPolicy: Retain
Properties:
AttributeDefinitions:
- AttributeName: location
AttributeType: S
KeySchema:
- AttributeName: location
KeyType: HASH
ProvisionedThroughput:
ReadCapacityUnits: 1
WriteCapacityUnits: 1
TableName: ${self:provider.environment.DYNAMODB_TABLE}
有谁知道我可能做错了什么?表就在那里,我可以从 DynamoDB CLI 查询它,但是当我尝试从代码查询它时,我总是收到 ResourceNotFoundException。
更新 1: 当我从 CLI 运行 查询时,我得到了同样的错误:
aws dynamodb query --table-name data-export-scheduler-dev --key-condition-expression "downloaded = :v1" --expression-attribute-values '{":v1": { "S" : "0"}}' $LOCAL
An error occurred (ResourceNotFoundException) when calling the Query operation: Requested resource not found
默认情况下,AWS SDK 在云中查找资源。因此,当尝试连接到您的 DynamoDB 实例时,它会根据您的配置尝试连接到位于 us-east-1
的名为 data-export-scheduler-dev
的 DynamoDB table。
您必须通过在其构造函数中定义 endpoint
属性来告诉 SDK 查找本地 DynamoDB 实例。
变化:
constructor() {
this.dynamoDb = new DynamoDB.DocumentClient({region: 'us-east-1'})
}
到
constructor() {
this.dynamoDb = new DynamoDB.DocumentClient({region: 'us-east-1', endpoint: 'http://localhost:8000'})
}