Connecting to elastiCache cluster with node.js

2

I am struggling to find node examples in connecting to my Elasticache cluster via node.js lambda using node-redis.

const { createCluster }= require('redis');
const redisClient = require('redis');

function createRedisClient() {
    const client = createCluster({
        rootNodes: [
            {
                host: process.env.redis_endpoint // This is the configuration endpoint given for the cluster plus port
            }
        ]      
    });
    return client;
}

I get an invalid protocol when I try to connect. I have tried adding 3 of the shard endpoints to the above code with the same result. I have tried just creating a redis client( rather than cluster) and get the same results. How is this done?

preguntada hace 2 años11546 visualizaciones
1 Respuesta
2

Hello,

as per node-redis source code , the error is thrown when ParseURL() finds an invalid configuration:

if (protocol === 'rediss:') {
	(parsed.socket as RedisTlsSocketOptions).tls = true;
} else if (protocol !== 'redis:') {
	throw new TypeError('Invalid protocol');
}

I could not find "host" as a valid parameter for CreateCluster().rootNodes configuration.

You probably want to replace host by url and make sure that process.env.redis_endpoint expands to a valid URL like:

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

Example:

  • no in-transit encryption
const client = createCluster({
    rootNodes: [
      {
        url: 'redis://Elasticache_Cluster_Endpoint_here:6379'
      }
    ]
  });
  • in-transit encryption without authentication:
const client = createCluster({
    rootNodes: [
      {
        url: 'rediss://Elasticache_Cluster_Endpoint_here:6379'
      }
    ]
  });

  • in-transit encryption and RBAC/ACL authentication:
const client = createCluster({
    rootNodes: [
      {
        url: 'rediss://user_name:password@Elasticache_Cluster_Endpoint_here:6379'
      }
    ]
  });

AWS
INGENIERO DE SOPORTE
Tulio_M
respondido hace 2 años

No has iniciado sesión. Iniciar sesión para publicar una respuesta.

Una buena respuesta responde claramente a la pregunta, proporciona comentarios constructivos y fomenta el crecimiento profesional en la persona que hace la pregunta.

Pautas para responder preguntas