我正在使用React-Native播放AWS Mobilehub,我希望它能为我加快后端托管的速度。
但是,我无法使其后端API工作。长期与文档进行了长期之后,我将其lambda功能和DynamoDB服务之间的问题弄清楚。
任何想法都非常感谢!
问题#1
标题为"我的AWS lambda函数"可以请求其DynamoDB,但没有响应。这里出了什么问题?还是如何从AWS DynamoDB获得调试信息?(我gg and启用了cloudtrial,但似乎也没有dynamoDB的操作日志。(
lambda side
在这里我有最简单的node.js 6.10代码:
const AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-2'});
const dynamodb = new AWS.DynamoDB.DocumentClient();
exports.handler = function(event, context, callback) {
var responseCode = 200;
var requestBody, httpMethod, res;
console.log("request: " + JSON.stringify(event));
// Request Body
requestBody = event.body;
/*testing dynamodb with put*/
console.log("PUT begins");
let putItemParams2 = {
TableName: "xxxx-mobilehub-xxxx-Test",//tableName
Item: {businessId:'putItemParams2r3',test:'yooo', hmm:'hhhh'}
};
console.log("putItemParams2: ",putItemParams2);
dynamodb.put(putItemParams2, (err, data) => {
console.log("putItemParams2");
if (err) console.log("dynamodb err: ",err, err.stack); // an error occurred
else console.log("dynamodb data: ",data); // successful response
});
console.log("PUT end");
var response = {
statusCode: responseCode
//....
};
...
//comment out context.succeed here to avoid program termination before intended.
//console.log("response: " + JSON.stringify(response))
//context.succeed(response);
};
logs
当触发以前的代码时,从AWS CloudWatch我可以看到日志:
START RequestId: 3d7c5f7f-1b98-11e8-ad00-93a6d10c8f4e Version: $LATEST
[timestamp] PUT begins
[timestamp] putItemParams2: { TableName: 'xxx-mobilehub-xxxx-Test',
Item: { businessId: 'putItemParams2r3', test: 'yooo', hmm: 'hhhh'}}
[timestamp] put end
END RequestId: 3d7c5f7f-1b98-11e8-ad00-93a6d10c8f4e
所以没有错误,没有数据,没有响应。我检查了DynamoDB,没有插入。
额外信息
条件#1:此DynamoDB表具有公共访问权限,因为我想排除Auth问题。
条件#2:我确保我的lambda功能可以访问这些表。例如ARN:AWS:dynamodb::xxxx:table/xxxx-mobilehub-xxxx- 允许一切
条件#3:我构建了一个简单的node.js来执行(aws-sdk(,并且该服务器在相同的代码方面很好地工作。我可以"获取"&" put" int&从我的DynamoDB表中出来。
问题#2
我的反应代码使用" AWS-弹药反应新生"。api.put很好,lambda函数至少是接收API调用(来自问题#1(。
但是,api.get返回我403错误,lambda函数甚至没有该操作的日志。
async function getBusiness(){
const path = "/Test";
const api = "TestCRUD";
let queryGetBusiness = {body: {userId: "hmmm"}};
try {
let apiResponse = await API.get(api, path, queryGetBusiness)//.then((data)=>{console.log(data)});
let apiResponseJson = await JSON.stringify(apiResponse);
console.log("response from saving Business: " + apiResponseJson);
}
catch (e) {console.log(e);}
}
P.S.(AWS可以在此Mobilehub上做得更好。他们的文档缺乏细节,AWSMOBILE CLOUD-API INDOKE有一些问题。(
const AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-2' });
const dynamodb = new AWS.DynamoDB.DocumentClient();
exports.handler = function (event, context, callback) {
var responseCode = 200;
var requestBody, httpMethod, res;
console.log("request: " + JSON.stringify(event));
// Request Body
requestBody = event.body;
/*testing dynamodb with put*/
console.log("PUT begins");
let putItemParams2 = {
TableName: "xxxx-mobilehub-xxxx-Test",//tableName
Item: { businessId: 'putItemParams2r3', test: 'yooo', hmm: 'hhhh' }
};
console.log("putItemParams2: ", putItemParams2);
dynamodb.put(putItemParams2, (err, data) => {
console.log("putItemParams2");
if (err) console.log("dynamodb err: ", err, err.stack); // an error occurred
else {
console.log("dynamodb data: ", data);
context.succeed(response);
}
// Call these here
console.log("PUT end");
});
//console.log("response: " + JSON.stringify(response))
};
确保您在回调功能中调用context.succeed
。像上面。
您也可以将第三个参数用于handler
函数-callback
而不是context.succeed
,例如callback(null, response);