Boto3 documentation for lambda_client.invoke
suggests it is possible to pass data via the ClientContext
object as a string.
The string is required to be base64 encoded JSON.
To encode such a string, use:
import baseb4
base64.b64encode(b'<string>').decode('utf-8')
However, it is not clear how to pass the data.
It's tempting to think any JSON string will do and will surface via the Context
object of the lambda function handler:
def my_handler(event,context):
However, data should be passed via a custom
field:
event = {"body": { "my": "data" }} # body not necessary, but compatible with API gateway invocation
res = lambda_client.invoke(FunctionName='<your_function_name>',
InvocationType='RequestResponse',
ClientContext=base64.b64encode(b'{"custom":{"foo":"bar", \
"fuzzy":"wuzzy"}}').decode('utf-8'),
Payload=json.dumps(event))
It can then be access in the lambda function like so:
def my_handler(event,context):
my_custom_dict = context.client_context.custom
foo = my_custom_dict["foo"] # "bar"
# and so on
The use of the custom
field is kind of mentioned here, but not very well:
http://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html
Thanks! This is very useful. Just wondering, in term of usage, what is the difference between ClientContext and Payload?