How to get AWS IOT client id?
Asked Answered
A

4

7

I use AWS IoT for real-time update in my web application. The application connects to AWS IoT using aws-iot-device-sdk:

const client = awsIot.device({
    region: awsConfig.region,
    protocol: 'wss',
    accessKeyId: <accessKey>,
    secretKey: <secretKey>,
    sessionToken: <sessionToken>,
    port: 443,
    host: <iotEndpoint>
});

client.on('connect', res => {
    // ok
});

I want to use AWS Lifecycle Events. For example:

$aws/events/presence/connected/{clientId}

How to get MQTT client id?

Althing answered 8/10, 2017 at 17:15 Comment(0)
L
0

If you look at the documentation, you will see that clientId is one of the parameters you can supply to the device() method. You should be generating a client ID for each connected device that is unique to your application (i.e. unique to your AWS IoT account).

Luminesce answered 8/10, 2017 at 17:44 Comment(4)
Can you be a bit more specific? What does "generating a client ID" mean? I think the question is: how is that done?Scalar
@Scalar Amazon doesn't provide the client ID. You generate the client ID. How it is generated is up to you. You just need to make sure each IoT device has a unique ID.Luminesce
Sorry, one more question. "Generate" sounds like there is a website or api that does it. Do you mean just make one up? or is it somehow tied to the asset itself?Scalar
Make one up, use UUID or something similar, etc.Luminesce
M
4

Whenever you define a thing in AWS IoT, a unique identifier will be assigned to your device in your AWS IoT account. By default it is same as the name of thing(defaultClientId) and you can use it for connect to the AWS IoT broker. You can retrieve informaiotn about your thing by using AWS SDKs(or name of your device). For example by using Python SDK:

import boto3
client = boto3.client('iot')
response = client.describe_thing(
    thingName = [Name of your thing in AWS IoT]
)

print(response)
Martine answered 28/5, 2018 at 17:14 Comment(6)
I'm having an issue where my device only connects if the clientId IS the thing name. I want them to be different. Is this possible after the thing is already created?Raptor
That is because when you define the thing, you define the clientId same as the thing's name (it is the default of AWS console). If you are using Boto, you can update thing's information by using update_thing() function.Martine
Hmm okay, that's helpful. I'm using the node sdk so I'll see if there's an equivalent. Thanks!Raptor
Thanks! From where can I get the thing name to do that? Should I save it in an environment variable or somwhere else and retrieve ir whenever I need it?Cootch
One way to get thing's name is to use list_things from Boto3 API, check this: boto3.amazonaws.com/v1/documentation/api/latest/reference/…Martine
This should be the selected answer. I was trying to get my device to appear "connected" in AWS Fleet Hub, to do so I had to change the client id to the name of the thing in AWS.Nature
L
0

If you look at the documentation, you will see that clientId is one of the parameters you can supply to the device() method. You should be generating a client ID for each connected device that is unique to your application (i.e. unique to your AWS IoT account).

Luminesce answered 8/10, 2017 at 17:44 Comment(4)
Can you be a bit more specific? What does "generating a client ID" mean? I think the question is: how is that done?Scalar
@Scalar Amazon doesn't provide the client ID. You generate the client ID. How it is generated is up to you. You just need to make sure each IoT device has a unique ID.Luminesce
Sorry, one more question. "Generate" sounds like there is a website or api that does it. Do you mean just make one up? or is it somehow tied to the asset itself?Scalar
Make one up, use UUID or something similar, etc.Luminesce
I
0

You can get the client Id by using the SQL functions clientId like for exemple.

SELECT clientId() as clientId,* FROM '$aws/events/presence/connected/+

You will me able to retrieve it inside the message payload.

Internuncio answered 13/10, 2020 at 9:6 Comment(0)
S
0

To get the IoT device ClientID you could simply use this AWS CLI command: aws iot describe-thing --thing-name <your-thing-name> --query 'thingId'

Sardou answered 22/11, 2023 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.