Borislav Hadzhiev
Fri Sep 17 2021·2 min read
Photo by Ryan Dam
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 "Invalid base64" 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 "Invalid base64" 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 contents of:
{ "name": "John Doe" }
Invoke the lambda function like so:
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --payload file://event.json response.json
If we were to omit setting the --cli-binary-format
parameter we would still
get the "Invalid base64" error.