One of Amazon Connect’s strengths is the ability to use Amazon Lambda in contact flows to access other AWS services and external systems. Some common use cases are looking up accounts in an external CRM, storing contact data in Amazon DynamoDB or sending emails using Amazon SES or Amazon SNS.
To keep contact flows responsive, Amazon limits any Lambda function’s execution time to 8 seconds after which it will time out. Even 8 seconds would be a long time for the caller to wait on the line for a data dip to complete. So, Lambdas in a contact flow should be designed to return as quickly as possible.
Lambda functions in contact flows execute just like other Lambdas in AWS. They execute within an isolated environment (container) that is stood up when the Lambda is invoked. AWS manages containers for you, and does its best to re-use existing containers. When it cannot, creating a new container can take a little bit of time. Which often isn’t a big deal, but maybe enough to get past the 8 second timeout in a contact flow and cause the invocation to fail.
One approach to handling such a failure is to add retry logic to your contact flow and try again. As a general rule of thumb, you should design your contact flows such that one Lambda failure won’t sink the call.
Along with robust contact flows, you can use Amazon CloudWatch Events to periodically invoke your Lambdas outside of a contact flow. This ensures that the Lambda function stays “warm” with a ready container and is less likely to timeout. This technique is widely used for Lambda in other contexts and works well for Amazon Connect. You can set up these Events from the CloudWatch Management Console or from the Amazon Command Line Interface (CLI).
CloudWatch Management Console
We’re going to create a single CloudWatch Event rule that triggers every Lambda “target” once every 60 minutes. From the CloudWatch Management Console, we navigate to the Events tab and click on “Create rule”.
Avoid Contact Center Outages: Plan Your Upgrade to Amazon Connect
Learn the six most common pitfalls when upgrading your contact center, and how Amazon Connect can help you avoid them.
In the first step of the wizard, we select “Schedule” and a “fixed rate of” 60 minutes. In the Targets area we use the “Function” drop down to select a Lambda and then the “Add target” button to add additional Lambda functions.
In this example, I’ve selected two Lambda functions: createCustomizedGreeting and getCustomerNameFromIncomingNumber.
In the next step, we name the new rule “KeepContactFlowLambdasWarm” and add a description of “Periodically trigger Lambda functions used in Amazon Connect contact flows so that they stay responsive”.
After we click “Create rule”, we are all set. Every hour our Lambda functions will be triggered. In the future if we add more Lambda functions to our contact flows, we can go back to the CloudWatch Management Console and edit the rule to add more targets.
If you prefer to script this rule and adding the targets, Amazon has provided a nice tutorial at: https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/RunLambdaSchedule.html that shows you the relevant commands and syntax. I don’t have much to add here, other than to suggest you also look into command options like list-targets-by-rule to give you a handy way to see which Lambda functions will be triggered:
A Warning Before We Go
When a Lambda is invoked by CloudWatch, it is a real invocation. Your code runs. The cost to your AWS subscription is minimal, but make sure your Lambda function can be executed like this without doing expensive or inappropriate reads or uses of business data or processes. Within CloudWatch you can customize the event data that is passed into the Lambda, giving you more granular control on what happens during that execution.
Thanks for reading. Any questions, comments or corrections are greatly appreciated. To learn more about what we can do with Amazon Connect, check out Helping You Get the Most Out of Amazon Connect