AWS Contact Center

Use asynchronous Lambda functions with Amazon Connect

Having access to data within an Interactive Voice Response (IVR) solution can help customers find the information they need and resolve their problems quickly. Unfortunately, data is not always easily accessible. Sometimes it resides in legacy systems that are slow and complex, and the last thing you want is to leave a customer waiting on the phone while your IVR tries to get the data it needs to continue. Instead, it would be a better customer experience to initiate a backend query, continue forward with the customer experience, and check back for results later. By using AWS Lambda, Amazon Connect can make this possible.

This post provides an example of how you can work with AWS Lambda functions asynchronously in an Amazon Connect contact flow. Typically, there is an eight-second timeout for Lambda to respond when executed from a contact flow. This is to prevent unusually long wait times for customers while they are on a voice call. In some situations, backend data sources or API calls may take longer than eight seconds to respond, which would cause the Lambda function to timeout. There are a number of ways to solve this problem, but effectively they all follow the same pattern:

  1. Log the request and initiate the function
  2. Process the request and receive the data
  3. Mark the request complete and provide the data

In this example, we will use AWS Lambda to execute the requests and Amazon DynamoDB to track their progress. You can certainly approach the solution with a wide array of services. However, the solution described ahead would be acceptable for many simple use cases and is easy to deploy.

Solution overview

In this example, we will use AWS Lambda to write a tracking record to Amazon DynamoDB. All items written to DynamoDB generate an event that is published via DynamoDB’s event stream. This event triggers a second Lambda function that retrieves the data from the tracker DB. The function also performs the necessary task associated with that data, and then writes the result back to the tracker, eventually updating the tracker DB to indicate that the request is complete. Meanwhile, Amazon Connect has continued processing the contact flow and returns to check the status of the request. Once a completed request is found, Amazon Connect retrieves the results and processes accordingly.

NOTE: In this sample, the processing function does not query an external resource or API. It simulates a long-running function by simply sleeping for a brief amount of time before returning a standard result.

Walkthrough

Most of this sample configuration can be deployed into your account with an AWS CloudFormation template. This template will create a CloudFormation stack consisting of an AWS Identity and Access Management (IAM) role, IAM policy, DynamoDB table, three Lambda functions, and a trigger then executes one of those Lambda functions when a new record is written to the DynamoDB table. The remaining configuration is done in Amazon Connect.

The high-level steps to configure are:

  • Download the resource pack for this solution
  • Deploy the CloudFormation template
  • Provide Amazon Connect access to the newly created Lambda functions
  • Import and modify Amazon Connect contact flows
  • Configure a phone number
  • Validate

Prerequisites

For this walkthrough, you should have the following prerequisites:

Step-by-step instructions

These instructions assume a general working knowledge of Amazon Connect and AWS CloudFormation. For details on how to perform basic administration tasks with either, please read:

Deploy the CloudFormation Template

  1. Download the resource pack
  2. Unzip the file to your local machine, this will create a ‘Resources’ folder, which contains
    • CloudFormation template
    • Code used in the Lambda functions that the template creates (for reference)
    • Amazon Connect contact flow
  3. Log in to the AWS Management Console
  4. Open the AWS CloudFormation console
  5. Make sure that you have the console open to the same Region as your Amazon Connect instance
  6. In the CloudFormation console, choose Create stack
  7. On the Create stack screen, leave the Prerequisite set to Template is ready
  8. In the Specify template section, select Upload a template file, then select the Choose file button
  9. Navigate to the ‘Resources’ folder that you created in step 2, open the CloudFormation folder inside, and select async.yaml
  10. Once the template loads, choose Next
  11. Provide a name for the stack
  12. In the Parameters section, complete the values as follows:

    • AWSRegion: choose the Region that your Amazon Connect instance is deployed to
    • TrackerDynamoTableName: provide a name for the table or accept the default provided
    • TrackerRecordTimeToLive: the amount of time, in seconds, before a tracker record expires
  13. Choose Next
  14. Apply any Tags as desired. Leave the rest of the options to their defaults and choose Next.
  15. Review the configuration. At the bottom of the page, select the check box to acknowledge that IAM resources may be created.
  16. Choose Create stack. This will launch the CloudFormation template and create the resources needed. Creation should only take a couple minutes.
  17. Once the stack is created, the status will change to CREATE_COMPLETE

  18. Select the Outputs tab
  19. Note the two ARN values provided. These are used during contact flow configuration.

