skip to Main Content

I’m currently developing a lambda that invokes another Lambda with Boto3. However, I need from one statement to retrieve a group of results and send them through the invoke payload to the other Lambda. However, I can’t find how to send this function as a parameter to call another Lambda and pass through parameters a function that returns a set of information.

I have implemented this method:

from MysqlConnection import MysqlConnection
from sqlalchemy import text

    def make_dataframe(self):
        conn = MysqlConnection()
        query = text("""select * from queue WHERE estatus = 'PENDING' limit 4;""")
        df = pd.read_sql_query(query,conn.get_engine())
       
        return df.to_json()

This is the Lambda handler:

import json
import boto3
from MysqlConnection import MysqlConnection
from Test import Test


client = boto3.client('lambda')

def lambda_handler(event, context):
    
    mydb = MysqlConnection()
    print(mydb.get_engine)
    df = Test()
    df.make_dataframe()


    
    object = json.loads(df.make_dataframe())
    

    response = client.invoke(
        FunctionName='arn:aws:lambda:',
        InvocationType='RequestResponse'#event
        Payload=json.dumps(object)
        
        )
        
    

    responseJson = json.load(response['Payload'])

    print('n')
    print(responseJson)
    print('n')

2

Answers


  1. Chosen as BEST ANSWER

    after days of refactoring and research I am sharing the answer. It is about packing the json.dump object and inside the handler place the method with the response already packed

    This a method to parent child class Test:

    def make_dataframe(self):
        conn = MysqlConnection()
        query = text("""select * from TEST WHERE status'PEN' limit 4;""")
        df = pd.read_sql_query(query,conn.get_engine())
        lst = df.values.tolist()
        obj = json.dumps(lst, cls=CustomJSONEncoder)
    
        return obj
    
    def lambda_handler(event, context):
    mydb = MysqlConnection()
    df = Test()
    
    response = client.invoke(
        FunctionName='arn:aws:lambda:',
        InvocationType='RequestResponse',
        Payload= df.make_dataframe()
        )
    
    responseJson = json.load(response['Payload'])
    print('n')
    print(responseJson)
    print('n')
    

    `


  2. What you’re doing is correct in terms of structuring your call.
    I assume the problem is with your payload structure and whether its stringified.
    I would try invoke your lambda with an empty payload and see what happens. If it works with empty payload then its your payload serialising, if it doesnt work with empty payload then its something else.

    In cloudwatch what do your logs of both your "runner" lambda and your "target" lambda say?

    It might also be a permissions thing – you will need to specify and grant execute permissions on your runner lambda.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search