Home > Back-end >  Returning a key value pair AWS Lambda function in python in a call centre connect instance
Returning a key value pair AWS Lambda function in python in a call centre connect instance

Time:08-27

I am trying to create a connect instance with a lambda function. This lambda function needs to go into DynamDB and return the value associated with the primary key as a key value pair. The function is using python to program in.

What happens is that testing the function is passing, I am able to get a successful result through the test option with the function. However when I add the lambda function to my contact flow it is failing to even run the function successfully, and it is throwing the error "error". I think this is due to the result of it needing a key value repair.

Below is the code for the Lambda funcion:

import json
import boto3

client = boto3.client('dynamodb')

def lambda_handler(event, context):
  data = client.get_item(
    TableName='MessageFlat',
    Key={'Message': {'S': "1"}
    }
  )

  response = {
      'statusCode': 200,
      'body': json.dumps(data),
      'headers': {
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*'
      },
  }
  
  return response

This is the response given by the lambda function.

Response
{
  "statusCode": 200,
  "body": "{\"Item\": {\"Message\": {\"S\": \"1\"}, \"Hello\": {\"S\": \"Good Day. Welcome to Shelby's contact centre. \"}}, \"ResponseMetadata\": {\"RequestId\": \"S1CRNMSO7KM15TE5I70HJ2AL0FVV4KQNSO5AEMVJF66Q9ASUAAJG\", \"HTTPStatusCode\": 200, \"HTTPHeaders\": {\"server\": \"Server\", \"date\": \"Thu, 25 Aug 2022 22:54:10 GMT\", \"content-type\": \"application/x-amz-json-1.0\", \"content-length\": \"93\", \"connection\": \"keep-alive\", \"x-amzn-requestid\": \"S1CRNMSO7KM15TE5I70HJ2AL0FVV4KQNSO5AEMVJF66Q9ASUAAJG\", \"x-amz-crc32\": \"996959635\"}, \"RetryAttempts\": 0}}",
  "headers": {
    "Content-Type": "application/json",
    "Access-Control-Allow-Origin": "*"
  }
}

This is the needed output from the lambda function

{
"Message": "1",
"Hello": "Good Day. Welcome to Shelby's contact centre."
}

This is what my table is made up of.

enter image description here

CodePudding user response:

If you simply want a subset of attributes of the DynamoDB item, then try this:

response = {
    "Message": data["Item"]["Message"]["S"],
    "Hello": data["Item"]["Hello"]["S"]
}

return response
  • Related