I'm trying to write an AWS Lambda function which uses Redis. When I run the code below:
'use strict'
function handler (data, context, callback) {
const redis = require("redis")
const _ = require("lodash")
console.log('before client')
const client = redis.createClient({
url: 'redis://cache-url.euw1.cache.amazonaws.com:6379',
})
console.log('after client')
callback(null, {status: 'result'})
console.log('after callback')
}
exports.handler = handler
I have an answer like this:
{
"errorMessage": "2016-09-20T15:22:27.301Z 07d24e0b-7f46-11e6-85e9-e5f48906c0da Task timed out after 3.00 seconds"
}
and logs look like:
17:22:24
START RequestId: 07d24e0b-7f46-11e6-85e9-e5f48906c0da Version: $LATEST
17:22:26
2016-09-20T15:22:26.014Z 07d24e0b-7f46-11e6-85e9-e5f48906c0da before client
17:22:26
2016-09-20T15:22:26.134Z 07d24e0b-7f46-11e6-85e9-e5f48906c0da after client
17:22:26
2016-09-20T15:22:26.135Z 07d24e0b-7f46-11e6-85e9-e5f48906c0da after callback
17:22:27
END RequestId: 07d24e0b-7f46-11e6-85e9-e5f48906c0da
17:22:27
REPORT RequestId: 07d24e0b-7f46-11e6-85e9-e5f48906c0da Duration: 3001.81 ms Billed Duration: 3000 ms Memory Size: 128 MB Max Memory Used: 24 MB
17:22:27
2016-09-20T15:22:27.301Z 07d24e0b-7f46-11e6-85e9-e5f48906c0da Task timed out after 3.00 seconds
which, IMHO, means that callback was called but nothing happened.
When I remove client's initialization I see proper response.
Any Ideas?
From the official documentation:
Since you are calling the callback, but your Lambda function invocation is not ending, it appears you still have something on the event loop. Your function isn't really doing anything except creating a Redis connection. I'm guessing you need to close the Redis connection when you are done with it, in order to clear the event loop and allow the Lambda invocation to complete.