Use ‘cli-binary-format’ flag with AWS CLI version 2
Recently I tried to invoke AWS Lambda from CLI with the event payload. It worked the day before, but it broke now. I took an example from the official documentation and invoked my function:
aws lambda invoke \
--function-name my-function \
--invocation-type DryRun \
--payload '{ "name": "Bob" }' \
response.json
The response was different from what you can find in documentation:
Invalid base64: "{ "name": "Bob" }"
Moreover, when I changed the name value to “Fred” I received a different error message:
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 157)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')
While the invocation failed in CLI it successfully worked via AWS Console.
It looked like AWS updated APIs and by this introduced breaking changes. Potentially, any API that accepts payloads — SQS, Kinesis, etc —could have the same issue. Now it “thinks” that the provided payload base64
encoded.
Luckily AWS CLI version 2 has --cli-binary-format
a flag that allows you to specify how the binary input parameters interpreted. It supports two options: base64
and raw-in-base64-out
.
Adding the flag to the command fixed the issue:
aws lambda invoke \
--function-name my-function \
--invocation-type DryRun \
--payload '{ "name": "Bob" }' \
response.json \
--cli-binary-format raw-in-base64-out
You can find documentation for cli-binary-fomat
flag here.