AWS IoT Core とは
AWS IoT Core is a managed platform that lets connected devices easily and securely interact with cloud applications and other devices. AWS IoT Core can support billions of devices and trillions of messages, and can process and route those messages to AWS endpoints and to other devices reliably and securely. With AWS IoT Core, your applications can keep track of and communicate with all your devices, all the time, even when they aren’t connected.
IoT デバイスを AWS や他のデバイスに連携できるマネージドプラットフォームです。
機能概要
- IoT デバイスと AWS 間の接続
- HTTP, MQTT, WebSocket, LoRaWAN のプロトコルをサポート
- 通信は TLS で暗号化
- 接続されたデバイスから送信されるデータの処理
- データのフィルタリング、変換、ルーティング
- 接続デバイスから送信されたデータにアプリケーションからアクセス可能
- 複数の AWS サービスと統合されている
- IAM, Cognito, X.509 証明書などによる認証が可能
- デバイス用の SDK が提供されている
以下のブログでは 6 つのポイントに絞って解説されているのであわせてご参照ください。
【入門】AWS IoT Coreを理解するために必要な6つのポイント | DevelopersIO
ユースケース
[AWS Black Belt Online Seminar] AWS IoT Core
- データ収集
- 数⼗万規模のデバイスからのデータ収集
- デバイスのデータの可視化、故障予測、異常検知
- 遠隔制御
- 遠隔にあるデバイスをクラウドを介してコントロール
- リモート機器制御
- ファームウェア更新
AWS の IoT サービスでの位置づけ
[AWS Black Belt Online Seminar] AWS IoT Core
上述のユースケースの通り、IoT Core はデータ収集とデバイス制御に使用されるという位置づけです。
2 つのユースケースのアーキテクチャ例やベストプラクティスについても上記公式資料で紹介されているのでご参照ください。
試してみた
AWS IoT Core チュートリアルの開始 - AWS IoT Core
チュートリアルについてはドキュメントでも紹介されていますが、よりシンプルな内容で試してみました。
01. モノの作成
IoT のコンソールから「モノ」を選択します。

モノを作成をクリックします。

1 つのモノを作成を選択します。

モノの名前を入力します。

新しい証明書を自動生成を選択します。

ポリシーの作成はスキップします。

証明書とキーをすべてダウンロードします。

コンソールにモノが表示されていれば作成完了です。

02. ポリシーの作成
IoT のコンソールから「ポリシー」を選択します。

作成をクリックします。

ポリシー名を入力します。

ポリシードキュメントは以下のように入力します。
- ポリシー効果: 許可
- ポリシーアクション: *
- ポリシーリソース: *

上記設定で作成し、コンソールにポリシーが表示されていれば作成完了です。

03. ポリシーを証明書にアタッチ
IoT のコンソールから「証明書」を選択します。

手順 01 で作成された証明書をクリックします。

ポリシーをアタッチをクリックします。

手順 02 で作成したポリシーを選択してアタッチします。

証明書にポリシーがアタッチされました。

04. スクリプト作成
今回は手元に IoT デバイスがないため、デバイスの代わり CloudShell でスクリプトを作成して IoT Core に接続します。
CloudShell を開き、手順 01 でダウンロードした証明書から以下の証明書をアップロードします。
- AmazonRootCA1.pem
- xxx.crt ファイル
- xxx-private.pem.key
アップロードされたことを確認します。
$ ls
AmazonRootCA1.pem
xxx-certificate.pem.crt
xxx-private.pem.key
以下のコマンドでアカウント内の AWS IoT エンドポイント情報を取得します。
$ aws iot describe-endpoint \
--endpoint-type iot:Data-ATS
{
"endpointAddress": "xxx-ats.iot.ap-northeast-1.amazonaws.com"
}
以下のコマンドでスクリプトを作成しますが、以下の項目は適宜置換してください。
- endpoint: describe-endpoint コマンドで取得したエンドポイント
- cert_path: xxx-certificate.pem.crt
- key_path: xxx-private.pem.key
$ cat <<EOF > publish.py
import paho.mqtt.client as mqtt
import ssl
import json
import time
endpoint = "<YOUR_ENDPOINT>.iot.ap-northeast-1.amazonaws.com"
port = 8883
topic = "test/topic"
ca_path = "AmazonRootCA1.pem"
cert_path = "device.pem.crt"
key_path = "private.pem.key"
def on_log(client, userdata, level, buf):
print("log: ", buf)
client = mqtt.Client()
client.on_log = on_log
client.tls_set(ca_certs=ca_path,
certfile=cert_path,
keyfile=key_path,
tls_version=ssl.PROTOCOL_TLSv1_2)
client.connect(endpoint, port, 60)
client.loop_start()
try:
count = 1
while True:
message = {
"message": f"Hello from CloudShell! ({count})"
}
result = client.publish(topic, json.dumps(message), qos=1)
result.wait_for_publish()
print(f"[{count}] Published message to topic {topic}: {message}")
count += 1
time.sleep(1)
except KeyboardInterrupt:
print("Stopped by user")
client.loop_stop()
client.disconnect()
EOF
必要なライブラリをインストール後、スクリプトを実行します。
$ pip3 install --user paho-mqtt
$ python3 publish.py
# 以下のようなログが出力されていれば OK
/home/cloudshell-user/publish.py:17: DeprecationWarning: Callback API version 1 is deprecated, update to latest version
client = mqtt.Client()
log: Sending CONNECT (u0, p0, wr0, wq0, wf0, c1, k60) client_id=b''
log: Sending PUBLISH (d0, q1, r0, m1), 'b'test/topic'', ... (41 bytes)
log: Sending CONNECT (u0, p0, wr0, wq0, wf0, c1, k60) client_id=b''
log: Received CONNACK (0, 0)
log: Sending PUBLISH (d1, q1, r0, m1), 'b'test/topic'', ... (41 bytes)
log: Received PUBACK (Mid: 1)
[1] Published message to topic test/topic: {'message': 'Hello from CloudShell! (1)'}
log: Sending PUBLISH (d0, q1, r0, m2), 'b'test/topic'', ... (41 bytes)
log: Received PUBACK (Mid: 2)
[2] Published message to topic test/topic: {'message': 'Hello from CloudShell! (2)'}
log: Sending PUBLISH (d0, q1, r0, m3), 'b'test/topic'', ... (41 bytes)
log: Received PUBACK (Mid: 3)
05. IoT Core で受信できていることを確認
手順 04 のスクリプトを実行した状態で IoT コンソールの MQTT テストクライアントを画面を開きます。

トピックのフィルター に test/topic と入力します。

サブスクライブをクリックし、1 秒ごとに以下のようなメッセージを受信できていることを確認します。
{
"message": "Hello from CloudShell! (48)"
}

以上で仮想の IoT デバイスから IoT Core にメッセージを送信できました。
まとめ
今回は AWS IoT Core について紹介しました。
どなたかの参考になれば幸いです。
Discussion