0

I am trying to implement AWS ElastiCache in AWS Lambda function. Here's my code

const { createClient } = require('redis');

const handler = async (event, context) => {

    const url = "xxxxxxxxxx.use2.cache.amazonaws.com";

    const client = createClient({
        socket: {
            host: url,
            port: 6379,
        }
    });
    client.on('error', error => console.error('Redis Client Error:', error));
    client.on('connect', () => console.log('Redis Client Connected!'));

    try {
        await client.connect();

        let date = await client.get('my-date');
        
        if(!date) {
            date = new Date().toString();
            await client.set('my-date', date);
            
            console.log('Cached: ', date);
        }

        return date;
    } catch(error) {
        console.error('Error wups', error);
        throw error;
    } finally {
        await client.disconnect();
    }
};

module.exports = {
    handler
};

Error:

{
  "errorType": "Sandbox.Timedout",
  "errorMessage": "RequestId: 668291d5-e1ee-4ce1-9256-428ddca42be9 Error: Task timed out after 30.00 seconds"
}

Function Logs
.com'
}
2024-08-21T06:00:15.529Z    668291d5-e1ee-4ce1-9256-428ddca42be9    ERROR   Redis Client Error: Error: getaddrinfo ENOTFOUND xxxxxxxxxxxxxxx.use2.cache.amazonaws.com
    at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:120:26) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'redis-new-vpc-sfkev9.serverless.use2.cache.amazonaws.com'
}

I've tried multiple approaches, but unable to connect to AWS ElastiCache. I've made VPC with three subnet mask but unable to connect to ElastiCache.

3
  • redis-new-vpc-sfkev9.serverless.use2.cache.amazonaws.com doesn't look like a valid Elasticache URL. Where are you getting that URL from? Please review this: docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/… Commented Aug 21, 2024 at 12:29
  • Check the role attached to your lambda, does it have the permissions needed to talk to ElastiCache. Also, check your security groups and make sure you have allowed access to the ElastiCache security group. Commented Aug 22, 2024 at 1:24
  • @MarkB i did the setup of cluster disable mode haivng multiple nodes, it gives the same endpoint which you have mentioned. any video tutotial or any blog which explains every step in details Commented Aug 26, 2024 at 11:54

0

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.