Return Value to lambda function via python

49 views
Skip to first unread message

James Scott

unread,
Aug 28, 2017, 1:19:59 PM8/28/17
to PyMySQL Users
Does an one see the defect of the code below, I'm getting error:  File "/var/task/index.py", line 28, in handler
    Client_id = event['currentIntent']['slots']['Client']
KeyError: 'currentIntent'

Code:

import sys
import logging
import rds_config
import pymysql

# rds settings
rds_host = rds_config.db_endpoint
name = rds_config.db_username
password = rds_config.db_password
db_name = rds_config.db_name

logger = logging.getLogger()
logger.setLevel(logging.INFO)

try:
    conn = pymysql.connect(rds_host, user=name, passwd=password, db=db_name, connect_timeout=5)
except:
    logger.error("ERROR: Unexpected error: Could not connect to MySql instance.")
    sys.exit()

logger.info("SUCCESS: Connection to RDS mysql instance succeeded")

def handler(event, context):
    """
    This function fetches content from mysql RDS instance
    """

    Client_id = event['currentIntent']['slots']['Client']
    Question_id = event['currentIntent']['slots']['Question']

    with conn.cursor() as cur:
        cur.execute("select question_value FROM PL360.ac_repository  WHERE client_id=" + Client_id + " and question_id=" + Question_id)
        cur.fetchone()
        result = cur.fetchone()
        
        return {
               'dialogAction': {
                    'type': 'Close',
                    'fulfillmentState': 'Fulfilled',
                    'message': {
                       'contentType': 'PlainText',
                       'content': result
                    }
                }
        }
       
   



Reply all
Reply to author
Forward
0 new messages