嘗試訪問DynamoDB問題時,我正面臨其中一個AWS Lambda node.js超時,但症狀顯示不同,並且我找到的解決方案無法解決此問題。AWS Lambda node.js嘗試訪問DynamoDB時發生超時
超時設置爲5分鐘,內存設置爲128MB,但不超過30MB的使用量。
的角色IAM策略:
- AWSLambdaFullAccess
- AmazonDynamoDBFullAccess
- AWSLambdaVPCAccessExecutionRole
默認VPC有7個安全組,包括默認安全組:
- Inbo UND:所有流量,所有協議,所有端口範圍,
- 出站:所有流量,所有協議,所有端口範圍,0.0.0.0/0
這裏是代碼:
var aws = require('aws-sdk');
exports.handler = function(event, context) {
var dynamo = new aws.DynamoDB();
dynamo.listTables(function(err, data) {
if (err) {
context.fail('Failed miserably:' + err.stack);
} else {
context.succeed('Function Finished! Data :' + data.TableNames);
}
});
};
而且結果:
START RequestId: 5d2a0294-fb6d-11e6-989a-edaa5cb75cba Version: $LATEST
END RequestId: 5d2a0294-fb6d-11e6-989a-edaa5cb75cba
REPORT RequestId: 5d2a0294-fb6d-11e6-989a-edaa5cb75cba Duration: 300000.91 ms Billed Duration: 300000 ms Memory Size: 128 MB Max Memory Used: 21 MB
2017-02-25T15:21:21.778Z 5d2a0294-fb6d-11e6-989a-edaa5cb75cba Task timed out after 300.00 seconds
解決相關node.js版本問題here不適用於我,並返回"ReferenceError: https is not defined at exports.handler (/var/task/index.js:6:16)"
。此外,AWS已棄用0.10版本。
這裏是代碼與HTTPS參考:
var aws = require('aws-sdk');
exports.handler = function(event, context) {
var dynamo = new aws.DynamoDB({
httpOptions: {
agent: new https.Agent({
rejectUnauthorized: true,
secureProtocol: "TLSv1_method",
ciphers: "ALL"
})
}
});
dynamo.listTables(function(err, data) {
if (err) {
context.fail('Failed miserably:' + err.stack);
} else {
context.succeed('Function Finished! Data :' + data.TableNames);
}
});
};
結果:
START RequestId: 6dfd3db7-fae0-11e6-ba81-a52f5fc3c3eb Version: $LATEST
2017-02-24T22:27:31.010Z 6dfd3db7-fae0-11e6-ba81-a52f5fc3c3eb ReferenceError: https is not defined
at exports.handler (/var/task/index.js:6:16)
END RequestId: 6dfd3db7-fae0-11e6-ba81-a52f5fc3c3eb
REPORT RequestId: 6dfd3db7-fae0-11e6-ba81-a52f5fc3c3eb Duration: 81.00 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 26 MB
RequestId: 6dfd3db7-fae0-11e6-ba81-a52f5fc3c3eb Process exited before completing request
設置爲5分鐘,我不能相信,AWS將不能超時在分配的時間範圍內返回表的列表,並且權限問題通常出現在日誌中。
感謝您關注此事。
更新:使用「No VPC」允許正確完成,因此它肯定是VPC問題:START RequestId:11930536-fb72-11e6-a386-25115dc82eee版本:$最新的 END RequestId:11930536-fb72-11e6-a386- 25115dc82eee 報告請求ID:11930536-fb72-11e6-a386-25115dc82eee \t時間:978.54毫秒\t標榜時間:1000毫秒\t內存大小:128 MB \t最大內存使用:28 MB \t我怎樣才能解決的VPC錯誤配置? – prg281
很明顯,VPC是你真正的問題,但是對於'https沒有定義'的問題,你只需要在文件的頂部添加'var https = require('https')'。 – idbehold
您運行此Lambda函數的VPC是否具有NAT網關?您選擇的VPC和子網中只有私有IP的實例能否實際路由到互聯網? – jarmod