Friday, 5 May 2017

AWS Lambda, API Gateway returns Malformed Lambda proxy response, 502 error

After a lot of googling and a few hours of trial and error I can't find out why i'm getting this error message:

Execution failed due to configuration error: Malformed Lambda proxy response

When I run the lambda using the test lambda functionality it works fine. Answers to similar questions to this state that the response from the lambda needs to meet this format:

{
  "isBase64Encoded": true|false,
  "statusCode": httpStatusCode,
  "headers": { "headerName": "headerValue", ... },
  "body": "..."
}

Below is the lambda I'm using (which adheres to this format):

'use strict';

var aws = require('aws-sdk');
var ses = new aws.SES();

var RECEIVER = "XXXX@XXXX.com";
var SENDER = "XXXX@XXXX.com";

const response_success = {
      'isBase64Encoded': false,
      'statusCode': 200,
      'headers': {
          'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        message: 'ok'
      }),
};

const response_error = {
    'isBase64Encoded': false,
    'statusCode': 400,
    'headers': {
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        message: 'error'
    }),
};

exports.handler = (event, context, callback) => {
    sendEmail(event, function(error, data) {
        if (error) {
           callback(response_error);
        } else {
           callback(null, response_success);
        }
    });
};

function sendEmail(event, done) {
    var params = {
        Destination: {
            ToAddresses: [
                RECEIVER
            ]
        },
        Message: {
            Body: {
                Text: {
                    Data: event.message,
                    Charset: 'UTF-8'
                }
            },
            Subject: {
                Data: 'Lamda Test Email',
                Charset: 'UTF-8'
            }
        },
        Source: SENDER
    }
    ses.sendEmail(params, done);
}

I don't think its a configuration issue and i'm using Terraform to build the infrastructure if thats' any help. Thanks in advance!



via Reuben S

No comments:

Post a Comment