Step Function Key Error?

0

Hello

I am pulling values from a dictionary provided as a payload to a Lambda Function and it runs successfully. These are known keys and I printed out the dictionary and checked that it's type is a dictionary. When I execute using the Step Function I am receiving the below key error. If I remove that key then the function throws an error on the next key I am using. { "errorMessage": "'PROC'", "errorType": "KeyError", "requestId": "bec5fb2a-5892-4b62-94be-d01a5b7cfa24", "stackTrace": [ " File "/var/task/lambda_function.py", line 63, in lambda_handler\n PROCNAME = output['PROC']\n" ] }

Here is the payload from the first function that is then used by the function that is erroring. "Payload": { "AWSDT": "2024-03-07 06:21:00", "DATALOADCONFIGID": 1, "PROC": "Info.usp_Load_F7", "TABLENAME": "Info.F7", "FILENAME": "F7_202403070621.csv" }

Here is my python code section that pulls from the dict and loads into variables. output = json.loads(dump_info) print(output) jdata_type = type(output) print(jdata_type) BATCHTIME = output['AWSDT'] CONFIGID = output['DATALOADCONFIGID'] PROCNAME = output['PROC'] TABLENAME = output['TABLENAME'] KEYNAME = output['FILENAME']

logger.info('Variables loaded')
logger.info('Values used {},{},{},{},{},'.format(PROCNAME,CONFIGID,TABLENAME,KEYNAME,BATCHTIME))

Can anyone tell me why this is happening and how to fix it?

Monty
asked 2 months ago281 views
1 Answer
1

It looks like the error you're encountering is a KeyError, which means the key you're trying to access in the dictionary doesn't exist. Based on the error message, it seems that PROC is missing from the dictionary output. This could happen if the dictionary doesn't contain the key PROC for some reason.

To troubleshoot, you can add some additional logging to check the keys present in the output dictionary before trying to access them. For example:

output = json.loads(dump_info)
print(output)
jdata_type = type(output)
print(jdata_type)

# Check if the keys exist in the dictionary
if 'AWSDT' in output:
    BATCHTIME = output['AWSDT']
else:
    logger.error('Key AWSDT not found in dictionary')

if 'DATALOADCONFIGID' in output:
    CONFIGID = output['DATALOADCONFIGID']
else:
    logger.error('Key DATALOADCONFIGID not found in dictionary')

if 'PROC' in output:
    PROCNAME = output['PROC']
else:
    logger.error('Key PROC not found in dictionary')

if 'TABLENAME' in output:
    TABLENAME = output['TABLENAME']
else:
    logger.error('Key TABLENAME not found in dictionary')

if 'FILENAME' in output:
    KEYNAME = output['FILENAME']
else:
    logger.error('Key FILENAME not found in dictionary')

logger.info('Variables loaded')
logger.info('Values used {},{},{},{},{},'.format(PROCNAME, CONFIGID, TABLENAME, KEYNAME, BATCHTIME))
profile picture
EXPERT
answered 2 months ago
  • Osvaldo - thankyou for offering a answer but I am trying to understand if there is a way that my Keys have to be surfaced using a Step Function than when I execute as a standalone Lambda function. The payload shown in the Step Function GUI shows that the Keys are being passed and when I print the EVENT data they are shown as well.

    "Payload": { "AWSDT": "2024-03-07 06:21:00", "DATALOADCONFIGID": 1, "PROC": "Info.usp_Load_F7", "TABLENAME": "Info.F7", "FILENAME": "F7_202403070621.csv" }

  • It's possible that the 'PROC' key is not being correctly passed from the Step Function to the Lambda function. Despite appearing in the Step Function GUI and your print statements, there might be an issue with how it's extracted or processed in your Lambda function. Check for typos, confirm the 'output' dictionary's contents, and ensure it's not modified before accessing 'PROC'. Simplify your code, verify the Step Function's output, and review how data is passed to the Lambda function.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions