MrTony
MrTony

Reputation: 312

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')

Upvotes: 2

Views: 2730

Answers (3)

cryptomamy
cryptomamy

Reputation: 67

Configurations

  • Mac OS
  • VS code

Error

--payload

The JSON that you want to provide to your Lambda function as input.

You can enter the JSON directly. For example, --payload '{ "key": "value" }' . You can also specify a file path. For example, --payload file://payload.json .

source

Correction

you need to clarify with this command:

--cli-binary-format raw-in-base64-out

Method

Here is the correct syntax:

aws lambda invoke \
> --invocation-type RequestResponse \
> --function-name my-function \
> --cli-binary-format raw-in-base64-out \
> --payload '{ "key": "value" }'  response.json

Run your code again

Upvotes: -1

Devashish Priyadarshi
Devashish Priyadarshi

Reputation: 175

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

Upvotes: 2

MrTony
MrTony

Reputation: 312

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...

Upvotes: 7

Related Questions