Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke another lambda async and pass context to it?

I'm trying to get working two basic lambdas using Python2.7 runtime for SQS message processing. One lambda reads from SQS invokes and passes data to another lambda via context. I'm able to invoke the other lambda but the user context is empty in it. This is my code of SQS reader lambda:

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)

The problem is nothing works and CloudWatch shows user context is empty:

('client_context', None)

I've tried example1, example2, example3, example4

Any ideas?

like image 420
Centurion Avatar asked Mar 13 '26 00:03

Centurion


1 Answers

I gave up trying to pass the data through the context. However, I was able to pass the data through the Payload param:

client.invoke(
    FunctionName='LambdaWorker',
    InvocationType='Event',
    LogType='None',
    Payload=json.dumps(payload)
)

And then to read it from event parameter inside invoked lambda:

ctx = json.dumps(event)
like image 195
Centurion Avatar answered Mar 14 '26 12:03

Centurion