javascriptnode.jsredisamazon-elasticache

aws Redis Connection Fails over authentication via NodeJS


I'm trying to connect aws Redis Cache via NodeJS, however, it fails with the below error. But I'm able to successfully connect via redis-cli.

Command: node app.js 
node version: v16.14.0
aws redis engine version: 7.0.7


const redis = require('redis');
const { promisify } = require('util');
console.log('hello');

const redisClient = redis.createClient({
    socket: {
        host: "master.dev.xxx.use1.cache.amazonaws.com",
        port: 6379,
        tls:true,
        reconnectStrategy: function () {
            const retries = 35000;
            return retries;
        },
        password: "XXX",
    }
});

const connect = promisify(redisClient.connect).bind(redisClient);

connect()
    .then(() => {
        console.log("connected successfully");
    })
    .catch((error) => {
        console.log(error);
    });

redisClient.on('error', (err) => {
    console.log(err);
});

// Example: Setting a key
redisClient.set('Finalli', 'value', redis.print);

// Example: Getting a key
redisClient.get('key', function(err, reply) {
    console.log('Value: ' + reply);
});

ERROR RECEIVED:

node app.js
hello
node:internal/process/promises:265
    triggerUncaughtException(err, true /* fromPromise */);
    ^

[ErrorReply: NOAUTH Authentication required.]

I'm able to connect via redis-cli successfully though.

redis-cli -h master.dev.xxx.use1.cache.amazonaws.com -p 6379 --tls
> AUTH  XXX
OK
> set one 1
OK
> keys *
1) "one"

Could you please help ?


Solution

  • Try using the latest version of node-redis by running npm install redis, make sure it is 4.6 or higher

    The latest version does not need promisify.

    Try using the simple url method, without socket.

    Based on the package basic example:

    redis[s]://[[username][:password]@][host][:port][/db-number]

    const redis = require('redis');
    
    const client = await redis.createClient({
      url: 'redis://username:password@host.server.address:6379'
    })
      .on('error', err => console.log('Redis Client Error', err))
      .connect();
    

    Let us know if you get any other error