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

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

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:

which, IMHO, means that callback was called but nothing happened.

When I remove client's initialization I see proper response.

Any Ideas?

回答1:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!