2

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?

Community
  • 1
  • 1
Centurion
  • 14,106
  • 31
  • 105
  • 197

2 Answers2

6

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)
Centurion
  • 14,106
  • 31
  • 105
  • 197
1

The code in the question is very close. The only issue is the InvocationType type:

This will work with the code in your question:

client.invoke(
    FunctionName='LambdaWorker',
    InvocationType='RequestResponse',
    LogType='None',
    ClientContext=payloadBase64
)

However this changes the invocation to synchronous which may be undesirable. The reason for this behavior is not clear.

Steve E.
  • 9,003
  • 6
  • 39
  • 57