LoginSignup
1
1

More than 5 years have passed since last update.

[JAWS-UG CLI] IAM #50 IAMポリシーの作成 (AWS IoT: Logging Put)

Last updated at Posted at 2016-11-17

AWS CLIを利用して、AWS IoTからのロギングに必要なIAMポリシを作成してみます。

前提条件

IAMへの権限

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

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. 準備

変数の確認

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

変数の確認
aws configure list
結果(例)
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile       lambdaFull-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

1. 事前作業

1.1. AWSアカウントIDの取得

コマンド
AWS_ID=$( \
        aws sts get-caller-identity \
          --query 'Account' \
          --output text \
) \
        && echo ${AWS_ID}
結果(例)
      XXXXXXXXXXXX

1.2. IAMポリシー名の決定

ポリシー名を決めます。

変数の設定
IAM_POLICY_NAME='AWSIoTLoggingPutRolePolicy'

同じ名前のポリシーが無いことを確認します。

コマンド
aws iam get-policy \
        --policy-arn arn:aws:iam::${AWS_ID}:policy/${IAM_POLICY_NAME}
結果(例)
      A client error (NoSuchEntity) occurred when calling the GetPolicy operation: Policy arn:aws:iam::XXXXXXXXXXXX:policy/AWSIoTLoggingPutRolePolicydoes not exist.

2. IAMポリシードキュメントの作成

ポリシードキュメントのファイル名を決めます。

変数の設定
FILE_INPUT="${IAM_POLICY_NAME}.json" \
          && echo ${FILE_INPUT}

ポリシードキュメントを作成します。

コマンド
cat << EOF > ${FILE_INPUT}
{
        "Version": "2012-10-17",
        "Statement": [
          {
              "Effect": "Allow",
              "Action": [
                  "logs:CreateLogGroup",
                  "logs:CreateLogStream",
                  "logs:PutLogEvents",
                  "logs:PutMetricFilter",
                  "logs:PutRetentionPolicy"
               ],
              "Resource": [
                  "*"
              ]
          }
        ]
}
EOF

cat ${FILE_INPUT}

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

コマンド
jsonlint -q ${FILE_INPUT}

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

3. IAMポリシーの作成

変数の確認
cat << ETX

        IAM_POLICY_NAME: ${IAM_POLICY_NAME}
        FILE_INPUT:      ${FILE_INPUT}

ETX
コマンド
aws iam create-policy \
        --policy-name ${IAM_POLICY_NAME} \
        --policy-document file://${FILE_INPUT}
結果(例)
      {
        "Policy": {
          "PolicyName": "AWSIoTLoggingPutRolePolicy",
          "CreateDate": "2016-11-17T01:23:45.678Z",
          "AttachmentCount": 0,
          "IsAttachable": true,
          "PolicyId": "ANPAXXXXXXXXXXXXXXXXX",
          "DefaultVersionId": "v1",
          "Path": "/",
          "Arn": "arn:aws:iam:::XXXXXXXXXXXXpolicy/AWSIoTLoggingPutRolePolicy",
          "UpdateDate": "2016-11-17T01:23:45.678Z"
        }
      }

4. 事後作業

IAMポリシーの確認

ARNを取得します。

変数の設定
IAM_POLICY_ARN=$( \
        aws iam list-policies \
          --max-items 1000 \
          --query "Policies[?PolicyName==\`${IAM_POLICY_NAME}\`].Arn" \
          --output text \
) \
        && echo "${IAM_POLICY_ARN}"
結果(例)
      arn:aws:iam::XXXXXXXXXXXX:policy/AWSIoTLoggingPutRolePolicy

ポリシのバージョンを取得します。

コマンド
IAM_POLICY_VERSION=$( \
        aws iam list-policies \
          --max-items 1000 \
          --query "Policies[?PolicyName==\`${IAM_POLICY_NAME}\`].DefaultVersionId" \
          --output text \
) \
        && echo ${IAM_POLICY_VERSION}
結果(例)
      v1

ポリシの内容を見てみましょう。

コマンド
aws iam get-policy-version \
        --policy-arn ${IAM_POLICY_ARN} \
        --version-id ${IAM_POLICY_VERSION}
結果(例)
      {
        "PolicyVersion": {
          "CreateDate": "2016-11-14T10:47:42Z",
          "VersionId": "v1",
          "Document": {
              "Version": "2012-10-17",
              "Statement": [
                  {
                      "Action": [
                          "logs:CreateLogGroup",
                          "logs:CreateLogStream",
                          "logs:PutLogEvents",
                          "logs:PutMetricFilter",
                          "logs:PutRetentionPolicy"
                      ],
                      "Resource": [
                          "*"
                      ],
                      "Effect": "Allow"
                  }
              ]
          },
          "IsDefaultVersion": true
        }
      }

完了

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