Menu
Amazon Connect
Administrator Guide

Granting Amazon Connect Access to AWS Lambda Functions

Amazon Connect can interact with your own systems and take different paths in IVR dynamically. To achieve this, invoke Lambda functions, fetch results in an IVR, and call your own services or interact with other AWS data stores or services.

Environment variables indicate where to store output and logging settings, and specify the correct directory for files to be installed. By separating these settings from the application logic, you don't need to update your function code when you need to change the function behavior based on different settings.

Environment variables for Lambda functions enable you to pass settings dynamically to your function code and libraries, without making changes to your code. Environment variables are key-value pairs that you create and modify as part of your function configuration. Lambda makes these key-value pairs available to your Lambda function code.

Lambda Function Invocation from IVR

Amazon Connect can successfully invoke a Lambda function in an AWS account when a resource policy has been set on the Lambda function. For more information, see Using Resource-Based Policies for AWS Lambda in the AWS Lambda Developer Guide.

Use the following add-permission command to create a resource policy using this information:

Copy
aws lambda add-permission --function-name function:my-lambda-function --statement-id 1 \ --principal connect.amazonaws.com --action lambda:InvokeFunction --source-account 123456789012 \ --source-arn arn:aws:connect:us-east-1:123456789012:instance/def1a4fc-ac9d-11e6-b582-06a0be38cccf \

This command uses the following input:

  • The name of the Lambda function (for example, my-lambda-function)

  • The ARN of a Amazon Connect instance (for example, arn:aws:connect:us-east-1:123456789012:instance/def1a4fc-ac9d-11e6-b582-example)

  • The AWS account ID for the Lambda function (for example, 123456789012)

Lambda Interaction Model

On every Lambda function invocation from a contact flow, you pass a default set of information related to ongoing contact as well as parameters specified by the user during contact flow creation.

Request

When you invoke a Lambda function, the set of parameters passed can be seen in the model definition. Below is an example request JSON which will be present in the event input of the Lambda function.

Copy
"event" : { "Name":"ContactFlowExecution", "Details":{ "Parameters":{ /* These are parameters that are specified by a user while configuring the contact flow */ "key1":"value1", "key2":"value2" }, "ContactData":{ /* Unique contact Id */ "ContactId":"ASDAcxcasDFSSDFs", /* Optional: Original id is present in cases like transfer */ "OriginalContactId":"Acxsada-asdasdaxA", "PreviousContactId":"Acxsada-agdasdaxA", "Channel":"Voice", "InstanceARN": organization ARN "InitiationMethod":"Inbound/Outbound/Transfer/Callback", "SystemEndpoint":{ "Type":"TELEPHONE_NUMBER", /* phone number for the Amazon Connect instance */ "Address":"01234567", }, "CustomerEndpoint":{ "Type":"TELEPHONE_NUMBER", /* Customer's phone number */ "Address":"+12065555555" }, "Queue" : { /* Name of queue */ "Name":"PrimaryPhoneQueue", /* Arn of the queue. */ "ARN" : "", }, /* Map of all contact attributes. These are set using the Set contact attribute block in the contact flow. */ "Attributes":{ "key1":"value", "key2":"value" } } } }

The request is divided into three parts:

  • Contact data—This is always passed by Amazon Connect for every contact. Some parameters are optional.

  • User attributes—These are attributes saved in a contact flow based on previous Set attributes blocks in the contact flow. This map may be empty if there aren't any saved attributes.

  • Parameters—These are parameters specific to this call.

The Lambda function response should be a simple Map String String. This map can be up to 32k. If you fail to reach Lambda, the function throws an exception, the response is not understood, or the Lambda function takes more time than the limit, the contact flow jumps to the Error label. The following code is an example Python Lambda function:

Copy
def lambda_handler(event, context): resultMap = {"lambdaResult":"Success"}; return resultMap;

Sample NodeJS lambda function

Copy
exports.handler = (event, context, callback) => { console.log("Received event from Lily"); callback(null, buildResponse()); }; function buildResponse() { return { foo: "bar", lambdaResult:"Success" }; }

The Lambda key being returned in this example is State. The return from the Lambda function must be a flat object of key/value pairs (no nested or complex objects) where the values are numbers, strings, or booleans. The properties available to access using JsonPath is the full ContactData object sent to your Lambda function as well as an External object that holds all of the properties returned by the last Lambda invocation. For example, you can access $.External.key or $.CustomerEndpoint.Address. You can also use attributes returned via Lambda in certain blocks using the Use attributes option.

Contact Flow External Invocation

You can use the following definition for an external contact flow:

Copy
def lambda_handler(event, context): customerPhoneNumber = event.get('Details').get('ContactData').get('CustomerAddress').get('Value'); if authenticateCustomer(customerPhoneNumber) == "true": resultMap = {"isCustomerAuthenticated": "true" } return resultMap; else : resultMap = {"isCustomerAuthenticated": "false" } return resultMap; def authenticateCustomer(customerPhoneNumber) : #We hardcode list of authenticated numbers. In production, make an external web service call to figure out if a customer is authenticated. customer_list = ["+12065555555"]; if (customerPhoneNumber in customer_list) : return "true"; else : return "false";