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?

gefragt vor 2 Jahren11546 Aufrufe
1 Antwort
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
SUPPORT-TECHNIKER
Tulio_M
beantwortet vor 2 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen