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.
redis-new-vpc-sfkev9.serverless.use2.cache.amazonaws.comdoesn'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/…