1
votes

I am trying to setup an api gateway to simply send and email via lambda and was ses.

var aws = require('aws-sdk');
var ses = new aws.SES({
   region: 'us-east-1'
});
const querystring = require('querystring');
const inspect = require('util').inspect;


exports.handler = function(event, context) {
    var output = querystring.parse(event);
    console.log("Incoming: ", output["email"]);
    email=output["email"]

    console.log("Incoming: ", email);
    var eParams = {
        Destination: {
            ToAddresses: [email]
        },
        Message: {
            Body: {
                Text: {
                    Data: "some trial text"
                }
            },
            Subject: {
                Data: "Email Subject!!!"
            }
        },
        Source: "[email protected]"
    };

    console.log('===SENDING EMAIL===');
    var email = ses.sendEmail(eParams, function(err, data){
        if(err) console.log(err);
        else {
            console.log("===EMAIL SENT===");
            console.log(data);


            console.log("EMAIL CODE END");
            console.log('EMAIL: ', email);
            context.succeed(event);

        }
    });

};

Lambda code looks like this. How do I get the event data passed on from api?

The console log looks like this:

14:00:57 START RequestId: f07a7595-3034-11e8-81c7-d3dcc4528819 Version: $LATEST START RequestId: f07a7595-3034-11e8-81c7-d3dcc4528819 Version: $LATEST  14:00:57 2018-03-25T14:00:57.195Z f07a7595-3034-11e8-81c7-d3dcc4528819 Incoming: undefined 2018-03-25T14:00:57.195Z f07a7595-3034-11e8-81c7-d3dcc4528819 Incoming: undefined  14:00:57 2018-03-25T14:00:57.232Z f07a7595-3034-11e8-81c7-d3dcc4528819 Incoming: undefined 2018-03-25T14:00:57.232Z f07a7595-3034-11e8-81c7-d3dcc4528819 Incoming: undefined  14:00:57 2018-03-25T14:00:57.232Z f07a7595-3034-11e8-81c7-d3dcc4528819 ===SENDING EMAIL=== 2018-03-25T14:00:57.232Z f07a7595-3034-11e8-81c7-d3dcc4528819 ===SENDING EMAIL===  14:00:58 2018-03-25T14:00:58.095Z f07a7595-3034-11e8-81c7-d3dcc4528819 { [ValidationError: 1 validation error detected: Value null at 'destination' failed to satisfy constraint: Member must not be null] message: '1 validation error detected: Value null at \'destination\' failed to satisfy constraint: Member must not be null', code: 'ValidationError', time: Sun Mar 25 2018 14:00:58 GMT+0000 (UTC), r 2018-03-25T14:00:58.095Z f07a7595-3034-11e8-81c7-d3dcc4528819 { [ValidationError: 1 validation error detected: Value null at 'destination' failed to satisfy constraint: Member must not be null] message: '1 validation error detected: Value null at \'destination\' failed to satisfy constraint: Member must not be null', code: 'ValidationError', time: Sun Mar 25 2018 14:00:58 GMT+0000 (UTC), requestId: 'f14b32f1-3034-11e8-897b-633b17ff4491', statusCode: 400, retryable: false, retryDelay: 54.40075590740889 }  14:00:58 END RequestId: f07a7595-3034-11e8-81c7-d3dcc4528819 END RequestId: f07a7595-3034-11e8-81c7-d3dcc4528819  14:00:58 REPORT RequestId: f07a7595-3034-11e8-81c7-d3dcc4528819 Duration: 1498.93 ms Billed Duration: 1500 ms Memory Size: 128 MB Max Memory Used: 37 MB

2

2 Answers

1
votes

Log the event and see how it is structured. Your function fails from the start as the email is undefined. In API Gateway, if you defined LAMBDA PROXY INTEGRATION, your event body will be structured like this:

{
  "message": "Good day, John of Seattle. Happy Friday!",
  "input": {
    "resource": "/{proxy+}",
    "path": "/Seattle",
    "httpMethod": "POST",
    "headers": {
      "day": "Friday"
    },
    "queryStringParameters": {
      "time": "morning"
    },
    "pathParameters": {
      "proxy": "Seattle"
    },
    "stageVariables": null,
    "requestContext": {
      "path": "/{proxy+}",
      "accountId": "123456789012",
      "resourceId": "nl9h80",
      "stage": "test-invoke-stage",
      "requestId": "test-invoke-request",
       ... 
      },
      "resourcePath": "/{proxy+}",
      "httpMethod": "POST",
      "apiId": "r275xc9bmd"
    },
    "body": "{ \"callerName\": \"John\" }",
    "isBase64Encoded": false
  }
}

You want to read the queryStringParameters Object.

0
votes

Ok. I figured out the problem. Needed to parse JSOn with JSON.parse and then it worked.