LoginSignup
3
3

More than 5 years have passed since last update.

DynamoDBに書き込む AWS IoT のルールの作り方

Last updated at Posted at 2017-12-10

1) DynamoDB のテーブルを作成します。
ハッシュキーを id、レンジキーを time にします。

iot_rule_dec1001.png

次のプログラムでも作成できます。

dynamo_create.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#   dynamo_create.py
#
#                   Dec/10/2017
# ---------------------------------------------------------------
import  sys
import  datetime
import  boto3
from decimal import *

# ---------------------------------------------------------------
def create_table_proc(table_name):
    table = dynamodb.create_table(
        TableName=table_name,
        KeySchema=[
        {
        'AttributeName': 'id',
        'KeyType': 'HASH'
        },
        {
        'AttributeName': 'time',
        'KeyType': 'RANGE'
        },
        ],
        AttributeDefinitions=[
            {'AttributeName': 'id', 'AttributeType': 'S' },
            {'AttributeName': 'time', 'AttributeType': 'S' },
            ],
        ProvisionedThroughput={
            'ReadCapacityUnits': 1,
            'WriteCapacityUnits': 1
            }
        )

    print("Table status:", table.table_status)
#
# ---------------------------------------------------------------
sys.stderr.write ("*** 開始 ***\n")
#
dynamodb = boto3.resource('dynamodb')

table_name = 'iot_test'

try:
    create_table_proc(table_name)
except Exception as ee:
    sys.stderr.write("*** error *** in create_table_proc ***\n")
    sys.stderr.write(str(ee) + "\n")

sys.stderr.write ("*** 終了 ***\n")
# ---------------------------------------------------------------

2) IoT のルールを作成します。
AWS Iot コンソールの ACT に入ります。
iot_rule_dec1002.png

ハッシュキー値 ${id_in}
レジスターの値 ${time_in}

iot_rule_dec1003.png

3) IoT Thing (ブローカー)を作成します。

iot_rule_dec1004.png

AWS コンソールで作成する方法は、
AWS IOT の簡単な使い方

AWS CLI で作成する方法は、
AWS IOT を AWS CLI で設定

4) Pub/Sub のテストを行ないます。
IoT Thing を作成した時に出来た認証ファイルを使います。

こちらにその方法があります。
Raspberry Pi で、mosquitto を使って、AWS IOT に Pub/Sub する

Publish する JSON を次のようにして下さい。

in01.json
{
  "id_in": "rasp_10001",
  "time_in": "2017-12-10 17:07:10",
  "message": "Hello"
}

次のように、DynamoDB に書き込まれます。
iot_rule_dec1005.png

3
3
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
3
3