1

I'm writing a function to connect to a REDIS instance on REDISLABS. I have attached an API gateway to invoke this code. Strangely, while testing it is from API gateway test console, it is not working. Unfortunately, I cant see any error on test console.

I have moved the code to local machine as a normal NodeJS code and it works without any problem. My code looks like this:

exports.handler = async (event) => {
    var client = require('redis').createClient({
        host: 'redis-XXXXXXXX.c10.us-east-1-2.ec2.cloud.redislabs.com',
        password: 'XXXXXXXXXXXXX',
        port: '14021'
        });

    client.on('connect', () => {
        return {
            status: 200,
            message: 'connected'
        }
    });

    client.on('error', (error)=> {

        return {
            status: 404,
            message: 'Something went wrong:'+ error
        };

    })
};

Response from TEST Console

I have a VPC configured and a security group with all ports enabled(for testing) for outbound connection.

Can someone suggest where I'm going wrong?

Prasanth Pillai
  • 146
  • 1
  • 13

1 Answers1

1

You're trying to return the result from your callbacks instead of from the handler itself. I'm not sure how to do it with async, but without it you can do:

exports.handler = (event, context, callback) => {
    var client = require('redis').createClient({
        host: 'redis-XXXXXXXX.c10.us-east-1-2.ec2.cloud.redislabs.com',
        password: 'XXXXXXXXXXXXX',
        port: '14021'
    });

    client.on('connect', () => {
        callback(null, {
            status: 200,
            message: 'connected'
        });
    });

    client.on('error', (error)=> {
        callback(null, {
            status: 404,
            message: 'Something went wrong:'+ error
        });
    });
};

You should probably add more error handling in case an exception is thrown so you don't get an empty response in that case.

kichik
  • 33,220
  • 7
  • 94
  • 114