AWS Lambda : How Can We Pass the Event by Lambda Test Event, Instead Of Transformer In Event Bridge?

Question:

We have the sceduled job to count and export several tables by Lambda Function.
We usually pass the timestamp and table name to Lambda Function by transformer in Event Bridge.

We’d like to run Lambda Function manually by Lambda Test Event, when we need to run only specific table as trouble shooting.
I set the Lambda Test Event just as same as the payload by transformer in Event Bridge, but ‘json.loads(event)’ returns error (It runs successfully by scheduled transformer in Event Bridge)

-- Lambda Test Event Json Setting
{
    "time": "2023-03-26T02:05:00Z",
    "table": "T_Test"
}

-- Lambda Code 

import os
import sys
import json
from datetime import datetime, date, timedelta

def lambda_handler(event, context):
    
    print('event: ', event)
    payload = json.loads(event)
    dateFormatter = "%Y-%m-%dT%H:%M:%S%z"
    time = payload['time']
    table = payload['table']
    tables = table.split(',')
    for table in tables: 
        print(table) ..

 -- Error Message by Test Event Manually
[ERROR] TypeError: the JSON object must be str, bytes or bytearray, not dict

We understand something wrong in my Test Event Json but not sure why.

Could you kinly give us any advice to handle this error, just only revise the Test Event, not to lambda code itself ?
Thank you so much in advane.

*** My Solution ***

Thanks to advices, I changed my test event that json.load can handle as follows and went OK now.

-- Revised Lambda Test Event Json Setting
"{ "time": "2023-03-26T02:05:00Z" , "table": "T_Test" }"
Asked By: Sachiko

||

Answers:

Follow this link to know more about json.loads() function.
https://www.geeksforgeeks.org/json-loads-in-python/


Argument: it takes a string, bytes, or byte array instance which contains the JSON document as a parameter (s).

You can not give dict to json.loads

Because the type of event is dict , hence you are getting error .

import os
import sys
import json
from datetime import datetime, date, timedelta

def lambda_handler(event, context):
    
    print('event: ', event)
    payload = event # removed json.loads
    dateFormatter = "%Y-%m-%dT%H:%M:%S%z"
    time = payload['time']
    table = payload['table']
    tables = table.split(',')
    for table in tables: 
        print(table)
    #This is working fine.

Answered By: Chaurasiya