1

I got "Internal server error" Status: 502 on AWS API Gateway. Searched solutions this post says need to handle callback with a callback function. Would anyone please advise how to incorporate the callback codes with my AWS-Lambda function below? Thank you so much.

message: "Internal server error" when try to access aws gateway api

callback(null, {
    statusCode: 200,
    body: JSON.stringify(message),
    headers: {'Content-Type': 'application/json'}
});

My AWS-Lambda function

'use strict';
const AWS = require('aws-sdk');

exports.handler = async (event, context) => {
    const documentClient = new AWS.DynamoDB.documentClient();

    let responseBody = "";
    let statusCode = 0;

    const {
        id,
        productname
    } = JSON.parse(event.body);

    const params = {
        TableName: "Products",
        Item: {
            id: id,
            productname: productname
        }
    };

    try {
        const data = await documentClient.put(params).promise();
        responseBody = JSON.stringify(data);
        statusCode = 201;
    } catch (err) {
        responseBody = `Unable to put product: ${err}`;
        statusCode = 403;
    }

    const response = {
        statusCode: statusCode,
        headers: {
            "Content-Type": "application/json"
        },
        body: responseBody
    };
    return response
};

0 Answers0