6

I'm trying to write an AWS Lambda function which uses redis(on amazon elasticcache). The problem – I can't connect to redis. I use code like this

'use strict'

function handler (data, context, cb) {
  const redis = require("redis")
  console.log('before client initialization')
  const client = redis.createClient({
    url: 'redis://propper-url-cache.some.0001.euw1.cache.amazonaws.com:6379',
    retry_strategy: function(options) {
      console.log(options)
      if (options.total_retry_time > 1000) {
        throw new Error('can`t connect to redis')
      }
    }
  })
  console.log('after client initialization')

  client.on("error", function (err) {
    console.log('in error')
    cb({error: err})
  });

  client.get("counter", function (err, counter) {
    console.log('counter', counter)
    if(_.isNull(counter)) {
      counter = 0
    }
    client.set('counter', counter + 1, function(err) {
      console.log(err)
      cb(null, {counter: counter})
    })
  });
}

exports.handler = handler

as a result I see something like this in logs:


15:33:41
START RequestId: d8024ec2-7f36-11e6-996c-1bfcb60572c6 Version: $LATEST

15:33:42
2016-09-20T13:33:42.632Z    d8024ec2-7f36-11e6-996c-1bfcb60572c6    before client initialization

15:33:42
2016-09-20T13:33:42.813Z    d8024ec2-7f36-11e6-996c-1bfcb60572c6    after client initialization

15:33:44
END RequestId: d8024ec2-7f36-11e6-996c-1bfcb60572c6

15:33:44
REPORT RequestId: d8024ec2-7f36-11e6-996c-1bfcb60572c6  Duration: 3002.67 ms    Billed Duration: 3000 ms Memory Size: 128 MB    Max Memory Used: 19 MB

15:33:44
2016-09-20T13:33:44.620Z d8024ec2-7f36-11e6-996c-1bfcb60572c6 Task timed out after 3.00 seconds

when I change redis url for something which definitely makes no sense I have an additional row:

2016-09-20T13:29:42.953Z    48fcb071-7f36-11e6-bc52-c5ac58c12843    { attempt: 1, error: { [Error: Redis connection to some-url.euw1.cache.amazonaws.com:6379 failed - getaddrinfo ENOTFOUND some-url.euw1.cache.amazonaws.com some-url.euw1.cache.amazonaws.com:6379] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo', hostna

Any ideas?

Mark B
  • 183,023
  • 24
  • 297
  • 295
kharandziuk
  • 12,020
  • 17
  • 63
  • 121
  • Did you enable VPC access for the Lambda function? – Mark B Sep 20 '16 at 13:39
  • Yep, the problem was with VPC access. Thanks. Do you have any ideas why I have different behavior? – kharandziuk Sep 20 '16 at 13:54
  • 1
    When you are trying to access something that exists, but you don't have network access to (due to improper VPC configuration) you get a timeout. When you are trying to access something that doesn't exist, you get a "not found" error. – Mark B Sep 20 '16 at 14:02
  • @MarkB can I ask to check my other question http://stackoverflow.com/questions/39598158/aws-lambda-and-redis-client-why-i-cant-call-callback ? – kharandziuk Sep 20 '16 at 15:45

1 Answers1

6

You need to have Redis in same VPC as Lambda. Check your security group settings. And then if you have EC2 access, install redis-cli and try to connect Redis. If this get connected your Lambda Redis will also get connected. As said earlier, you need to have your lambda in same VPC. The following is boilerplate code demonstrating connecting to Lambda:

console.log('before client initialization')
const redisOptions = {
  host: 'xxxx.xxx.xxx.xxx.xxx.amazonaws.com',
  port: 6379,
}

var client =  redis.createClient(redisOptions);
console.log('after client initialization');

client.on('connect', function(result) {
  console.log('connected');
}
Joseph Combs
  • 909
  • 9
  • 12
Anup Tilak
  • 336
  • 3
  • 15
  • 22
    client.quit() is very important after executing any redis command. If you don't call quit(), your lambda will timeout the execution. – Anup Tilak May 04 '17 at 15:04