0
0

More than 1 year has passed since last update.

AWSIOT: デバイスデータを DynamoDB に保存

Last updated at Posted at 2021-09-13

次のページを参考にしました。
デバイスデータを DynamoDB テーブルに保存する

device/13/aa や device/13/bb というトピックにパブリッシュされたデータを
table_sep13 に保存します。

次の4ステップになります。
1) DynamoDB でテーブルを作成

テーブル名 table_sep13

キー 名前
ハッシュキー  device_id String
レインジキー  sample_time Number

2) IAM でロールを作成

ロール名 role_sep13

3) IAM でポリシーをロールにアタッチ

DynamoDB にアクセスできるポリシーをアタッチ

4) IOT でルールを作成

ルール名 rule_sep13
トピックの第 3 項をハッシュキー
タイムスタンプを レインジキー
に入れる。

テーブルを作成

create_table.sh
#
aws dynamodb create-table --table-name 'table_sep13' \
--attribute-definitions '[{"AttributeName":"device_id","AttributeType": "S"},{"AttributeName":"sample_time","AttributeType": "N"}]' \
--key-schema '[{"AttributeName":"device_id","KeyType": "HASH"},{"AttributeName":"sample_time","KeyType": "RANGE"}]' \
--provisioned-throughput '{"ReadCapacityUnits": 1,"WriteCapacityUnits": 1}'
#

確認

aws dynamodb list-tables
aws dynamodb describe-table --table-name table_sep13

削除

aws dynamodb delete-table --table-name table_sep13

ロールの作成

次のページを参考にしました。
AWS IoT への必要なアクセス権限の付与

create_role.sh
aws iam create-role --role-name role_sep13 --assume-role-policy-document file://iot-role-trust.json
iot-role-trust.json
{
  "Version":"2012-10-17",
  "Statement":[{
      "Effect": "Allow",
      "Principal": {
        "Service": "iot.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
  }]
}

確認

aws iam list-roles | grep RoleName
aws iam get-role --role-name role_sep13

ポリシーをロールにアタッチ

ポリシーの ARN を使ってポリシーをロールにアタッチします。

attach_role.sh
ROLE_NAME=role_sep13
aws iam attach-role-policy --role-name $ROLE_NAME \
    --policy-arn "arn:aws:iam::aws:policy/AmazonDynamoDBFullAccess"

確認

aws iam  list-attached-role-policies --role-name role_sep13

IOT ルールの作成

ロールの ARN が必要です。

create_rule.sh
aws iot create-topic-rule --rule-name rule_sep13 \
    --topic-rule-payload file://myrule.json
myrule.json
{
  "sql": "SELECT temperature,humidity FROM 'device/13/+'",
  "ruleDisabled": false,
  "awsIotSqlVersion": "2016-03-23",
  "actions": [{
      "dynamoDB": {
          "tableName": "table_sep13",
          "roleArn": "arn:aws:iam::123495704257:role/service-role/role_sep13",
          "hashKeyField": "device_id",
          "hashKeyValue": "${topic(3)}",
          "hashKeyType": "STRING",
          "rangeKeyField": "sample_time",
          "rangeKeyValue": "${timestamp()}",
      "rangeKeyType": "NUMBER",
      "payloadField": "device_data"
      }
  }]
}

確認

aws iot list-topic-rules | grep ruleName
aws iot get-topic-rule --rule-name rule_sep13

削除

aws iot delete-topic-rule --rule-name rule_sep13

テスト

例えば
device/13/aa というトピックに次のようなデータをパプリッシュします。

in01.json
{
  "temperature": 20.7,
  "humidity": 45.7
}

DynamoDB にデータが挿入されたことを確認

aws dynamodb scan --table-name table_sep13  --select "COUNT"

パブリッシュをするスクリプト

curl のサンプル

curl_publish.sh
#
CAFILE="../certs/Amazon-root-CA-1.pem"
CERTFILE="../certs/device.pem.crt"
KEYFILE="../certs/private.pem.key"
#
HOST="https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com:8443"
TOPIC="device/13/ttt"
URL=$HOST"/topics/"$TOPIC"?qos=1"
#
curl --tlsv1.2 \
    --cert $CERTFILE \
    --key $KEYFILE \
    -X POST $URL -d@in01.json
#

HTTPie のサンプル

http_publish.sh
#
CAFILE="../certs/Amazon-root-CA-1.pem"
CERTFILE="../certs/device.pem.crt"
KEYFILE="../certs/private.pem.key"
#
HOST="https://abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com:8443"
TOPIC="device/13/vvv"
URL=$HOST"/topics/"$TOPIC"?qos=1"
#
http --cert $CERTFILE \
    --cert-key $KEYFILE \
    -f post $URL < in01.json
#

mosquitto のサンプル

mos_publish.sh
#
CAFILE="../certs/Amazon-root-CA-1.pem"
CERTFILE="../certs/device.pem.crt"
KEYFILE="../certs/private.pem.key"
#
HOST="abcd6goq68zt4o-ats.iot.ap-northeast-1.amazonaws.com"
TOPIC="device/13/uuu"
#
mosquitto_pub --cafile $CAFILE \
    --cert $CERTFILE \
    --key $KEYFILE \
    -h $HOST \
    -p 8883 -q 1 -d -t $TOPIC \
    -f in01.json
#
0
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
0
0