4

How to solve this error, I am running my Node.js with AWS IoT then it at times shows this error:

      throw er; // Unhandled 'error' event
      ^

Error: How  (/home/ec2-user/work/nodejs_27_01/node_modules/end-of-stream/index.js:54:                                                                                                     86)
    at processTicksAndRejections (internal/process/task_queues.js:79:11)
Emitted 'error' event on DeviceClient instance at:
    at MqttClient.<anonymous> (/home/ec2-user/work/nodejs_27_01/node_modules/aws-iot-device-sdk/                                                                                                     device/index.js:772:15)
    at MqttClient.emit (events.js:333:22)
    at MqttClient.EventEmitter.emit (domain.js:485:12)
    at TLSSocket.f (/home/ec2-user/work/nodejs_27_01/node_modules/once/once.js:25:25)
    at onclosenexttick (/home/ec2-user/work/nodejs_27_01/node_modules/end-of-stream/index.js:54:                                                                                                     73)

Brian Luong
  • 538
  • 3
  • 15

3 Answers3

4

There can be multiple reasons for this:

Multiple Connections with same ClientId

The clientId can only be used for one connection at a time. If you connect with the same clientId while another connection is established, the older connection gets dropped (which leads to the premature close error) and the new connection is established.

The client is using a client ID that is already in use. In this case, the client that is already connected will be disconnected [...]. (Source)

Permissions

This error can happen if a device (mqtt.Client from aws-iot-device-sdk-js) does not hold the correct permissions to connect and/or publish/subscribe/receive messages on a given topic.

See here for more documentation: https://docs.aws.amazon.com/iot/latest/developerguide/pub-sub-policy.html

The policy should look like this (example shows a Cloudformation Iot Policy resource):

MyIotThingsPolicy:
  Type: AWS::IoT::Policy
  Properties:
    PolicyDocument:
      Version: "2012-10-17"
      Statement:
        - Action: iot:Connect
          Effect: Allow
          Resource: !Join [ "", [!Sub "arn:aws:iot:${AWS::Region}:${AWS::AccountId}:client/",
                                 "${iot:ClientId}"] ]
        - Action: iot:Receive
          Effect: Allow
          Resource: !Join [ "", [!Sub "arn:aws:iot:${AWS::Region}:${AWS::AccountId}:topic/",
                                "${iot:ClientId}/eg/your/broadcast/topic"] ]
        - Action: iot:Subscribe
          Effect: Allow
          Resource: !Join [ "", [!Sub "arn:aws:iot:${AWS::Region}:${AWS::AccountId}:topicfilter/",
                                 "${iot:ClientId}/eg/your/broadcast/topic"] ]
        - Action: iot:Publish
          Effect: Allow
          Resource: !Join [ "", [!Sub "arn:aws:iot:${AWS::Region}:${AWS::AccountId}:topic/",
                                 "${iot:ClientId}/eg/your/publish/topic"] ]

The !Join is necessary since Cloudformation would try to resolve ${iot:ClientId}, which is a runtime value, and not known during deployment.

Troubleshooting

Simon
  • 173
  • 1
  • 6
  • Thanks at Simons, I will see what I can workaround for this because I had certificates for a single thing which I have built it to be able to read eve other things shadows and updates, now I have to find a way to have an object which will do the work. Subscribing to topics, listening to topics –  Jan 28 '20 at 17:02
  • Hey guys thank you so much for the assistance. It turned out to be an issue with my clientId. I was using single clientId for multiple devices, my device locally, my co-worker, the EC2 instance. SO the conflict was there. To solve it out I had to add multiple clientId in my Iot policy. Now I no longer have such problem. –  Jan 29 '20 at 12:59
1

While testing, I had attached policy with full "Action": "iot:*" permissions, but missed to activate the newly created certificate. Please go to IoT core service, Secure --> Certificates and verify the certificate attached to <Thing> is activated if you get error Error: premature close after you have confirmed the connection to end point with :

telnet <your-iot-endpoint> 8883.

See the attached image for the options available with each certificate on above specified page.

enter image description here

Midhun KM
  • 1,647
  • 1
  • 21
  • 31
0

I could solve this issue using the policy without any restriction and it worked for me.Since I didn't want to restrict to the client connected and the topics subscribed or publihsed I used this policy

"Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "iot:*",
      "Resource": "*"
    }
  ] 
rahulmr
  • 681
  • 1
  • 7
  • 19
  • Great thanks this would work too, but I suppose it would just posses you to security risks allowing all client to connect not one defined by you –  Feb 05 '20 at 10:58