Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (19.1k points)

I have create 2 simple Lambda function using a Python 2.7 runtime and the trigger is SQS.

The first Lambda function reads the messages from the SQS queue which is the actually the trigger of the function. Now, I am able to invoke the second lambda function but the user context part is empty. The code for SQS reader is below: 

import boto3

import base64

import json

import logging

messageDict = {'queue_url': 'queue_url',

       'receipt_handle': 'receipt_handle',

       'body': 'messageBody'}

ctx = {

   'custom': messageDict,

   'client': 'SQS_READER_LAMBDA',

   'env': {'test': 'test'},

}

payload = json.dumps(ctx)

payloadBase64 = base64.b64encode(payload)

client = boto3.client('lambda')

client.invoke(

    FunctionName='LambdaWorker',

    InvocationType='Event',

    LogType='None',

    ClientContext=payloadBase64,

    Payload=payload

)

And this is how I'm trying to inspect and print the contents of context variable inside invoked lambda, so I could check logs in CloudWatch:

memberList = inspect.getmembers(context)

    for a in memberList:

       logging.error(a)

It isn't working and CloudWatch shows the below output that there is no context:

('client_context', None)

I took reference from this as well:  https://github.com/aws/aws-sdk-js/issues/1388 but still now working

1 Answer

0 votes
by (44.4k points)
edited by

I will provide an solution but using a payload parameter instead of context:

client.invoke(

    FunctionName='LambdaWorker',

    InvocationType='Event',

    LogType='None',

    Payload=json.dumps(payload)

)

 Then you can use the below code to read it from the event parameter in the second/invoked lambda:

ctx = json.dumps(event)

 Also, I think you can take a look at the AWS Lambda part of this AWS Tutorial and for more insights you can check the AWS training page :)

Related questions

Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...