能够连接到 redis,但设置/获取超时



我正在尝试使用客户端从我的 AWS Lambda (NodeJS( 在 ElastiCache Redis 上执行 get(node_redis(。我相信我能够连接到 redis,但是当我尝试执行 get(( 操作时,我遇到了超时(Lambda 60 秒超时(。

我还授予了我的 AWS lambda 管理员访问权限,以确保这不是权限问题。我正在通过转到 AWS 控制台并单击测试按钮来点击 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);
}

这是我的索引.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"
}

我尝试使用蓝鸟(将get更改为getAsync(((:https://github.com/UtkarshYeolekar/promisify-redis-client/blob/master/redis.js 但仍然得到相同的行为。

我还更改了端口以使用我用于创建客户端的随机值(如 8088((以查看失败连接的连接事件的行为( - 在这种情况下,我仍然看到超时错误响应,但我在日志中看不到subs Redis is readysubs connected to redis

谁能指出我正确的方向?我似乎不明白为什么我能够连接到 redis 但 get(( 请求超时。

我想出了这个问题并在这里发布,以防将来对任何人有所帮助,因为这种行为对我来说不是很直观。

我在设置 redis 时启用了AuthToken参数。我正在使用环境变量将参数传递给 lambda,但在发送 get((/set(( 请求时没有使用它。当我从 redis 配置中禁用AuthToken要求时 - Lambda 能够通过 get/set 请求命中 redis。有关AuthToken的更多详细信息可以在这里找到:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-elasticache-replicationgroup.html#cfn-elasticache-replicationgroup-authtoken

最新更新