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:
- Log the request and initiate the function
- Process the request and receive the data
- 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.
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.
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
For this walkthrough, you should have the following prerequisites:
- An AWS account
- An Amazon Connect instance
- Basic understanding of AWS CloudFormation, Amazon Connect, Amazon DynamoDB, and AWS Lambda
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
- Download the resource pack
- 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
- Log in to the AWS Management Console
- Open the AWS CloudFormation console
- Make sure that you have the console open to the same Region as your Amazon Connect instance
- In the CloudFormation console, choose Create stack
- On the Create stack screen, leave the Prerequisite set to Template is ready
- In the Specify template section, select Upload a template file, then select the Choose file button
- Navigate to the ‘Resources’ folder that you created in step 2, open the CloudFormation folder inside, and select async.yaml
- Once the template loads, choose Next
- Provide a name for the stack
- 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
- Choose Next
- Apply any Tags as desired. Leave the rest of the options to their defaults and choose Next.
- Review the configuration. At the bottom of the page, select the check box to acknowledge that IAM resources may be created.
- Choose Create stack. This will launch the CloudFormation template and create the resources needed. Creation should only take a couple minutes.
- Once the stack is created, the status will change to CREATE_COMPLETE
- Select the Outputs tab
- Note the two ARN values provided. These are used during contact flow configuration.
Give Amazon Connect access to the new Lambda functions
- Log in to the AWS Management Console
- Open the Amazon Connect console
- Choose the name of the instance from the Instance Alias column.
- In the navigation pane, choose Contact flows.
- For AWS Lambda, select AsyncWriteTrackerSample from Function and choose Add Lambda Function. Confirm that the ARN of the function is added under Lambda functions.
- 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
- Log in to your contact center using your access URL.
- In the navigation pane, choose Routing, Contact flows.
- Choose Create contact flow. This opens the contact flow designer and creates an inbound contact flow (Type = Contact flow).
- Select the down arrow next to the greyed out Save button and choose Import flow (beta)
- Choose Select
- Navigate to the resources folder and select the ‘Async Sample’ file in the ContactFlows folder, then choose Open
- Choose Import
- Once the flow imports, edit the third object in the flow, Set contact attributes, by double-clicking the title
- 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
- 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
- Save the Set contact attributes block
- Publish the contact flow
- In the navigation pane, choose Routing, Phone Numbers.
- Choose an existing phone number or claim a new one.
- For the Contact flow / IVR value, choose the newly imported contact flow, named ‘Async Sample’
- Note the phone number and select Save.
Validate the solution
- Wait roughly two minutes for the contact flow to publish and the phone number to reassign to the new flow.
- Dial the phone number that you configured
- 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:
#Fake response with delay
########## End Your Async Code ##########
Simply replace the contents with the code you need to execute.
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.
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.