1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWS IoT Core 単体で MQTT クライアントの接続状態を取得できるようになったので試す

1
Posted at

はじめに

こんにちは、ほうき星 @H0ukiStar です。

本日 AWS IoT Core に以下のようなアップデートがありました。

Posted on: May 28, 2026
Today, AWS IoT Core launches two new MQTT connection management APIs, GetConnection and ListSubscriptions, enabling you to easily access MQTT client connection and subscription information for your Internet of Things (IoT) devices. These APIs help you troubleshoot connectivity issues, monitor client behavior, and audit connection patterns across your device fleet.

デバイスの接続状態を確認するためにはこれまで Fleet Indexing を利用したり、あるいは Device Shadow 等を使用して自前で接続状態を保持したりする必要がありましたが、このアップデートにより IoT Core 単体でデバイスの接続状態を確認できるようになりました。
なお、Fleet Indexing や Device Shadow を利用して接続状態を確認する方法は過去以下の記事で紹介しておりますので、合わせて参考にしてください。

試してみた

早速試してみます。

AWS SDK にはまだ追加されていないように見えますので、SigV4 で署名の上 API を呼び出す必要があります。
今回は awscurl を利用して署名しました。awscurl は以下のように導入できます。

pip install awscurl

また API リクエスト時のエンドポイントは AWS CLI で以下のように確認できます。

aws iot describe-endpoint --endpoint-type iot:Data-ATS
{
    "endpointAddress": "abcd1234567890-ats.iot.ap-northeast-1.amazonaws.com"
}

GetConnection

この API は指定した MQTT クライアントの接続状態や Keep Alive 情報などを確認することができます。

awscurl を使用して以下のようにリクエストします。

awscurl \
  --service iotdevicegateway \
  --region ap-northeast-1 \
  "https://abcd1234567890-ats.iot.ap-northeast-1.amazonaws.com/connections/<clientId>"

SigV4 の service 名には iot ではなく iotdevicegateway を指定する必要があります。

以下のようにクライアントが現在接続されているかや、いつから接続されているのか等を確認できます。

{
    "connected": true,
    "cleanSession": true,
    "clientId": "clientId",
    "thingName": "thingName",
    "keepAliveDuration": 15,
    "connectedSince": 1780061470032
}

また includeSocketInformation=true とリクエストすることで、ソケット情報も取得できます。

awscurl \
  --service iotdevicegateway \
  --region ap-northeast-1 \
  "https://abcd1234567890-ats.iot.ap-northeast-1.amazonaws.com/connections/<clientId>?includeSocketInformation=true"
{
    "connected": true,
    "cleanSession": true,
    "clientId": "clientId",
    "thingName": "thingName",
    "sourceIp": "sourceIp",
    "sourcePort": sourcePort,
    "targetIp": "targetIp",
    "targetPort": 8883,
    "keepAliveDuration": 15,
    "connectedSince": 1780061470032
}

ListSubscriptions

この API は指定したクライアントがサブスクライブしているトピック等を確認することができます。

awscurl を使用して以下のようにリクエストします。

awscurl \
  --service iotdevicegateway \
  --region ap-northeast-1 \
  "https://abcd1234567890-ats.iot.ap-northeast-1.amazonaws.com/connections/<clientId>/subscriptions"

SigV4 の service 名には iot ではなく iotdevicegateway を指定する必要があります。

以下のように指定したクライアントがどのトピックをサブスクライブしているかを確認できます。

{
    "nextToken": "",
    "subscriptions": [
        {
            "topicFilter": "$aws/things/<thingName>/shadow/update/delta",
            "qos": 0
        }
    ]
}

さいごに

今回追加された GetConnection / ListSubscriptions API により、AWS IoT Core 単体で MQTT クライアントの接続状態やサブスクリプション情報を取得できるようになりました。

これまで接続状態の確認には Fleet Indexing や Device Shadow、Lifecycle Event を利用した独自実装などが必要でしたが、今後は AWS IoT Core 単体でよりシンプルにクライアント状態を確認できそうです。

まだ AWS SDK には追加されていないように見えますが、SigV4 署名を行うことで現時点でも利用可能です。
気になった方はぜひ試してみてください。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?