AWS Lambda在MongoDB Atlas连接上超时

时间:2018-11-19 15:22:37

标签: mongodb amazon-web-services aws-lambda

我只是想编写一个简单的Lambda函数,将数据插入我的MongoDB Atlas集群。我已将群集设置为接受所有传入流量(0.0.0.0/0),并确认我可以在本地连接。

对于AWS Lambda,我使用VPC向导设置了VPC,并为Lambda函数赋予了具有完全管理员访问权限的安全性角色。我将超时设置为12秒,但是仍然出现以下错误:

Response:
{
  "errorMessage": "2018-11-19T15:17:23.200Z 3048e1fd-ec0e-11e8-a03d-fb79584484c5 Task timed out after 11.01 seconds"
}

Request ID:
"3048e1fd-ec0e-11e8-a03d-fb79584484c5"

Function Logs:
START RequestId: 3048e1fd-ec0e-11e8-a03d-fb79584484c5 Version: $LATEST
2018-11-19T15:17:12.191Z    3048e1fd-ec0e-11e8-a03d-fb79584484c5    Calling MongoDB Atlas from AWS Lambda with event: {"address":{"street":"2 Avenue","zipcode":"10075","building":"1480","coord":[-73.9557413,40.7720266]},"borough":"Manhattan","cuisine":"Italian","grades":[{"date":"2014-10-01T00:00:00Z","grade":"A","score":11},{"date":"2014-01-16T00:00:00Z","grade":"B","score":17}],"name":"Vella","restaurant_id":"41704620"}
2018-11-19T15:17:12.208Z    3048e1fd-ec0e-11e8-a03d-fb79584484c5    => connecting to database
2018-11-19T15:17:12.248Z    3048e1fd-ec0e-11e8-a03d-fb79584484c5    (node:1) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
END RequestId: 3048e1fd-ec0e-11e8-a03d-fb79584484c5
REPORT RequestId: 3048e1fd-ec0e-11e8-a03d-fb79584484c5  Duration: 11011.08 ms   Billed Duration: 11000 ms   Memory Size: 128 MB Max Memory Used: 29 MB  
2018-11-19T15:17:23.200Z 3048e1fd-ec0e-11e8-a03d-fb79584484c5 Task timed out after 11.01 seconds

我用于连接的代码的相关部分是(使用user和pass作为适当的值):

const MongoClient = require('mongodb').MongoClient;
let atlas_connection_uri = "mongodb+srv://<user>:<pass>@restaurantcluster-2ylyf.gcp.mongodb.net/testdb"


let cachedDb = null;

exports.handler = (event, context, callback) => {
    var uri = atlas_connection_uri

    if (atlas_connection_uri != null) {
        processEvent(event, context, callback);
    } 
    else {
        atlas_connection_uri = uri;
        console.log('the Atlas connection string is ' + atlas_connection_uri);
        processEvent(event, context, callback);
    } 
};

function processEvent(event, context, callback) {
    console.log('Calling MongoDB Atlas from AWS Lambda with event: ' + JSON.stringify(event));
    var jsonContents = JSON.parse(JSON.stringify(event));

    //date conversion for grades array
    if(jsonContents.grades != null) {
        for(var i = 0, len=jsonContents.grades.length; i < len; i++) {

            jsonContents.grades[i].date = new Date();
        }
    }

    context.callbackWaitsForEmptyEventLoop = false;

    try {
        if (cachedDb == null) {
            console.log('=> connecting to database');
            MongoClient.connect(atlas_connection_uri, function (err, client) {
                cachedDb = client.db('testdb');
                return createDoc(cachedDb, jsonContents, callback);
            });
        }
        else {
            createDoc(cachedDb, jsonContents, callback);
        }
    }
    catch (err) {
        console.error('an error occurred', err);
    }
}

考虑到我可以从本地计算机进行连接的事实,我怀疑我的VPC防火墙/权限/安全组正在发生某些情况,但是我不知道在授予全部管理员特权时会发生什么情况担任安全角色,并将所有传出VPC流量都设置到我的公共子网。

在解决此问题方面,我将不胜感激!

编辑以提供更多信息:

功能console.logs'=>连接到数据库',然后立即在MongoClient.connect上超时(此后通过直接尝试console.log进行确认)。

0 个答案:

没有答案