Last updated: Feb 26, 2024
Reading time·2 min

The "Invalid base64: {"key": "value"}" error most often occurs when a Lambda
function is invoked with --payload parameter consisting of JSON without
setting the --cli-binary-format to raw-in-base64-out.

To solve the error, set the --cli-binary-format parameter to
raw-in-base64-out when invoking the Lambda function.
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --payload '{"name": "John Smith"}' response.json

If you are on Windows, you have to escape the double quotes in the --payload
parameter - '{\"name\": \"John Smith\"}'.
The reason the AWS CLI throws the error is that by default the --payload
parameter is of type blob and expects valid base64 encoded input -
docs.
By setting the --cli-binary-format parameter to raw-in-base64-out we are
able to provide a raw JSON string to the --payload parameter.
--cli-binary-format parameter has to be set toraw-in-base64-out, even when passing a file to the --payload parameter.For example, if we had an event.json file with the following contents.
{ "name": "John Doe" }
Then you have to invoke the lambda function as follows.
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --payload file://event.json response.json

If we were to not set the --cli-binary-format parameter we would still get the
"Invalid base64" error.

I've also written detailed guides on how to invoke a Lambda function using AWS CLI and how to create a lambda function with AWS CLI.
You can learn more about the related topics by checking out the following tutorials: