Mos aws-iot-setup fails

Hi there, I’m trying to get my devices on AWS IoT, but I’ve been having this intermittent issue that’s very frustrating.

Occasionally, when running the command mos aws-iot-setup --aws-region us-west-2, the command fails with the following message:

Attaching policy "mos-default" to the certificate...
2020/12/21 20:52:13 This operation, AttachPrincipalPolicy, has been deprecated
Attaching the certificate to "my-device-name"...
Writing certificate to aws-my-device-name.crt.pem...
Uploading aws-my-device-name.crt.pem (1141 bytes)...
Error: context deadline exceeded
/build/mos-BVS5qt/mos-2.18.0+1ec8595~xenial0/common/mgrpc/mgrpc.go:460:
/build/mos-BVS5qt/mos-2.18.0+1ec8595~xenial0/cli/dev/dev_conn_impl.go:167:
/build/mos-BVS5qt/mos-2.18.0+1ec8595~xenial0/cli/dev/dev_conn_impl.go:190:
/build/mos-BVS5qt/mos-2.18.0+1ec8595~xenial0/cli/fs/fs.go:240:
/build/mos-BVS5qt/mos-2.18.0+1ec8595~xenial0/cli/x509utils/gen_cert.go:328: failed to upload aws-my-device-name.crt.pem
/build/mos-BVS5qt/mos-2.18.0+1ec8595~xenial0/cli/aws/aws.go:401:
/build/mos-BVS5qt/mos-2.18.0+1ec8595~xenial0/cli/main.go:198: aws-iot-setup failed

The command hangs on Uploading aws-my-device-name.crt.pem (1141 bytes)... for about a minute or two. If I re-run the command multiple times, it’ll eventually work. Sometimes it works on the first try. Sometimes, I have to re-run this command 5+ times to get my device connected. I’ve tried reflashing the device with my firmware, power cycling the device, and even rebuilding the whole firmware & reflashing, without success.

Has anyone else experienced this issue or know what’s going on with this? Does it have anything to do with the This operation, AttachPrincipalPolicy, has been deprecated warning?

“Uploading aws-my-device-name.crt.pem” means mos is trying to store your certificate in your device. Somehow this operation fails. The way you connect to your device is too slow or not realiable; or what your device is doing at that time does not leave enough memory/processor time for the file operations to take place reliably.
The “operation deprecated” message is related to the way mos uses the aws API, one of the commands is old but working. It shouldn’t mean problems for a while.