sp_m
sp_m

Reputation: 2707

aws cli lambda-Could not parse request body into json

I have created aws lambda function in .net core and deployed. I have tried executing function in aws console with test case and its working. but i am not able achieve the same with cli command

aws lambda invoke --function-name "mylambda" --log-type Tail --payload file://D:/Files/lamdainputfile.json file://D:/Files/response.txt

i got getting error with cli command

An error occurred (InvalidRequestContentException) when calling the Invoke operation: Could not parse request body into json: Unexpected character ((CTRL-CHAR, code 138)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
 at [Source: (byte[])"�zn�]t�zn�m�"; line: 1, column: 2]

I tried passing json

aws lambda invoke --function-name "mylambda" --log-type Tail --payload "{'input1':'100', 'input2':'200'}" file://D:/Files/response.txt

but it's not working

This lambda function is executing aws console with test case and giving correct result. I have added same input in local json file and tried with cli command.

Json input:

{
  "input1": "100",
  "input2": "200"
}

EDIT:

After correction in inline json i am getting error for output file

Unknown options: file://D:/Files/response.txt

is there any command to print output in cli only?

Upvotes: 11

Views: 9288

Answers (4)

B3N3D1K7
B3N3D1K7

Reputation: 59

Depending on which aws cli version (v1 vs v2) you use, you need to consider the cli-binary-format option and its default:

The formatting style to be used for binary blobs. The default format is base64. The base64 format expects binary blobs to be provided as a base64 encoded string. The raw-in-base64-out format preserves compatibility with AWS CLI V1 behavior and binary values must be passed literally. When providing contents from a file that map to a binary blob fileb:// will always be treated as binary and use the file contents directly regardless of the cli-binary-format setting. When using file:// the file contents will need to properly formatted for the configured cli-binary-format.

Documentation: https://awscli.amazonaws.com/v2/documentation/api/latest/reference/lambda/invoke.html

Upvotes: 0

Jonathan Eckel
Jonathan Eckel

Reputation: 396

Adding the option --cli-binary-format raw-in-base64-out will allow you to pass raw json in the invoke command.

aws lambda invoke \
    --cli-binary-format raw-in-base64-out \
    --function-name "mylambda" \
    --payload '{"input1": "100", "input2": "200"}' \
    file://D:/Files/response.txt

Upvotes: 23

user 923227
user 923227

Reputation: 2715

The documentation is not updated from the cli version 1. For the aws cli version 2 we need to base64 encode the payload.

Mac:

payload=`echo '{"input1": 100, "input2": 200 }' | openssl base64`
aws lambda invoke --function-name myfunction --payload "$payload" SomeOutFile &

Upvotes: 22

Attila Viniczai
Attila Viniczai

Reputation: 693

Based on ASW CLI invoke command options --payload only accepts inline blob arguments (i.e. JSON). In other words --payload parameter can not be used to read input from a file, so --payload file://D:/Files/lamdainputfile.json will not work.

In the example provided what probably happens is --payload is ignored, file://D:/Files/lamdainputfile.json is treated as <outfile>, and an error is raised for file://D:/Files/response.txt as it is an unexpected positional argument. What is required is reading the contents of D:/Files/lamdainputfile.json with a separate command. How this can be done is different based on the type of shell used. Bash example:

aws lambda invoke --payload "$(cat /path/to/input.json)" ...

Original answer:

I don't know about the first case (--payload file:///...), however the second case is not a valid JSON, as JSON requires strings to be double quoted. Try the following JSON:

{
    "input": "100",
    "input2": "200"
}

Upvotes: 2

Related Questions