title: AWS lambda function failing in Amazon Connect
body: <p>I have written an AWS lambda function in node.js to send an email, which is invoked in an Amazon Connect contact flow.  In the error branch it plays a prompt saying "lambda function failed."  I verified the IAM role has permission to send email with SES, the sender/receiver emails are verified in SES, and also the lambda function has permissions for Amazon Connect.  </p>
  
  <p>The email does actually get sent out, but oddly I still hear the prompt "lambda function failed."  Here is the code:</p>
  
  <pre><code>"use strict";
  
  const aws = require('aws-sdk');
  
  const sender = "Sender Name &lt;sender@email.com&gt;";
  const recipient = "recipient@email.com";
  const subject = "ALERT:  no agents are logged in";
  const body_text = "There are no agents logged in";
  
  const body_html = 
  `&lt;html&gt;
  &lt;head&gt;&lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;ALERT&lt;/h1&gt;
    &lt;p&gt;Ther are no agents logged in to take calls in the queue.&lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;`;
  
  const charset = "UTF-8";
  
  let params = { 
    Source: sender, 
    Destination: { 
      ToAddresses: [
        recipient 
      ],
    },
    Message: {
      Subject: {
        Data: subject,
        Charset: charset
      },
      Body: {
        Text: {
          Data: body_text,
          Charset: charset 
        },
        Html: {
          Data: body_html,
          Charset: charset
        }
      }
    },
  };
  
  const ses = new aws.SES({apiVersion: '2010-12-01'});
  
  
  exports.handler = function(event, context, callback) {
    ses.sendEmail(params, function(err, data) {
  
      if(err) {
        console.log("fail");
        callback(err, err.message);
      } 
      else {
        console.log("success");
        callback(null);
      }
    });
  };
  </code></pre>
  
  <p>I checked the cloudwatch logs and don't see any error:</p>
  
  <pre><code>00:17:24
  START RequestId: 17f1e239-990e-11e8-96bb-a1980f44db91 Version: $LATEST
  00:17:24
  2018-08-06T00:17:24.723Z 17f1e239-990e-11e8-96bb-a1980f44db91 success
  00:17:24
  END RequestId: 17f1e239-990e-11e8-96bb-a1980f44db91
  00:17:24
  REPORT RequestId: 17f1e239-990e-11e8-96bb-a1980f44db91 Duration: 226.51 ms Billed Duration: 300 ms Memory Size: 128 MB Max Memory Used: 32 MB 
  </code></pre>
  
  <p>How do I troubleshoot this?</p>
  
  <p>EDIT:</p>
  
  <p>I enabled contact flow logs.  In CloudWatch I noticed this:</p>
  
  <pre><code>{ 
  "Parameters": { "FunctionArn": "arn:aws:lambda:us-west-2:769182588423:function:noAgentEmail", "TimeLimit": "8000" }, 
  "Timestamp": "2018-08-06T06:36:31.786Z", 
  "ContactFlowModuleType": "InvokeExternalResource", 
  "Results": "The Lambda Function Returned An Error.", 
  "ExternalResults": { "forceClose": "false" }, 
  "ContactId": "458027b0-d895-439e-bc06-114500dce64a", 
  "ContactFlowId": "arn:aws:connect:us-west-2:769182588423:instance/1e2ddedd-8335-42fe-89de-1e986fc016ef/contact-flow/2329af39-682c-4dc8-b3a2-5e7fe64de5d2" 
  }
  </code></pre>
  
  <p>What's confusing is it indicates the lambda function returned something:</p>
  
  <pre><code> "ExternalResults": { "forceClose": "false" }
  </code></pre>
  
  <p>But this is clearly not the case given the code.  What's going on?</p>