Invalid base64 Error when invoking Lambda - AWS CLI [Fixed]

avatar
Borislav Hadzhiev

Last updated: Sep 17, 2021
2 min

banner

# Invalid base64 Error when invoking Lambda - 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 as follows:

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 not set the --cli-binary-format parameter we would still get the "Invalid base64" error.

with file without param

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.