【发布时间】:2021-05-18 11:48:24
【问题描述】:
下面是 dynamo-table.yml
Resources:
AuctionsTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: AuctionsTable
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: id,
AttributeType: S
KeySchema:
- AttributeName: id,
KeyType: HASH
以下是在 dynamodb 中插入记录的处理程序代码。
import AWS from 'aws-sdk';
import { v4 as uuid } from 'uuid';
const dynamodb = new AWS.DynamoDB.DocumentClient();
async function createAuction(event, context) {
const { title } = JSON.parse(event.body);
const now = new Date();
const params = {
TableName: 'AuctionsTable',
Item: {
'id' : {S: `${uuid()}`},
'title' : {S: `${title}`},
'status': {S: `OPEN`},
'createdAt': {S: now.toISOString()}
}
};
console.log(`Auction: ${JSON.stringify(params)}`);
await dynamodb.put(params).promise();
return {
statusCode: 201,
body: JSON.stringify(params),
};
}
export const handler = createAuction;
下面是serverless.yml文件的内容,
service:
name: auction-service
plugins:
- serverless-bundle
- serverless-pseudo-parameters
provider:
name: aws
runtime: nodejs12.x
memorySize: 128
stage: ${opt:stage, 'dev'}
region: ap-south-1
resources:
- ${file(src/resources/dynamodb-table.yml)}
- ${file(src/resources/role.yml)}
functions:
createAuction:
handler: src/handlers/createAuction.handler
role: auctionServiceRole
events:
- http:
method: POST
path: /auction
custom:
bundle:
linting: false
tableName: "AuctionsTables"
问题:当我点击 POST URL 并访问 CloudWatch 日志时,出现以下错误,
{
"errorType": "ValidationException",
"errorMessage": "One or more parameter values were invalid: Missing the key id, in the item",
"code": "ValidationException",
"message": "One or more parameter values were invalid: Missing the key id, in the item",
"time": "2021-02-15T15:02:42.606Z",
"requestId": "C344O7CTTHF3PAR1OP8BQIFBT7VV4KQNSO5AEMVJF66Q9ASUAAJG",
"statusCode": 400,
"retryable": false,
"retryDelay": 8.687787589723229,
"stack": [
"ValidationException: One or more parameter values were invalid: Missing the key id, in the item",
" at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:52:27)",
" at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
" at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)",
" at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:688:14)",
" at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)",
" at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)",
" at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10",
" at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)",
" at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:690:12)",
" at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:116:18)"
]
}
同样在邮递员中,它返回状态码为 502 Bad gateway,
{
"message": "Internal server error"
}
问题:可能是什么问题?即使是很小的帮助也会很有帮助。提前致谢。
【问题讨论】:
标签: aws-lambda amazon-dynamodb serverless-framework aws-serverless