Give Amazon Connect access to the new Lambda functions

  1. Log in to the AWS Management Console
  2. Open the Amazon Connect console
  3. Choose the name of the instance from the Instance Alias column.
  4. In the navigation pane, choose Contact flows.
  5. For AWS Lambda, select AsyncWriteTrackerSample from Function and choose Add Lambda Function. Confirm that the ARN of the function is added under Lambda functions.
  6. Repeat the previous step, this time selecting AsyncGetResultSample. Confirm that the ARN of the function is added under Lambda functions

Import the Amazon Connect contact flow

  1. Log in to your contact center using your access URL.
  2. In the navigation pane, choose Routing, Contact flows.
  3. Choose Create contact flow. This opens the contact flow designer and creates an inbound contact flow (Type = Contact flow).
  4. Select the down arrow next to the greyed out Save button and choose Import flow (beta)
  5. Choose Select
  6. Navigate to the resources folder and select the ‘Async Sample’ file in the ContactFlows folder, then choose Open
  7. Choose Import
  8. Once the flow imports, edit the third object in the flow, Set contact attributes, by double-clicking the title
  9. Replace the value for the write_tracker_function_arn attribute with the output value from your CloudFormation stack noted earlier. This would be the value for the key WriteTrackerLambdaFunctionARN
  10. Replace the value for the get_results_function_arn attribute with the output value from your CloudFormation stack noted earlier. This would be the value for the key GetResultsLambdaFunctionARN
  11. Save the Set contact attributes block
  12. Publish the contact flow
  13. In the navigation pane, choose Routing, Phone Numbers.
  14. Choose an existing phone number or claim a new one.
  15. For the Contact flow / IVR value, choose the newly imported contact flow, named ‘Async Sample’
  16. Note the phone number and select Save.

Validate the solution

  1. Wait roughly two minutes for the contact flow to publish and the phone number to reassign to the new flow.
  2. Dial the phone number that you configured
  3. Listen to the flow and confirm function. The flow explains the process as it moves through the flow

Adapt to your use case

This configuration can easily be adapted to your use case. In addition to modifying the contact flow to fit your needs, there are two main configuration points to edit: the parameters to pass when the initial tracker is created, and the code to execute during processing.

When you initially create the tracker by calling the AsyncWriteTrackerSample function, you can pass along whatever parameters you need to provide during the asynchronous call. These values are written to the tracker record. When the processing function executes, it extracts the parameters and makes them available for use in the asynchronous function. These parameters are set in the first Invoke AWS Lambda function block in the contact flow. The sample includes one parameter as an example.

Once you have modified the parameters, you will then must edit the ‘AsyncEventProcessorSample’ Lambda function to include your required code. This could be a specific code that you want to execute inline or an API call, etc. In the Lambda function, you will find the following section:

########## Start Your Async Code ##########

#You can reference your passed params like so:

print(async_data[‘contact_id’])

#Fake response with delay

async_data.update({‘payment_confirmation’:’OU’,’result’:’success’})

time.sleep(15)

########## End Your Async Code ##########

Simply replace the contents with the code you need to execute.

Cleaning up

To avoid incurring future charges, disassociate the contact flow from the phone number that you used. If you claimed a new phone number, you should also release it. The remaining resources are deleted simply by deleting the CloudFormation stack. This will remove the Lambda functions, DyanmoDB table, and the IAM resources used in this sample.

Conclusion

This blog demonstrated how you could leverage a tracking mechanism, such as Amazon DynamoDB in order to execute Lambda functions asynchronously within Amazon Connect. This allows customers to access data from slow responding backend systems, without keeping callers stuck in an IVR menu. There are a number of use cases that this can address such as accessing data from legacy systems, initiating step functions that run multiple processes, or calling on APIs that have a slow response rate.