5

In my project i have to create a py that call a lambda function passing body parameters, i write this code:

import boto3
import json
import base64

client = boto3.client(‘lambda’)
d = {'calID': '[email protected]', 'datada': '2017-12-22T16:40:00+01:00', 'dataa': '2017-12-22T17:55:00+01:00', 'email': '[email protected]'}
s = json.dump(d)
s64 = base64.b64encode(s.encode('utf-8'))

response = client.invoke(
    FunctionName='arn:aws:lambda:eu-west-1:13737373737:function:test',
    InvocationType='RequestResponse',
    LogType='None',
    ClientContext='None',
    Payload=s64
)

but when response run this error is generated:

InvalidRequestContentException: An error occurred (InvalidRequestContentException) when calling the Invoke operation: Could not parse request body into json: Unrecognized token 'eyJjYWxJRCI6ICI5MmRxaXNzNWJnODdldGNxZWVhbWxtb2IyZ0Bncm91cC5jYWxlbmRhci5nb29nbGUuY29tIiwgImRhdGFkYSI6ICIyMDE3LTEyLTIyVDE2OjQwOjAwKzAxOjAwIiwgImRhdGFhIjogIjIwMTctMTItMjJUMTc6NTU6MDArMDE6MDAiLCAiZW1haWwiOiAibHVjYV9ncmV6eml4eEBob3RtYWlsLmNvbSJ9': was expecting ('true', 'false' or 'null') at [Source: [B@4587098d; line: 1, column: 481]

what this mean?

Many thanks in advance

7
  • 3
    You can simply do Payload=json.dumps(d). Nothing else is needed Commented Dec 21, 2017 at 22:32
  • 1
    Hi, if i use d the response is : ParamValidationError: Parameter validation failed: Invalid type for parameter Payload, type: <class 'dict'>, valid types: <class 'bytes'>, <class 'bytearray'>, file-like object Commented Dec 21, 2017 at 22:34
  • how about s :P Commented Dec 21, 2017 at 22:35
  • Sorry, yes. It was meant to be json.dumps. Edited the previous comment as well. Please also see github.com/cleesmith/boto3_test/blob/master/invoke.py#L42 Commented Dec 21, 2017 at 22:35
  • if i use s (json.dumps(d)) code return : InvalidRequestContentException: An error occurred (InvalidRequestContentException) when calling the Invoke operation: Client context must be a valid Base64-encoded JSON object. Commented Dec 21, 2017 at 22:37

1 Answer 1

12

The error is because of the following parameter:

ClientContext='None',

From the docs:

ClientContext (string) --

Using the ClientContext you can pass client-specific information to the Lambda function you are invoking. You can then process the client information in your Lambda function as you choose through the context variable. For an example of a ClientContext JSON, see PutEvents in the Amazon Mobile Analytics API Reference and User Guide .

The ClientContext JSON must be base64-encoded and has a maximum size of 3583 bytes.

You do not need the ClientContext parameter here at all. Simply invoke as follows:

response = client.invoke(
    FunctionName='arn:aws:lambda:eu-west-1:13737373737:function:test',
    LogType='None',
    Payload=json.dumps(d)
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.