Venkatesh Voona
Venkatesh Voona

Reputation: 423

Unable to Invoke AWS Lambda Function in Amazon Connect Contact Flow

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);
  }
});

}

Upvotes: 4

Views: 3536

Answers (1)

Parrett Apps
Parrett Apps

Reputation: 589

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.

Upvotes: 4

Related Questions