如何从同一个lambda函数访问AWS Elasticache(Redis)和AWS EC2(MongoDB)?

时间:2017-07-13 19:37:38

标签: mongodb aws-lambda amazon-vpc serverless-framework

我可以单独访问,但我无法连接这两种服务。(AWS Elasticache(Redis)和AWS EC2(MongoDB))。我在AWS VPC中的同一个安全组中试过了:EC2,Elasticache和Lambda。还尝试了不同安全组中的EC2和Elasticache AWS VPC和lambda具有这些安全组。但没有改变任何事情。我使用无服务器框架1.0。 localhost redis和EC2(MongoDB)中的这些连接成功地协同工作。当我写入控制台无服务器部署时,这些操作无法正常工作。邮差错误是:502 Bad Gateway。

{
    "message": "Internal server error"
}

这是(我可以成功连接到redis):

var client = redis.createClient(config.cache.connection.port, config.cache.connection.endpoint);
                client.on("error", function (err) {
                    if(err){
                        console.log("Error: Redis connection was failed");
                    }else{
                        console.log("Success: Redis connection was established");
                    }
                });   
                client.set("FEED10", "feed", redis.print);
                client.get("FEED10", function(err, data){
                    if(err){
                        console.log("Error: ", err);
                    }
                    console.log("DATA: ", data);
                    client.quit();
                });

这是(ı可以成功连接到mongodb)

        var options = {
            user: config.database.connection.user,
            pass: config.database.connection.password,
            promiseLibrary: require('bluebird')
        }
        mongoose.connect(config.database.connection.endpoint, options, function(error) {
        // Check error in initial connection. There is no 2nd param to the callback.
            if(error){
                console.log("Error: MongoDB connection was failed");
            }
            else{
                console.log("Success: MongoDB connection was established");
            }
        });

这是(我可以连接MongoDB,但不能重新连接)。有什么想法吗?

var options = {
            user: config.database.connection.user,
            pass: config.database.connection.password,
            promiseLibrary: require('bluebird')
        }
        mongoose.connect(config.database.connection.endpoint, options, function(error) {
        // Check error in initial connection. There is no 2nd param to the callback.
            if(error){
                console.log("Error: MongoDB connection was failed");
            }
            else{
                console.log("Success: MongoDB connection was established");
            }
        });

var client = redis.createClient(config.cache.connection.port, config.cache.connection.endpoint);
                client.on("error", function (err) {
                    if(err){
                        console.log("Error: Redis connection was failed");
                    }else{
                        console.log("Success: Redis connection was established");
                    }
                });   
                client.set("FEED10", "feed", redis.print);
                client.get("FEED10", function(err, data){
                    if(err){
                        console.log("Error: ", err);
                    }
                    console.log("DATA: ", data);
                    client.quit();
                });

0 个答案:

没有答案