AWS Contact Center

Use asynchronous AWS 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

This 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 CoudFormation template
  • Deploy the CloudFormation template
  • Configure a phone number
  • Complete a test call

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 CloudFormation template.
  2. Log in to the AWS Management Console.
  3. Open the AWS CloudFormation console.
  4. Make sure that you have the console open to the same Region as your Amazon Connect instance.
  5. In the CloudFormation console, choose Create stack.
  6. On the Create stack screen, leave the Prerequisite set to Template is ready.
  7. In the Specify template section, select Upload a template file, then select the Choose file button.
  8. Navigate to theasync-lambda-amazon-connect.yaml file that you downloaded in step 1.
  9. Once the template loads, choose Next.
  10. Provide a name for the stack.
  11. In the Parameters section, complete the values as follows:

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

Assign the newly created contact flow to a phone number

  1. Log in to your contact center using your access URL.
  2. In the navigation pane, choose Channels, Phone Numbers.
  3. Choose an existing phone number or claim a new one.
  4. For the Contact flow / IVR value, choose the newly imported contact flow, named ‘AsyncSampleFlow’.
  5. Note the phone number and select Save.

Validate the solution

  1. Wait a moment 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.
  4. When you hear the “Thank you for waiting…” prompt, the asynchronous flow has completed.

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 Contact Flow, 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.