Fixing "Invalid base64" errors when publishing data to AWS IoT Core from the AWS CLI

Originally published on 2020-11-06

If you've tried to publish data using the iot-data module in the AWS CLI you've likely done something like this:

aws iot-data publish --topic 'iot/topic' --payload file://data.json

And seen an error like this:

Invalid base64: "{
  "mykey": "myvalue"

You never said your data would be in base64 format but the AWS CLI v2 defaults to base64 encoding. To fix it you need to add the --cli-binary-format raw-in-base64-out option like this:

aws iot-data publish --topic 'iot/topic' --cli-binary-format raw-in-base64-out --payload file://data.json

That tells the CLI that your input is raw and it'll no longer try to base64 decode it.