2

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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, I tried modifying, but the results are same.
Try adding some log messages and look for the results in CloudWatch to see what happens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.