skip to Main Content

I’m trying to connect to Elasticache redis instance from lambda. Lambda and Elasticache are on same vpc and Lambda’s execution role has all the permission for elasticache. But still I’m getting ECONNREFUSED.

const redis = require("redis");
const AWS = require("aws-sdk");
const apig = new AWS.ApiGatewayManagementApi({
  endpoint: process.env.ApiGatewayEndpoint,
});

exports.handler = async (event, context) => {
  const redisClient = redis.createClient({
    host: "botgo-cache.fhvwf2.ng.0001.aps1.cache.amazonaws.com",
    port: "6379",
  });
  
  await redisClient.connect();
  

  redisClient.on("connect", (err) => {
    console.log("Redis Connected " + err);
  });

  redisClient.on("error", (err) => {
    console.log("Redis Error " + err);
  });

  const TEST_KEY = "test_node";

  await redisClient.json.set(TEST_KEY, ".", { node: 4303 });
  const value = await redisClient.json.get(TEST_KEY, {
    // JSON Path: .node = the element called 'node' at root level.
    path: ".node",
  });

  console.log(`value of node: ${value}`);
};

2

Answers


  1. Have you added security group to your Elasticache Redis cluster to allow incoming traffic to port 6379?

    Login or Signup to reply.
  2. I was able to replicate your issue on my end. Do this:

    const redisClient = redis.createClient({
        url: "rediss://botgo-cache.fhvwf2.ng.0001.aps1.cache.amazonaws.com:6379"
      });
    

    This should fix it! And the rediss with double ss is important

    Hope this helps!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search