2

I have to call using boto3 an AWS lambda. I do:

client = boto3.client("lambda")

dtime1 = str(datetime.datetime.now())
#After some computations
dtime2 = str(datetime.datetime.now())

elapsed =  time.time() - start_time

payload = {"key_id":"1",
        "data_start":dtime1,
        "data_stop":dtime2,
        "elapsed_t": int(elapsed)}

r = client.invoke(
            FunctionName='mylambda',
            InvocationType='RequestResponse',
            Payload=bytes(str(payload), 'utf-8')
        )

print(r.read())

but when i run it an error occur:

"An error occurred (InvalidRequestContentException) when calling the Invoke operation: Could not parse request body into json: Unexpected character (''' (code 39)): was expecting double-quote to start field name\n at [Source: [B@4cb02e4e; line: 1, column: 3]"

How can I resolve my issue?

Thanks in advance

2 Answers 2

2

If you want to pass a JSON object as a string, you can use json.dumps(payload) as described in https://docs.python.org/3/library/json.html.

Sign up to request clarification or add additional context in comments.

Comments

1

All the official documentation I've seen on passing a payload in a Lambda invocation has been missing or incorrect. This is what has worked for me:

# Construct a dict object
payload = {"key": "value"}

# Invoke the Lambda function, passing the payload
lambda_client.invoke(FunctionName='myFunctionName',
                     InvocationType='RequestResponse',
                     Payload=json.dumps(payload))

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.