LoginSignup
1

More than 5 years have passed since last update.

[JAWS-UG CLI] IoT #2 IoTポリシーの作成

Last updated at Posted at 2016-11-17

AWS CLIを利用して、IoT上にThingを作成してみます。

前提条件

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         administrator-prjz-mbp13        env    AWS_DEFAULT_PROFILE
      access_key     ****************XXXX shared-credentials-file
      secret_key     ****************XXXX shared-credentials-file
          region                         ap-northeast-1  env    AWS_DEFAULT_REGION

AssumeRoleを利用している場合はprofileが ''と表示されます。 そ
れ以外のときにprofileが '' と表示される場合は、以下を実行して
ください。

変数の設定
export AWS_DEFAULT_PROFILE=<IAMユーザ名>

1. 事前作業

1.1. ポリシー名の決定

変数の設定
IOT_POLICY_NAME='AWSIoTFullAccess' \
        && echo ${IOT_POLICY_NAME}

同名のポリシーが存在しないことを確認します。

コマンド
aws iot get-policy \
        --policy-name ${IOT_POLICY_NAME}
結果(例)
      An error occurred (ResourceNotFoundException) when calling the GetPolicy operation: Policy default version not found

2. ポリシー作成

変数の設定
FILE_INPUT="${IOT_POLICY_NAME}".json \
        && echo ${FILE_INPUT}
コマンド
cat << EOF > ${FILE_INPUT}
{
        "Version": "2012-10-17",
        "Statement": [{
            "Effect": "Allow",
            "Action": ["iot:*"],
            "Resource": ["*"]
        }]
}
EOF

cat ${FILE_INPUT}

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

コマンド
jsonlint -q ${FILE_INPUT}

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

コマンド
aws iot create-policy \
        --policy-name ${IOT_POLICY_NAME} \
        --policy-document file://${FILE_INPUT}
結果(例)
      {
        "policyName": "AWSIoTFullAccess",
        "policyArn": "arn:aws:iot:ap-northeast-1:XXXXXXXXXX:policy/AWSIoTFullAccess",
        "policyDocument": "{n  "Version": "2012-10-17",n  "Statement": [{n      "Effect": "Allow",n      "Action": ["iot:*"],n      "Resource": ["*"]n  }]n}n",
        "policyVersionId": "1"
      }

3. 事後作業

コマンド
aws iot list-policies 
結果(例)
      {
        "policies": [
          {
              "policyName": "AWSIoTFullAccess",
              "policyArn": "arn:aws:iot:ap-northeast-1:XXXXXXXXXXXX:policy/AWSIoTFullAccess"
          }
        ]
      }
コマンド
aws iot get-policy \
        --policy-name ${IOT_POLICY_NAME}
結果(例)
      {
        "policyName": "AWSIoTFullAccess",
        "policyArn": "arn:aws:iot:ap-northeast-1:XXXXXXXXXXXX:policy/AWSIoTFullAccess",
        "policyDocument": "{"Version":"2012-10-17","Statement":[{"Action":["iot:*"],"Resource":["*"],"Effect":"Allow"}]}",
        "defaultVersionId": "1"
      }
コマンド
aws iot list-policy-principals \
        --policy-name ${IOT_POLICY_NAME}
結果(例)
      {
        "principals": []
      }

完了

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
1