4

I am trying to Integrate AWS Lambda function in Amazon Connect Contact flow. The AWS Lambda function is working fine and giving a response. While invoking the function in the Connect contact flow, it is returning error statement but I am unable to find out what is the error and where the error log is storing. enter image description here

I am trying to get the user's phone number to the Amazon Connect and then I would like to check whether the phone number already exists in the DynamoDB or not. For this, I am writing the lambda function and trying to invoke it from Amazon Connect

const AWS=require('aws-sdk');
const doClient=new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});

exports.handler = function(event, context, callback) {
var params={
    TableName:'testdata',
    Key: {
       Address: event.Details.ContactData.CustomerEndpoint.Address
    }   
};
 doClient.get(params,function(err,data){
  if(err)
  { 
    callback(err,null);
  }
  else
  {
    callback(null,data);
  }
});

}
Parrett Apps
  • 569
  • 1
  • 4
  • 14
Venkatesh Voona
  • 423
  • 8
  • 27

1 Answers1

3

First, you need to make sure permissions have been granted properly. From AWS CLI, issue the following command with the following edits.

  • Replace function "Lambda_Function_Name" with the actual name of your Lambda function.
  • Replace the source-account "111122223333" with your AWS account number
  • Replace the source-arn string with the arn string of your Amazon Connect instance.

    aws lambda add-permission --function-name function:Lambda_Function_Name --statement-id 1 --principal connect.amazonaws.com --action lambda:InvokeFunction --source-account 111122223333 --source-arn arn:aws:connect:us-east-1:111122223333:instance/444555a7-abcd-4567-a555-654327abc87

Once your permissions are setup correctly, Amazon Connect should be able to access Lambda. You must, however, ensure that your Lambda function returns a properly formatted response. The output returned from the function must be a flat object of key/value pairs, with values that include only alphanumeric, dash, and underscore characters. Nested and complex objects are not supported. The size of the returned data must be less than 32 Kb of UTF-8 data.

Even with logging enabled on your call flow, Amazon Connect doesn't provide very detailed information about why a Lambda function fails. I would recommend hard coding a simple response in your Lambda function such as the following node.js response to ensure your Lambda response format isn't causing your issue and then work from there.

callback(null, {test : "Here is a valid response"});

When you are using the "Invoke AWS Lambda function" step, you do not need to pass the phone number to Lambda as a separate parameter as your image shows. Amazon Connect already passes a JSON object to Lambda that contains that information. Below is a sample of what Amazon Connect sends to Lambda.

{
  "Details": {
    "ContactData": {
      "Attributes": {
        "Call_Center": "0"
      },
      "Channel": "VOICE",
      "ContactId": "",
      "CustomerEndpoint": {
        "Address": "+13215551212",
        "Type": "TELEPHONE_NUMBER"
      },
      "InitialContactId": "",
      "InitiationMethod": "INBOUND",
      "InstanceARN": "",
      "PreviousContactId": "",
      "Queue": null,
      "SystemEndpoint": {
        "Address": "+18005551212",
        "Type": "TELEPHONE_NUMBER"
      }
    }
  },
  "Name": "ContactFlowEvent"
}

You can use the following in your Lambda function to reference the calling number to lookup in your DynamoDB.

var CallingNumber = event.Details.ContactData.CustomerEndpoint.Address;

Hope this helps.

Parrett Apps
  • 569
  • 1
  • 4
  • 14
  • I gave all the permissions to the lamda function and executed all those 3 commands Through AWS CLI. When I am giving the sample response from lamda to connect I can able to get that in the connect. But I am facing issues while giving some input from connect to lamda for getting the data from the DynamoDB – Venkatesh Voona Feb 03 '18 at 14:59
  • Can you share some screenshots or sample code? What data are you passing from Connect to Lambda? What data are you collecting from DynamoDB? What are you returning from Lambda to Connect? And what exactly doesn't work? – Parrett Apps Feb 03 '18 at 15:07
  • I edited my question with required screenshots, please do have a look at it – Venkatesh Voona Feb 05 '18 at 11:47
  • I updated my answer to include additional information of how to collect calling number from within your Lambda function. If you are having issues with Lambda returning data to Amazon Connect, you will need to share the code you are using in your Lambda function. – Parrett Apps Feb 05 '18 at 17:35
  • I have changed the code in Lambda function accordingly and the response is getting when I test the Lambda function. But still the same issue while invoking it from Connect – Venkatesh Voona Feb 05 '18 at 17:51
  • I would need to see your Lambda code to help any further. – Parrett Apps Feb 05 '18 at 18:32
  • Your callback is sending data back to Amazon Connect in the exact format it received it from Dynamo which won't likely be a flat object that Amazon Connect expects. Add "console.log("GetDBItem succeeded:", JSON.stringify(data, null, 2));" to your Lambda function and then share the execution results. Also, let me know what you want returned to Amazon Connect. I think you are getting close. – Parrett Apps Feb 05 '18 at 19:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/164576/discussion-between-venkatesh-voona-and-parrett-apps). – Venkatesh Voona Feb 05 '18 at 19:18
  • @ParrettApps This solved my issue of Connect sending over a null object to my Lambda function, but can you please address the issue of sending a custom parameter along with it? – CSharpMinor Mar 28 '23 at 02:25