Solve - Invalid base64 Error when invoking Lambda - AWS CLI

avatar

Borislav Hadzhiev

Fri Sep 17 20212 min read

banner

Photo by Ryan Dam

Solving Invalid base64 Error in AWS CLI #

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.

Invalid base64 error

To solve the "Invalid base64" error set the --cli-binary-format parameter to raw-in-base64-out when invoking the lambda function:

shell
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --payload '{"name": "John Smith"}' response.json

with raw input

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.

Note that the --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:

event.json
{ "name": "John Doe" }

Invoke the lambda function like so:

shell
aws lambda invoke --function-name testFunction --cli-binary-format raw-in-base64-out --payload file://event.json response.json

invoke with param and file

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

with file without param

Further Reading #

Join my newsletter

I'll send you 1 email a week with links to all of the articles I've written that week

Buy Me A Coffee