Search code examples
node.jsamazon-web-servicesredisamazon-ecsamazon-elasticache

Node process from ECS connects to AWS Elasticache but then just stops


I'm trying to communicate with a Redis Elasticache from an ECS instance using NodeJS, and the logs show that it connects but then nothing else happens.

  • They're on the same VPC
  • They're on the same subnets
  • SecurityGroup for ECS allows any outbound TCP traffic
  • SecurityGroup for Elasticache allows inbound TCP traffic on 6379 and 6380

Here's the script I'm running:

const redis = require("redis");

(async () => {
  try {
    const client = redis.createClient({
      url: "redis://demo-cache-123abc.serverless.use1.cache.amazonaws.com:6379",
    });

    client.on("connect", () => console.log("Redis Client Connected"));
    client.on("ready", () => console.log("Redis Client is ready."));
    client.on("end", () => console.log("Redis Client has ended."));
    client.on("error", (err) => console.log("Redis Client Error", err));
    client.on("reconnecting", (err) => console.log("Redis Client is reconnecting...", err));

    console.log(`connecting...`);
    await client.connect();
    console.log(`connected!`);

    await client.set("key", "value123");
    const value = await client.get("key");

    console.log("value", value);

    await client.disconnect();
  } catch (error) {
    console.log("error:");
    console.log(error);
  }
})();

The logs that get printed are this:

connecting...
Redis Client Connected

And then it doesn't log anything else... No errors. No nothing...

What makes it more strange is that the process continues running. If I add setInterval(() => console.log(Date.now()), 3000) then the logs print a new timestamp every 3 seconds. But my code below the line await client.connect(); never executes. Almost like the promise never resolves...

No error is ever logged. The task never crashes. It just keeps running, and says it's connected, but it doesn't ever interact with Redis. The code just stops executing...

I don't even know what to ask, other than how can I get this working?! What settings can I add or remove or double check?


Solution

  • On AWS, a TLS connection is mandatory (I don't see a way to turn it off, and I don't want to anyway), so I just needed to add a simple config and the above started working.

    const client = redis.createClient({
      url: "redis://demo-cache-123abc.serverless.use1.cache.amazonaws.com:6379",
      socket: {
        tls: true,
      },
    });
    

    Shouts out to this answer from another post: https://stackoverflow.com/a/75557575/4927236