Connect to Sync Gateway REST API via AWS Lambda

Hi Team,

I am trying to connect to sync gateway via aws lambda over port 4984.

So, what I’m doing is trying to login to sync gateway using my credentials and after that trying to create a document.

However, when trying to login to sync gateway via lambda it gives me an error saying “Login Required”. And if I try login with the same credentials via sync gateway on the website it doesn’t give me this error and I can create documents after that where we are using pouchDB.

Below is my code that I’m trying to run via lambda :

So, when a request is sent via lambda using the request module from nodejs I’m receiving the above error.

exports.handler = function(event, context) {
    user.findByEmail(event.email, function(err,curUsers) {
        if(err){
            context.fail('Error while creating group: ' + err);
        }
        if(curUsers){
            if(event.groupDetails != undefined) {
                **request**({
                    method: 'POST',
                    uri: "https://example.com:4984/sync_gateway/",
                    body: JSON.stringify({name: event.email, password:event.password})
                }, function(error, response, body) {
                    var data = JSON.parse(body);
                    if(error){
                        return res.send(response);
                    }
                    request({
                        headers: {contentType: 'application/json'},
                        method: 'PUT',
                        uri: "https://example.com:4984/sync_gateway/"+event.groupDetails.group_name,
                        body: JSON.stringify({groupDetails: event.groupDetails})
                    }, function(error, response, body) {
                        context.succeed({
                            success: true,
                            data: body
                        });
                    });
                });
            } else {
                groupCreate(event.group_name, curUsers[0], [], function(error, callback) {
                    if(error) {
                        context.fail('Error while creating group: ' + error);
                    } else {
                        context.succeed({
                            success: true,
                            data: callback
                        });
                    }
                });
            }
        }
    });
};

Please let me know how to proceed further.

Thanks,

Ajinkya.S.Udgirkar

I don’t think any of us here have experience with AWS Lambda. Can you isolate the problem to something that isn’t specific to Lambda? If you could capture the login request being sent to SG from Lambda, that would help.