1

I am trying to add an Item to DynamoDB upon a post request from API Gateway using Lambda.

This is what my Lambda code looks like:

var AWS = require('aws-sdk');
var dynamoDB = new AWS.DynamoDB();

exports.handler = (event, context, callback) => {
var temp_id = "1";
var temp_ts = Date.now().toString();
var temp_calc = event['params']['calc'];

var params = {
    TableName:"calc-store",
    Item: {
        Id: {
            S: temp_id
        },
        timestamp: {
            S: temp_ts
        },
        calc: {
            S: temp_calc
        }
    }
};
dynamoDB.putItem(params,callback);


const response = {
    statusCode: 200,
    headers: {
        'content-type': 'application/json',
        'Access-Control-Allow-Origin': '*' 
    },
    body: event['params']['calc']
};

callback(null, response); 
};

This is how I am calling the function from my client

axios.post(apiURL, {params:{calc:calc}})
.then ((res) => {
  console.log(res);
})

I have enabled CORS over 30 times on my API Gateway, and I've also double checked by adding headers to the response. But no matter what I do, I keep getting a CORS error and for some reason, in my response, I can see that the "Access-Control-Allow-Origin" header is not being appended.

POST https://egezysuta5.execute-api.us-east-1.amazonaws.com/TEST 502
localhost/:1 Failed to load https://egezysuta5.execute-api.us-east- 
1.amazonaws.com/TEST: No 'Access-Control-Allow-Origin' header is 
present on the requested resource. Origin 'http://localhost:3000' is 
therefore not 
allowed access. The response had HTTP status code 502.
createError.js:17 Uncaught (in promise) Error: Network Error
at createError (createError.js:17)
at XMLHttpRequest.handleError (xhr.js:87)

I tried not using Lambda Proxy Integration, and it worked then, however, I was unable to access the params I passed.

EDIT: After spending hours on this, here is what I've boiled the problem down to. My client is making a successful pre-flight request to OPTIONS. The OPTIONS is successfully returning the correct CORS headers, but for some reason, these are not being passed to my POST request!

EDIT2: (This does not solve the problem) If I change the response body to a string there is no error!! There is something wrong with

event['params]['calc']
sideshowbarker
  • 72,859
  • 23
  • 167
  • 174
VBoi
  • 189
  • 2
  • 15
  • Similar to the post here, except I am getting a 502 error: https://stackoverflow.com/questions/38987256/aws-api-gateway-cors-post-not-working – VBoi Oct 05 '18 at 19:30
  • 1
    Ignore the cors issue this is probably your Lambda bugging out. – Mrk Fldig Oct 06 '18 at 11:41
  • I can't ignore an issue as it's critical to the functioning of my application – VBoi Oct 06 '18 at 20:02
  • No what I mean is the cors will work correctly when your function stops bugging out, it's a red herring you see - the cors headers aren't getting sent because the function never gets there. – Mrk Fldig Oct 07 '18 at 10:18

1 Answers1

0

Your problem is with the flow of the code. Basically you're not waiting for putItem to complete before callback gets executed...Try this...

dynamoDB.putItem(params,(err,data) => {
if(err){

  return callback(err, null)

}

const response = {
    statusCode: 200,
    headers: {
        'content-type': 'application/json',
        'Access-Control-Allow-Origin': '*' 
    },

    body: JSON.parse(event.body).calc
};

return callback(null, response); 

});
Mrk Fldig
  • 3,722
  • 5
  • 28
  • 58