NOTE: Salesforce is in the process of enabling Amazon EventBridge and Amazon Simple Notification Service for Service Cloud Voice created accounts. Please validate service availability before proceeding with this configuration.
When working with Lambda functions in a voice interaction, fast response times are critical. With AWS Lambda, if a function has not been used in a while, the resources allocated to it can be redistributed elsewhere. This can cause a delay the next time the function is executed. To prevent this, you can use Amazon EventBridge to periodically call the function, keeping it ready to execute quickly. In this example, we will show you how to modify the existing Salesforce-provided Lambda functions for Service Cloud voice to properly handle Amazon EventBridge events. This modification is NOT NECESSARY for EventBridge to work and to keep your function active, however EventBridge can generate errors as it sends events to your functions that they are not designed to respond to. Since the modification is simple, it is reasonable to just adapt the code to handle the EventBridge events properly and eliminate those errors.
There are three Salesforce-provided Lambda functions that would benefit most from this modification. They are:
- InvokeSalesforceRestApiFunction (handler.js)
- InvokeTelephonyIntegrationApiFunction (handler.js)
- kvsConsumerTrigger (kvs_trigger.js)
NOTE: You can set all three functions as targets for one EventBridge rule.
In this example, we will show you how to modify the InvokeTelephonyIntegrationApiFunction function. This modification simply looks at the incoming event and determines if it is an EventBridge event. If so, it responds accordingly. If not, it continues to evaluate the existing function code. Since the InvokeTelephonyIntegrationApiFunction function is written for Node.js, we will use the following code:
// BEGIN AWS modification for EventBridge
let eventSource = event.source || 'undefined';
if (eventSource == 'aws.events') {
result = {'eventStatusCode': 200,'eventResponse' : 'warm', 'eventType' : 'EventBridge'};
console.log(result)
return result
}
// END AWS modification for EventBridge
Essentially, we set the value for eventSource
to the incoming event value of source, if it exists. If this is an EventBridge event, the source will be aws.events
. Once set, we check the value of eventSource. If it is an EventBridge event, we configure a response, log the response, and return the response. We're using the result variable since that is what the Lambda function is configured to use under normal operation. That's it! We insert this code into the function just after the main result variable declaration and it will be ready to respond accordingly. For other functions in other languages, a similar approach can be taken. for example, if the function was in Python, we could do something like:
# BEGIN AWS modification for EventBridge
response = {}
if 'source' in event:
event_source = event['source']
if event_source == 'aws.events':
response.update({'event_status_code': 200,'event_response' : 'warm', 'event_type' : 'EventBridge'})
print(response)
return response
# END AWS modification for EventBridge
Additionally, you could add logic to handle exceptions, other use cases, etc, but fundamentally we just need something that sees the EventBridge event and replies so that we don't continue processing the function.
This example will require the following modification
- Update the Lambda function with the appropriate code
This example will require the following new configuration
- Amazon EventBridge Rule
- Operational Salesforce Service Cloud Voice configuration
-
Login to the AWS Console
-
Make sure you are in the same region as your Amazon Connect instance. You can set the region by expanding the region selector in the upper right and choosing the region
-
Navigate to the AWS Lambda console
-
Open your existing InvokeTelephonyIntegrationApiFunction function
-
Once the function loads, select Action and choose Export function
-
In the Export your function window, select Download deployment package
-
A zip file of your function will be downloaded to your local computer
-
Extract the ZIP folder contents
-
In the newly extracted folder, open the handler.js file in your preferred editor
-
Add a new line after line 9 so that you are just below
let result = {};
-
Paste the javascript code:
// BEGIN AWS modification for EventBridge
let eventSource = event.source || 'undefined';
if (eventSource == 'aws.events') {
result = {'eventStatusCode': 200,'eventResponse' : 'warm', 'eventType' : 'EventBridge'};
console.log(result)
return result
}
// END AWS modification for EventBridge
-
Save the handler.js file
-
Rezip the entire folder
-
Return to the AWS Lambda console
-
Make sure that you are still on the same function, if not, re-open the InvokeTelephonyIntegrationApiFunction function
-
Scroll to the Function code section and select Action then choose Upload a .zip file
-
In the popup, select Upload, select the zip file you just created, and choose Save
-
The file will update. Once it has loaded, select the Test Events dropdown at the top of the page and choose Configure test events
-
In the Event template selection box, choose Amazon Cloudwatch
-
Give the new event a name, such as
EventBridgeTest
and choose Create -
Once the test event is create, make sure it is selected and choose Test
-
Once the test completes, you should receive the following result:
{
"eventStatusCode": 200,
"eventResponse": "warm",
"eventType": "EventBridge"
}
- Lambda setup is complete, next we need to configure the EventBridge Rule
-
Login to the AWS Console
-
Make sure you are in the same region as your Amazon Connect instance. You can set the region by expanding the region selector in the upper right and choosing the region
-
Navigate to the Amazon EventBridge console
-
Choose Events, then select Create Rule
-
Provide a name for the Rule, such as
KeepSCVFunctionsWarm
-
Provide a description, if desired
-
In the Define pattern section, select Schedule
-
Set the Schedule options as Fixed rate every 5 Minutes
-
Leave the Select event bus settings to their defaults
-
In the Select targets section, leave Lambda function selected, then choose your InvokeTelephonyIntegrationApiFunction function from the Function dropdown
-
Select Create
-
Your rule is now enabled and will execute every 5 minutes, keeping the function warm