https://dev.classmethod.jp/articles/aws-iot-events-ga/
https://docs.aws.amazon.com/ja_jp/iotevents/latest/developerguide/iotevents-getting-started.html
https://docs.aws.amazon.com/ja_jp/iotevents/latest/developerguide/iotevents-simple-example.html
-- 1. コマンド等のインストール
-- 1.1 aws cli version 2 インストール
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
aws --version
-- 1.2 jqインストール
sudo yum -y install jq
-- 2. SNSトピック作成
aws sns list-topics
aws sns list-subscriptions
aws sns create-topic --name topic01
aws sns create-topic --name topic02
aws sns subscribe \
--topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic01 \
--protocol email \
--notification-endpoint hoge@example.com
aws sns subscribe \
--topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic02 \
--protocol email \
--notification-endpoint hoge@example.com
-- 3. IAMポリシー作成
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sns:*"
],
"Effect": "Allow",
"Resource": [ "arn:aws:sns:ap-northeast-1:999999999999:topic01",
"arn:aws:sns:ap-northeast-1:999999999999:topic02"
]
}
]
}
aws iam create-policy \
--policy-name policy01 \
--policy-document file://policy01.json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "iotevents.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
aws iam create-role \
--role-name role01 \
--assume-role-policy-document file://role01.json
-- 5. ポリシーをロールにアタッチ
aws iam attach-role-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01 \
--role-name role01
-- 6. デバイスデータをキャプチャするための入力を作成します
vim input01.json
{
"inputName": "input01",
"inputDescription": "Pressure readings from a motor",
"inputDefinition": {
"attributes": [
{ "jsonPath": "sensorData.pressure" },
{ "jsonPath": "motorid" }
]
}
}
aws iotevents create-input --cli-input-json file://input01.json
aws iotevents list-inputs
aws iotevents describe-input \
--input-name input01
-- 7. デバイスの状態を表すディテクターモデルを作成する
vim model01.json
{
"detectorModelName": "model01",
"detectorModelDefinition": {
"states": [
{
"stateName": "Normal",
"onEnter": {
"events": [
{
"eventName": "init",
"condition": "true",
"actions": [
{
"setVariable": {
"variableName": "pressureThresholdBreached",
"value": "0"
}
}
]
}
]
},
"onInput": {
"transitionEvents": [
{
"eventName": "Overpressurized",
"condition": "$input.input01.sensorData.pressure > 70",
"actions": [
{
"setVariable": {
"variableName": "pressureThresholdBreached",
"value": "$variable.pressureThresholdBreached + 3"
}
}
],
"nextState": "Dangerous"
}
]
}
},
{
"stateName": "Dangerous",
"onEnter": {
"events": [
{
"eventName": "Pressure Threshold Breached",
"condition": "$variable.pressureThresholdBreached > 1",
"actions": [
{
"sns": {
"targetArn": "arn:aws:sns:ap-northeast-1:999999999999:topic01"
}
}
]
}
]
},
"onInput": {
"events": [
{
"eventName": "Overpressurized",
"condition": "$input.input01.sensorData.pressure > 70",
"actions": [
{
"setVariable": {
"variableName": "pressureThresholdBreached",
"value": "3"
}
}
]
},
{
"eventName": "Pressure Okay",
"condition": "$input.input01.sensorData.pressure <= 70",
"actions": [
{
"setVariable": {
"variableName": "pressureThresholdBreached",
"value": "$variable.pressureThresholdBreached - 1"
}
}
]
}
],
"transitionEvents": [
{
"eventName": "BackToNormal",
"condition": "$input.input01.sensorData.pressure <= 70 && $variable.pressureThresholdBreached <= 1",
"nextState": "Normal"
}
]
},
"onExit": {
"events": [
{
"eventName": "Normal Pressure Restored",
"condition": "true",
"actions": [
{
"sns": {
"targetArn": "arn:aws:sns:ap-northeast-1:999999999999:topic02"
}
}
]
}
]
}
}
],
"initialStateName": "Normal"
},
"key" : "motorid",
"roleArn": "arn:aws:iam::999999999999:role/role01"
}
aws iotevents create-detector-model --cli-input-json file://model01.json
aws iotevents list-detector-models
aws iotevents describe-detector-model \
--detector-model-name model01
-- 8. ディテクターへの入力としてメッセージを送信する
echo -n '{"motorid": "Fulton-A32", "sensorData": {"pressure": 80, "temperature": 39} }' | base64
vim high.json
{
"messages": [
{
"messageId": "00001",
"inputName": "input01",
"payload": "eyJtb3RvcmlkIjogIkZ1bHRvbi1BMzIiLCAic2Vuc29yRGF0YSI6IHsicHJlc3N1cmUiOiA4MCwgInRlbXBlcmF0dXJlIjogMzl9IH0="
}
]
}
aws iotevents-data batch-put-message --cli-input-json file://high.json
警告メールの受信確認
echo -n '{"motorid": "Fulton-A32", "sensorData": {"pressure": 60, "temperature": 29} }' | base64
vim normal.json
{
"messages": [
{
"messageId": "00002",
"inputName": "input01",
"payload": "eyJtb3RvcmlkIjogIkZ1bHRvbi1BMzIiLCAic2Vuc29yRGF0YSI6IHsicHJlc3N1cmUiOiA2MCwgInRlbXBlcmF0dXJlIjogMjl9IH0="
}
]
}
aws iotevents-data batch-put-message --cli-input-json file://normal.json
sed -i 's/"messageId": "00002"/"messageId": "00003"/' normal.json
aws iotevents-data batch-put-message --cli-input-json file://normal.json
sed -i 's/"messageId": "00003"/"messageId": "00004"/' normal.json
aws iotevents-data batch-put-message --cli-input-json file://normal.json
復旧メールの受信確認
-- 9. クリーンアップ
-- ディテクターモデルの削除
aws iotevents list-detector-models
aws iotevents delete-detector-model \
--detector-model-name model01
-- 入力の削除
aws iotevents list-inputs
aws iotevents delete-input \
--input-name input01
-- IAMロールの削除
aws iam list-roles | grep role01
aws iam detach-role-policy \
--role-name role01 \
--policy-arn arn:aws:iam::999999999999:policy/policy01
aws iam delete-role --role-name role01
-- IAMポリシーの削除
aws iam list-policies | grep policy01
aws iam delete-policy \
--policy-arn arn:aws:iam::999999999999:policy/policy01
-- SNSトピック削除
aws sns list-topics
aws sns list-subscriptions
aws sns unsubscribe --subscription-arn arn:aws:sns:ap-northeast-1:999999999999:topic01:11111111-1111-1111-1111-111111111111
aws sns delete-topic --topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic01
aws sns unsubscribe --subscription-arn arn:aws:sns:ap-northeast-1:999999999999:topic02:22222222-2222-2222-2222-222222222222
aws sns delete-topic --topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic02