0

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}`);
};

3 Answers 3

6

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I fought with this for a full day, so dumb.
Great it solve my problem
1

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

1 Comment

Not an answer, please delete and add it as a comment
1

Your Redis cache instance, EC2 instance if you have one, and Lambda all need to share the same security groups and subnets.

Also, in your security group, make sure you have an inbound rule set up with Custom TCP and port 6379.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.