AWS node.js lambda request dynamodb but no response (no err, no return data)
Asked Answered
B

1

6

I am playing the aws mobilehub with react-native and I was hoping that it can speed up the backend hosting for me.

However, I cannot get its backend API working. After a long run with their docs, I pin down the problem between its lambda function and dynamodb service.

Any thoughts are greatly appreciated!

Problem#1

As the titled says: my aws lambda functions can request its dynamodb but has no response. What went wrong here? Or how can I get debug info from AWS dynamodb? (I gg and enabled Cloudtrial but it doesn't seem to have operation logs of the dynamodb too.)

Lambda side

Here I have the simplest node.js 6.10 codes:

const AWS = require('aws-sdk');
AWS.config.update({region: 'us-east-2'});
const dynamodb = new AWS.DynamoDB.DocumentClient();

exports.handler = function(event, context, callback) {
    var responseCode = 200;
    var requestBody, httpMethod, res;
    console.log("request: " + JSON.stringify(event));

    // Request Body
    requestBody = event.body;

    /*testing dynamodb with put*/

    console.log("PUT begins");
    let putItemParams2 = {
        TableName: "xxxx-mobilehub-xxxx-Test",//tableName
        Item: {businessId:'putItemParams2r3',test:'yooo', hmm:'hhhh'}
    };
    console.log("putItemParams2: ",putItemParams2);
    dynamodb.put(putItemParams2, (err, data) => {
        console.log("putItemParams2");
        if (err) console.log("dynamodb err: ",err, err.stack); // an error occurred
        else     console.log("dynamodb data: ",data);           // successful response
    });
    console.log("PUT end");

    var response = {
        statusCode: responseCode
        //....
    };

    ...
    //comment out context.succeed here to avoid program termination before intended. 
    //console.log("response: " + JSON.stringify(response))
    //context.succeed(response);
};

Logs

When the previouse codes are triggered, from AWS CloudWatch I can see logs:

START RequestId: 3d7c5f7f-1b98-11e8-ad00-93a6d10c8f4e Version: $LATEST
[timestamp] PUT begins
[timestamp] putItemParams2: { TableName: 'xxx-mobilehub-xxxx-Test',
Item: { businessId: 'putItemParams2r3', test: 'yooo', hmm: 'hhhh'}}
[timestamp] put end
END RequestId: 3d7c5f7f-1b98-11e8-ad00-93a6d10c8f4e

So no err, no data, no response. I checked my dynamodb and there is nothing insert.

Extra info

condition#1: this dynamodb table has public access since I want to rule out the auth problem.

condition#2: I ensure that my lambda function has access to these tables. e.g. arn:aws:dynamodb::xxxx:table/xxxx-mobilehub-xxxx- allow everything

condition#3: I build myself a simple node.js to execute the (aws-sdk)and this server works perfectly fine with the same code.. I am able to "get" &"put" items int & out from my dynamodb table.

Problem#2

my react-native code use 'aws-amplify-react-native'. Which the API.put is fine and the lambda function is at least receiving the api call (from problem#1).

However, API.get returns me 403 error, and the lambda function doesn't even has log for this operation..

async function getBusiness(){
    const path = "/Test";
    const api = "TestCRUD";
    let queryGetBusiness = {body: {userId: "hmmm"}};

    try {
        let apiResponse = await API.get(api, path, queryGetBusiness)//.then((data)=>{console.log(data)});
        let apiResponseJson = await JSON.stringify(apiResponse);
        console.log("response from saving Business: " + apiResponseJson);
    }
    catch (e) {console.log(e);}
} 

P.S.(AWS could do much better with this mobilehub.. their documentation is lacking details and awsmobile cloud-api invoke has some problems I guess.)

Bilbao answered 27/2, 2018 at 11:40 Comment(0)
J
1
const AWS = require('aws-sdk');
AWS.config.update({ region: 'us-east-2' });
const dynamodb = new AWS.DynamoDB.DocumentClient();

exports.handler = function (event, context, callback) {
    var responseCode = 200;
    var requestBody, httpMethod, res;
    console.log("request: " + JSON.stringify(event));

    // Request Body
    requestBody = event.body;

    /*testing dynamodb with put*/
    console.log("PUT begins");
    let putItemParams2 = {
        TableName: "xxxx-mobilehub-xxxx-Test",//tableName
        Item: { businessId: 'putItemParams2r3', test: 'yooo', hmm: 'hhhh' }
    };
    console.log("putItemParams2: ", putItemParams2);
    dynamodb.put(putItemParams2, (err, data) => {
        console.log("putItemParams2");
        if (err) console.log("dynamodb err: ", err, err.stack); // an error occurred
        else {
            console.log("dynamodb data: ", data);
            context.succeed(response);
        }
        // Call these here
        console.log("PUT end");
    });

    //console.log("response: " + JSON.stringify(response))
};

Make sure you call context.succeed inside the callback function. Like above.

You can also just use the third argument to handler function - callback instead of context.succeed like callback(null, response);

Jahncke answered 27/2, 2018 at 11:54 Comment(3)
Thx for the reply! I tried your suggestion by adding context.succeed(data); inside the dynamodb.put() 's callback function, but problem remains. Maybe I'm missing something.. Why would you think this should help though?Bilbao
403 would mean Forbidden. So, something must be off with your credentials. Are you sure the read and write are public?Jahncke
yeah, that’s what I thought at first.. I ensured the lambda function has access to use get&query&put& of the table. And I changed the table to public through mobilehub too.. And it show 403 to API.get() but not API.put(), so weird..Bilbao

© 2022 - 2024 — McMap. All rights reserved.