Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoking AWS Lambda via CLI returns ERROR: Could not parse payload into json: Unexpected character ((CTRL-CHAR, code 145)):

I am trying to follow along the AWS Getting Started with Lambda Tutorial, but I am having Issues actually invoking my Function using the CLI.

I came across THIS step and got two errors:

An error occurred (InvalidRequestContentException) when calling the Invoke operation: Could not parse request body into json: Could not parse payload into json: Unexpected character ((CTRL-CHAR, code 145)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false') at [Source: (byte[])"��j[�"; line: 1, column: 2]

and

An error occurred (ResourceNotFoundException) when calling the GetLogEvents operation: The specified log group does not exist.

I assume the first error is caused by the first command : aws lambda invoke --function-name my-function --payload '{"key": "value"}' out.json

and the second error accordingly by: aws logs get-log-events --log-group-name /aws/lambda/my-function --log-stream-name $(cat out) --limit 5

I am more concerned about the first error. I tried to solve this, by looking at the documentation for invoking a lambda function using the CLI. The most basic example was:

aws lambda invoke --function-name my-function --payload '{ "key": "value" }' response.json

Using this, I get the same Error code

Could not parse payload into json: Unexpected character ((CTRL-CHAR, code 145)):

I have asked about this in the AWS Dev Forums, but have not gotten any answer. There where a few topics about similar errors on Stackoverflow, however they mentioned a specific character that was missing in the payload to be valid JSON. According to google "CTRL-CHAR" sometimes points out a line break in your JSON, but there are none in this example. As far, as I can tell, the payload is valid JSON.

According to the CLI Documentation, you can also use other data types as payload. So I tried just passing a list:

aws lambda invoke --function-name my-func2 --payload '[2, 3, 4, 5]' out.json

I got the error:

Could not parse request body into json: Could not parse payload into json: Unrecognized token 'Û': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

like image 407
MrTony Avatar asked Apr 05 '26 18:04

MrTony


2 Answers

Just in case anyone ever gets stuck at the same point while doing the official Lambda Tutorial:

I had the issue solved by adding: --cli-binary-format raw-in-base64-out as a parameter.

According to : CLI 2 AWS DOCS

This has something to do with encoding changes from CLI 1 to CLI 2. It can also be added to to the aws config file, so you dont have to add it manually every time.

However, I am not sure why the Lambda Tutorial would not mention this, since the tutorial assumes you use CLI 2 and also guides you through the steps of the installation...

like image 112
MrTony Avatar answered Apr 08 '26 07:04

MrTony


For me this way it worked in windows:

aws lambda invoke --function-name Func2 --payload {\"key1\":\"val1\"} --cli-binary-format raw-in-base64-out out.json

As suggested by MrTony, I added "--cli-binary-format raw-in-base64-out" args

like image 26
Devashish Priyadarshi Avatar answered Apr 08 '26 07:04

Devashish Priyadarshi