Search the list of devices connected to aws iot core

0

Is there a way to query a list of devices connected to aws iot core?

  1. Is it provided by the AWS IoT API? (https://docs.aws.amazon.com/iot/latest/apireference/API_Operations_AWS_IoT.html)
  2. Is it provided by AWS IoT Core monitoring? (example. cloudwatch)
  3. Or do you need to manage the device's connection state yourself? I want to know the list of connected devices, not the access history. Thank you
질문됨 일 년 전983회 조회
2개 답변
2

Hi. You can use fleet indexing and SearchIndex. The query is connectivity.connected:true: https://docs.aws.amazon.com/iot/latest/developerguide/example-queries.html. These queries can be done on the console, with the CLI, or with Fleet Hub.

You can also use fleet metrics to get aggregates such as the count of connected devices: https://docs.aws.amazon.com/iot/latest/developerguide/index-aggregate.html. You can monitor the fleet metrics in CloudWatch.

profile pictureAWS
전문가
Greg_B
답변함 일 년 전
  • Thank you!!

  • No worries. The best way to thank me is to Accept the answer. :-)

  • Yes, you can use fleet indexing, but it's not real-time. I prefer to catch "$aws/events/presence/connected/clientId" and "$aws/events/presence/disconnected/clientId" events and save it on DynamoDB

0

I did this the way one of the commenters above, Eddy, suggested. To explain that a little more, what I did was to write a short lambda attached to the presence lifecycle:

SELECT * FROM '$aws/events/presence/#'

In my case, the lambda writes a record to timestream. You could write it somewhere else, like dynamodb, I benefit from having a log. This works for me because I don't have that many disconnect/reconnects so the timestream cost is negligible - YMMV.

I won't paste the entire lambda here (unless you want me to) but this is the general outline:

function handleConnectionChanged(status: LifecycleMessage, context: Context): Promise<boolean> {
/* junk deleted */
    const values: MeasureValue[] = [
        {Name: "session", Value: status.sessionIdentifier, Type: MeasureValueType.VARCHAR}
    ];
    if (status.eventType?.length) {
        values.push({Name: "event", Value: status.eventType, Type: MeasureValueType.VARCHAR})
    }

    if (status.ipAddress?.length) {
        values.push({Name: "ip", Value: status.ipAddress, Type: MeasureValueType.VARCHAR})
    }
    const writeRecord: WriteRecordsRequest = {
        DatabaseName: "my-iot-db",
        TableName: "my-tracking-table",
        CommonAttributes: {
            Time: status.timestamp.toString(),
            TimeUnit: TimeUnit.MILLISECONDS
        },
        Records: [
            {
                Dimensions: [
                    {"Name": "clientId", "Value": status.clientId, DimensionValueType: DimensionValueType.VARCHAR},
                    {"Name": "principal", "Value": status.principalIdentifier, DimensionValueType: DimensionValueType.VARCHAR}
                ],
                MeasureName: "state",
                MeasureValueType: MeasureValueType.MULTI,
                MeasureValues: values
            }
        ]
    }
   /* write the record */
}
profile picture
wz2b
답변함 9달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인