Skip to content

Instantly share code, notes, and snippets.

@pgolding
Last active August 25, 2023 16:07
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pgolding/4edc4f3d066679117bba187105678028 to your computer and use it in GitHub Desktop.
Save pgolding/4edc4f3d066679117bba187105678028 to your computer and use it in GitHub Desktop.
Passing AWS Lambda Function parameters using the ClientContext object in Python

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

@mannharleen
Copy link

Please note:

  • I noticed that we cannot pass ClientContext when we invoke lambda using InvocationType = 'Event'

@stanyates
Copy link

Nice. Much appreciated. This is so poorly documented and poorly conceived in AWS that I'm interested in how it ended up the way it is. But I have a use case needing it and this was the key.

@dmytrokosiachenko
Copy link

Thanks, same applies for Rust.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment