Krisna
Krisna

Reputation: 3433

aws-serverless-lambda: "Internal server error"

I am new to server-less lambda function. I created one post request by using lambda. I deploy my app to AWS. I am testing my app by using Postman. I tired my post request, I got internal server error.. I also tested in api-gateway I got this errors:

Fri Mar 05 19:09:33 UTC 2021 : Endpoint response body before transformations: null
Fri Mar 05 19:09:33 UTC 2021 : Execution failed due to configuration error: Malformed Lambda proxy response
Fri Mar 05 19:09:33 UTC 2021 : Method completed with status: 502

I don't get what I did wrong. This is my yaml file post request event

  createBeers:
    handler: handlers/createBeers.createBeers
    events:
      - http:
          path: createBeers
          method: post
          cors: true

This is my lambda function

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

const dynamoDb = new AWS.DynamoDB.DocumentClient();
module.exports.createBeers = async event => {
  const requestBody = JSON.parse(event.body);
  const params = {
    TableName: "tablename",
    Item: {
      beer_name: requestBody.beer_name,
      beer_type: requestBody.beer_type,
      beer_img_url: requestBody.beer_img_url,
      beer_description: requestBody.beer_description,
      alcohol_per: requestBody.alcohol_per
    }
  };

  try {
    await dynamoDb.put(params).promise();
    return {
      statusCode: 200,
      body: JSON.stringify(requestBody),
    };
  } catch (error) {
    console.log(error);
  }
};

Upvotes: 1

Views: 1040

Answers (1)

Seth Geoghegan
Seth Geoghegan

Reputation: 5747

Malformed Lambda proxy response

This sounds like your lambda is not returning the correct response. A few troubleshooting suggestions:

  1. Print out the request body in your lambda to convince yourself that the lambda code is being executed and the event is as you expect.
  2. You are not returning a response if your lambda fails. Try returning an error in your catch block as well
  try {
    await dynamoDb.put(params).promise();
    return {
      statusCode: 200,
      body: JSON.stringify(requestBody),
    };
  } catch (error) {
    return {
      statusCode: 500,
      body: JSON.stringify(error),
    };
  }

Upvotes: 1

Related Questions