By using AWS re:Post, you agree to the Terms of Use

Connecting to elastiCache cluster with node.js

0

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?

1 Answer
1

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'
      }
    ]
  });

SUPPORT ENGINEER
answered 6 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions