LoginSignup
0
2

More than 5 years have passed since last update.

[JAWS-UG CLI] IoT #7 IoTトピックルールの作成 (S3)

Posted at

前提条件

IoTへの権限

AWS IoTに対してフル権限があること。

AWS CLI

以下のバージョンで動作確認済

  • AWS CLI 1.11.14
コマンド
aws --version
結果(例)
      aws-cli/1.11.14 Python/2.7.10 Darwin/15.6.0 botocore/1.4.71

バージョンが古い場合は最新版に更新しましょう。

コマンド
sudo -H pip install -U awscli

0. 準備

0.1. リージョンの決定

変数の設定
export AWS_DEFAULT_REGION='ap-northeast-1'

0.2. 変数の確認

プロファイルが想定のものになっていることを確認します。

変数の確認
aws configure list
結果(例)
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile       iotFull-handson-mbpr13        env    AWS_DEFAULT_PROFILE
      access_key     ****************XXXX shared-credentials-file
      secret_key     ****************XXXX shared-credentials-file
          region        ap-northeast-1        env    AWS_DEFAULT_REGION

0.3. IAMロールの指定

利用するIAMロールを決めます。

変数の設定
IAM_ROLE_NAME='AWSIoTRole'
コマンド
IAM_ROLE_ARN=$( \
        aws iam get-role \
          --role-name ${IAM_ROLE_NAME} \
          --query 'Role.Arn' \
          --output text \
      ) \
        && echo ${IAM_ROLE_ARN}
結果(例)
      arn:aws:iam::XXXXXXXXXXXX:role/AWSIoTRole

1. 事前作業

1.1. Topic Role名の決定

英数字大文字とアンダースコアだけが利用できます。 (ハイフンは使えないの
で注意してください。)

変数の設定
IOT_RULE_NAME="handson_rule_$( date '+%Y%m%d' )" \
        && echo ${IOT_RULE_NAME}

同名のトピックルールの不存在確認

コマンド
aws iot get-topic-rule \
        --rule-name ${IOT_RULE_NAME}
結果(例)
      An error occurred (UnauthorizedException) when calling the GetTopicRule operation: Access to topic rule 'handson_rule_20161117' was denied

1.2. S3バケット名の作成

変数の設定
S3_BUCKET_NAME="handson-$( \
          aws sts get-caller-identity \
            --query 'Account' \
            --output text \
)" \
        && echo ${S3_BUCKET_NAME}
結果(例)
      handson-20161117-XXXXXXXXXXXX

同名のバケットが存在しないことを確認します。

コマンド
aws s3 ls s3://${S3_BUCKET_NAME}/
結果(例)
      An error occurred (NoSuchBucket) when calling the ListObjects operation: The specified bucket does not exist

バケットを作成します。

変数の確認
cat << ETX

          S3_BUCKET_NAME:     ${S3_BUCKET_NAME}
          AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION}

ETX
コマンド
aws s3api create-bucket \
          --bucket ${S3_BUCKET_NAME} \
          --create-bucket-configuration "LocationConstraint=${AWS_DEFAULT_REGION}"
結果(例)
      {
        "Location": "http://corp-handson-20160309.s3.amazonaws.com/"
      }
コマンド
aws s3api get-bucket-location \
        --bucket ${S3_BUCKET_NAME}
結果(例)
      {
        "LocationConstraint": "ap-northeast-1"
      }

1.x. トピックルールペイロードの作成

変数の設定
FILE_INPUT="${IOT_RULE_NAME}.json" \
        && echo ${FILE_INPUT}
変数の設定
IOT_RULE_DESC="Topic Rule for S3."
変数の設定
IOT_RULE_S3_KEY='handson-${timestamp()}'
変数の設定
IOT_RULE_ATTRIBUTE='*'
IOT_RULE_FILTER='#'
変数の確認
cat << EOF

        FILE_INPUT: ${FILE_INPUT}
        IOT_RULE_ATTRIBUTE:    ${IOT_RULE_ATTRIBUTE}
        IOT_RULE_FILTER:       ${IOT_RULE_FILTER}
        IOT_RULE_DESC:         ${IOT_RULE_DESC}
        IAM_ROLE_ARN:          ${IAM_ROLE_ARN}
        S3_BUCKET_NAME:        ${S3_BUCKET_NAME}
        IOT_RULE_S3_KEY:       ${IOT_RULE_S3_KEY}
        IOT_RULE_NAME:         ${IOT_RULE_NAME}

EOF
コマンド
cat << EOF > ${FILE_INPUT}
{
          "sql": "SELECT ${IOT_RULE_ATTRIBUTE} FROM '${IOT_RULE_FILTER}'",
          "description": "${IOT_RULE_DESC}",
          "actions": [
              {
                  "s3": {
                      "roleArn": "${IAM_ROLE_ARN}",
                      "bucketName": "${S3_BUCKET_NAME}",
                      "key": "${IOT_RULE_S3_KEY}"
                  }
              }
          ],
          "ruleDisabled": false,
          "awsIotSqlVersion": "2016-03-23"
}
EOF

cat ${FILE_INPUT}

JSONファイルを作成したら、フォーマットが壊れてないか必ず確認します。

コマンド
jsonlint -q ${FILE_INPUT}

エラーが出力されなければOKです。

2. トピックルールの作成

変数の確認
cat << ETX

        IOT_RULE_NAME: ${IOT_RULE_NAME}
        FILE_INPUT:    ${FILE_INPUT}

ETX
コマンド
aws iot create-topic-rule \
        --rule-name ${IOT_RULE_NAME} \
        --topic-rule-payload file://${FILE_INPUT}
結果(例)
      (戻り値なし)

3. 事後作業

コマンド
aws iot get-topic-rule \
        --rule-name ${IOT_RULE_NAME}
結果(例)
      {
        "ruleArn": "arn:aws:iot:us-west-2:580535187998:rule/tmpToS3sample",
        "rule": {
          "description": "20161117-handson-sample",
          "ruleName": "tmpToS3sample",
          "actions": [
              {
                  "s3": {
                      "roleArn": "arn:aws:iam::580535187998:role/AWSIoTRole",
                      "bucketName": "handson-rule-20161117-580535187998",
                      "key": "handson-${timestamp()}"
                  }
              }
          ],
          "sql": "SELECT * FROM '#'",
          "awsIotSqlVersion": "2016-03-23",
          "ruleDisabled": false
        }
      }

完了

0
2
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
2