Amazon Web Services, AWS, Cloud, Connect, IAM, Infrastructure as a Code, Lambda, Python, Script

AWS Connect and Lambda integration to get caller ID

April 27, 2018

Greetings!

IT Service Desk is backbone for support for any company to function. It’s the face of IT operations. what if you can take few tasks service desk performs and automate them. I am going dig into this. Last year Amazon announced service called connect, with this you can be up and running within minutes.

In this article we will connect AWS Connect with lambda. We will capture the called ID of user and if user exists in database we will greet user.

To start with, we will first create IAM role and policy for same.

Create a policy with with below shown configuration. I have named my policy as “connect_lambda_policy”

Next create a new role and attach this policy to it. My role name is “connect_lambda_role”.

Now we are ready with required permissions.

These permissions would be attached to lambda function we will create.

Next create a lambda function and attach the role we created earlier to it.

I am using python here.

Before writing the code we need to understand the input and output parameters which would be passed by AWS connect to lambda. On every Lambda function invocation from a contact flow, you pass a default set of information related to ongoing contact. Below id the example JSON request to a lambda function.

{

“Details”: {

“ContactData”: {

“Attributes”: {},

“Channel”: “VOICE”,

“ContactId”: “4a573372-1f28-4e26-b97b-XXXXXXXXXXX”,

“CustomerEndpoint”: {

“Address”: “+1234567890”,

“Type”: “TELEPHONE_NUMBER”

},

“InitialContactId”: “4a573372-1f28-4e26-b97b-XXXXXXXXXXX”,

“InitiationMethod”: “INBOUND | OUTBOUND | TRANSFER | CALLBACK”,

“InstanceARN”: “arn:aws:connect:aws-region:1234567890:instance/

c8c0e68d-2200-4265-82c0-XXXXXXXXXX”,

“PreviousContactId”: “4a573372-1f28-4e26-b97b-XXXXXXXXXX”,

“Queue”: “QueueName”,

“SystemEndpoint”: {

“Address”: “+1234567890”,

“Type”: “TELEPHONE_NUMBER”

}

},

“Parameters”: {

“sentAttributeKey”: “sentAttributeValue”

}

},

“Name”: “ContactFlowEvent”

}

The request is divided into three parts:

Contact data—This is always passed by Amazon Connect for every contact. Some parameters are

optional.

User attributes—These are attributes that have been previously associated with a contact, such as

when using a Set contact attributes block in a contact flow. This map may be empty if there aren’t any

saved attributes.

Parameters—These are parameters specific to this call that were defined when you created the

Lambda function.

To successfully pass attributes between your Lambda function and Amazon Connect, configure your

function to correctly parse the JSON request sent from the Invoke AWS Lambda function block, and

define any business logic that should be applied.

 

Below is the sample code I have written:

def lambda_handler(event, context):

telnumber = event[‘Details’][‘ContactData’][‘CustomerEndpoint’][‘Address’];

resultmap = {“Name”:”Virtual Singh”,”telnumber”:telnumber};

return resultmap;

 

Here input is passed in event object. We have referred the above JSON input above to get the telephone number of user calling and returned the name phone number of user calling.

Next create a AWS connect instance if you don’t have one.

Once created inside the AWS connect dashboard, from menu(left side) go to Contact flows.

I have created above wokflow.

You need to provide the lambda function arn in “Invoke AWS Lambda function” branch. Rest is straight forward.

I have used the result returned by lambda in success branch of “Invoke AWS Lambda function”.

You can use the members of JSON retuned using $.External.<member name>

The last thing you need to do it provide AWS connect permission to invoke lambda. For that run below command in AWS CLI.

aws lambda add-permission –function-name function:my-lambda-function –statement-id 1  –principal connect.amazonaws.com –action lambda:InvokeFunction –sourceaccount 123456789012  –source-arn arn:aws:connect:us-east-1:123456789012:instance/def1a4fc-ac9d-11e6- b582-06a0be38cccf

Happy Coding 😊

You Might Also Like

No Comments

Leave a Reply