我对Lambda,SAM和DynamoDB都很陌生。我希望通过匹配DynamoDB属性从template.yml
表中选择一个记录,该属性定义为template.yml
中表定义中的排序键。当我调用这个函数时,我得到的只是:
{"errorType":"ValidationException","errorMessage":"The provided key element does not match the schema"}
下面是SAM模板定义
CustomersTable:
Type: AWS::DynamoDB::Table
Properties:
ProvisionedThroughput:
ReadCapacityUnits: 50
WriteCapacityUnits: 100
AttributeDefinitions:
-
AttributeName: "id"
AttributeType: "S"
-
AttributeName: "email"
AttributeType: "S"
KeySchema:
-
AttributeName: "id"
KeyType: "HASH"
-
AttributeName: "email"
KeyType: "RANGE"
这是NodeJS代码。
const { v4: uuidv4 } = require('uuid');
const dynamodb = require('aws-sdk/clients/dynamodb');
const docClient = new dynamodb.DocumentClient();
const tableCustomers = process.env.TABLE_CUSTOMERS;
exports.handlerFunction = async (event) => {
const body = JSON.parse(event.body);
const email = (body.hasOwnProperty('email')) ? body.email : null;
let emailData = await docClient.get({
TableName: tableCustomers,
Key: { email: email },
AttributesToGet: ['email']
}).promise();
const response = {
statusCode: 200,
body: JSON.stringify({ EMAILDATA: emailData }),
headers: {
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Allow-Origin": "*", // Allow from anywhere
"Access-Control-Allow-Methods": "POST" // Allow only GET request
},
};
// All log statements are written to CloudWatch
console.info(`response from: ${event.path} statusCode: ${response.statusCode} body: ${response.body}`);
return response;
}
这里面有什么不对的?
发布于 2022-01-25 19:27:35
TL;DR您的查询缺少分区键id
的值。
您的表模式具有复合主键 of id
("Hash“或"Partition”键)和email
(“范围”或“排序”键)。docClient.get
执行一个DynamoDB查询操作,它根据主键值查找项:
您必须提供分区键属性的名称和该属性的单个值。查询返回具有该分区键值的所有项。或者,您可以提供一个排序键属性,并使用比较运算符来细化搜索结果。
https://stackoverflow.com/questions/70849059
复制相似问题