Getting 500 internal server Errors, when I publish JSON payload using HTTP post in AWS API gateway

21 Views Asked by At

I am experiencing a problem while trying to publish a JSON Payload to IoT Core. I am getting a 500 internal server error. API Gateway appears to be configured correctly since it is reaching the Lambda function. However, the Lambda function is encountering an error and throwing a 500 internal server error message. The payload is being sent using HTTP POST in AWS API Gateway. When using AWS Lambda build-in testing tools, no errors occur. However, when using HTTP post with Postman, errors are encountered.

Here is a Lambda function I am using.

import AWS from 'aws-sdk';

const iotData = new AWS.IotData({ endpoint: 'IoTendpoint' });

export const handler = async (event) => {
  const params = {
    topic: event.topic,
    payload: JSON.stringify(event.message),
    qos: 0
  };

  try {
    await iotData.publish(params).promise();
    return { statusCode: 200, body: 'Message published' };
  } catch (error) {
    console.error('Publish error:', error);
    return { statusCode: 500, body: 'Failed to publish message' };
  }
};

Here is the Payload I am trying to publish.

{
  "topic": "gateway/test",
  "message": {
    "Temp": "60",
    "battery": "42"
  }
}

and this is a Log I got from Cloudwatch ERROR Publish error: MissingRequiredParameter: Missing required key 'topic' in params.

1

There are 1 best solutions below

0
adam blaydes On

Swap the response body with the err event and the statusCode as the err.code and try again. This will provide you with a better understanding of the stack trace as it can be difficult viewing/understanding errors that are running on AWS. If it’s not evident what the issue is by the actual error, update your question with the returned error event.