AWS Elasticache Serverless Redis Connection Problems With IORedis


Are you trying to connect to an AWS Elasticache Serverless Redis instance but it is just not happening? You have double checked your security groups, your subnets, and you are just not connecting? I was usingioredis to connect but it kept timing out as if it was blocked by a security group. After verifying that was not the case I turned on ioredis’s debug logging and saw it was indeed able to at least open a connection to redis but it was failing after that.

What it turned out to be was an issue with TLS and the solution was to turn off TLS when initializing the ioredis client. Since we have multiple other layers of security between the Security Groups and VPCs additional authentication for this project, the indexes for my daily writing, seemed unnecessary so I turned off the TLS and it worked.

Here is the code I am using to initialize the connection:

let _redis: Redis;
export const getRedis = (): Redis => {
    if (!_redis) {
        console.log("Connecting Redis:", process.env.REDIS_HOST)
        let options: any = {
            host: process.env.REDIS_HOST,
            port: 6379,
            // tls: {}
        }
        if (options.host !== '127.0.0.1') {
            options.tls = {};
        }
        _redis = new Redis(options);
        console.log("Redis Successful:", process.env.REDIS_HOST)
    }
    return _redis;
}

Note that when I develop locally I use a local Docker Compose file to boot up a local copy of redis. Therefore the localhost check.

Best of luck and feel free to ping me if you have questions.

~Cheers

PS: If you are interested in engaging my services as a Web Application Architect check out my Group Coaching Program or 1 on 1 Consulting at schematical.com and check out my FREE eBook 20 Things You Can DoTo Save Money On Your Amazon Web Services Bill Today


Buy me a coffee if you found this helpful.

© Copright 2024 Schematical
17 S. Fairchild St. FL 7, Madison, WI 53703
DISCLAIMER: The information provided is for general informational purposes only. Posts and other information may not be updated to account for changes in the law and should not be considered advice. None of the articles or posts on this website are intended to create an attorney-client relationship. You should consult with legal and/or financial advisors for legal and tax advice tailored to your specific circumstances.