AWS Lambda and Redis client. Why can't I call

2019-05-18 07:14发布

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?

1条回答
我只想做你的唯一
2楼-- · 2019-05-18 08:17

From the official documentation:

When the callback is called, the Lambda function exits only after the Node.js event loop is empty.

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.

查看更多
登录 后发表回答