可以连接到redis,但设置/获取超时

2hh7jdfx  于 2021-06-10  发布在  Redis
关注(0)|答案(1)|浏览(481)

我正在尝试使用node\ redis客户端在elasticache redis上从aws lambda(nodejs)执行get()。我相信我能够连接到redis,但是当我尝试执行get()操作时,我会超时(lambda 60秒超时)。
我还授予了我的aws lambda管理员访问权限,以确保这不是权限问题。我通过转到aws控制台并单击test按钮来点击lambda。
这是我的redisclient.js:

const util = require('util');
const redis = require('redis');

console.info('Start to connect to Redis Server');
const client = redis.createClient({
    host: process.env.ElastiCacheEndpoint,
    port: process.env.ElastiCachePort
});

client.get = util.promisify(client.get);
client.set = util.promisify(client.set);

client.on('ready',function() {
    console.log(" subs Redis is ready");  //Can see this output in logs
});

client.on('connect',function(){
    console.log('subs connected to redis'); //Can see this output in logs
})

exports.set = async function(key, value) {
    console.log("called set!");
    return await client.set(key, value);
}

exports.get = async function(key) {
    console.log("called get!"); //Can see this output in logs
    return await client.get(key);
}

我的index.js调用redisclient.js:

const redisclient = require("./redisClient");

exports.handler = async (event) => {
    const params = event.params
    const operation = event.operation;
    try {

        console.log("Checking RedisCache by calling client get") // Can see this output in logs
        const cachedVal = await redisclient.get('mykey');
        console.log("Checked RedisCache by calling client get") // This doesn't show up in logs.
        console.log(cachedVal);
        if (cachedVal) {
            return {
                statusCode: 200,
                body: JSON.stringify(cachedVal)
            }
        } else {
            const setCache = await redisclient.set('myKey','myVal');
            console.log(setCache);
            console.log("*******")
            let response = await makeCERequest(operation, params, event.account);
            console.log("CE Request returned");
            return response;

        }
    }
    catch (err) {
        return {
            statusCode: 500,
            body: err,
        };
    }
}

这是我得到的输出(超时错误消息):

{
  "errorMessage": "2020-07-05T19:04:28.695Z 9951942c-f54a-4b18-9cc2-119eed65e9f1 Task timed out after 60.06 seconds"
}

我尝试过使用bluebird(将get改为getasync()),具体如下:https://github.com/utkarshyeolekar/promisify-redis-client/blob/master/redis.js 但还是有同样的行为。
我还将端口更改为使用随机值(如8088),我正在使用该值创建客户机(以查看失败连接的connect事件的行为)-在本例中,我仍然看到超时错误响应,但没有看到 subs Redis is ready 以及 subs connected to redis 在我的日志里。
谁能给我指一下正确的方向吗?我似乎不明白为什么我可以连接到redis,但是get()请求超时了。

dzjeubhm

dzjeubhm1#

我发现了这个问题,并在这里张贴,以防它有助于任何人在未来的行为不是很直观的我。
我启用了 AuthToken 在设置我的redis时。我用环境变量将param传递给lambda,但在发送get()/set()请求时没有使用它。当我禁用 AuthToken redis配置的要求-lambda能够通过get/set请求命中redis。有关authtoken的更多详细信息,请参见:https://docs.aws.amazon.com/awscloudformation/latest/userguide/aws-resource-elasticache-replicationgroup.html#cfn-elasticache复制组authtoken

相关问题