Help us understand the problem. What is going on with this article?

[JAWS-UG CLI] IAM # S3 put用IAMロール

More than 3 years have passed since last update.

AWS CLIを利用して、S3 put用のIAMロールの作成をしてみます。

前提条件

Cognito (Identity)への権限

  • Cognito (Identity)に対してフル権限があること。

AWS CLIのバージョン

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

    • AWS CLI 1.8.3
コマンド
aws --version
結果(例)
      aws-cli/1.8.3 Python/2.7.5 Darwin/13.4.0

0. 準備

0.1. 変数の確認

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

変数の確認
aws configure list
結果(例)
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile       iamFull-prjZ-mbp13iamFull-prjZ-mbp13              env    AWS_DEFAULT_PROFILE
      access_key     ****************XXXX shared-credentials-file
      secret_key     ****************XXXX shared-credentials-file
          region                eu-west-1              env    AWS_DEFAULT_REGION

0.2. Cognito ID Pool IDの指定

変数の設定(例)
COGNITO_IDPOOL_ID='ap-northeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'

0.3. S3バケット名の指定

変数の設定(例)
S3_BUCKET_NAME=<バケット名>

1. 事前作業

1.1. IAMロール名の決定

変数の設定
IAM_ROLE_NAME='Cognito_s3putUnauth_Role'

同じ名前のIAMロールが存在しないことを確認します。

コマンド
aws iam get-role \
         --role-name ${IAM_ROLE_NAME}
結果(例)
      A client error (NoSuchEntity) occurred when calling the GetRole operation: The role with name ecsInstanceRole cannot be found.

1.2. assumeロールポリシドキュメントの作成

変数の設定
FILE_ROLE_DOC="${IAM_ROLE_NAME}.json" \
        && echo ${FILE_ROLE_DOC}
コマンド
cat << EOF >> ${FILE_ROLE_DOC}
{
        "Version": "2008-10-17",
        "Statement": [
          {
            "Action": "sts:AssumeRoleWithWebIdentity",
            "Principal": {
              "Federated": "cognito-identity.amazonaws.com"
            },
            "Effect": "Allow",
            "Condition": {
              "StringEquals": {
                "cognito-identity.amazonaws.com:aud": "${COGNITO_IDPOOL_ID}"
              },
              "ForAnyValue:StringLike": {
                "cognito-identity.amazonaws.com:amr": "unauthenticated"
              }
            },
            "Sid": ""
          }
        ]
}
EOF

cat ${FILE_ROLE_DOC}

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

コマンド
jsonlint -q ${FILE_ROLE_DOC}

2. IAMロールの作成

2.1. IAMロールの作成

変数の確認
cat << ETX

        IAM_ROLE_NAME: ${IAM_ROLE_NAME}
        FILE_ROLE_DOC: ${FILE_ROLE_DOC}

ETX
コマンド
aws iam create-role \
        --role-name ${IAM_ROLE_NAME} \
        --assume-role-policy-document file://${FILE_ROLE_DOC}
結果(例)

      {
        "Role": {
          "AssumeRolePolicyDocument": {
              "Version": "2008-10-17",
              "Statement": [
                  {
                      "Action": "sts:AssumeRoleWithWebIdentity",
                      "Sid": "",
                      "Effect": "Allow",
                      "Condition": {
                          "StringEquals": {
                              "cognito-identity.amazonaws.com:aud": "ap-northeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
                          },
                          "ForAnyValue:StringLike": {
                              "cognito-identity.amazonaws.com:amr": "unauthenticated"
                          }
                      },
                      "Principal": {
                          "Federated": "cognito-identity.amazonaws.com"
                      }
                  }
              ]
          },
          "RoleId": "AROAXXXXXXXXXXXXXXXXX",
          "CreateDate": "2015-09-28T08:12:38.002Z",
          "RoleName": "Cognito_s3putUnauth_Role",
          "Path": "/",
          "Arn": "arn:aws:iam::XXXXXXXXXXXX:role/Cognito_s3putUnauth_Role"
        }
      }

2.2. IAMロールの確認

コマンド
aws iam get-role \
         --role-name ${IAM_ROLE_NAME}

3. ロールポリシ (cognito-sync)

3.1. IAMロールポリシーの作成

変数の設定
IAM_ROLE_POLICY_NAME='oneClick_Cognito_s3putUnauth_Role'
FILE_ROLE_POLICY_DOC="${IAM_ROLE_POLICY_NAME}.json" \
        && echo ${FILE_ROLE_POLICY_DOC}
コマンド
cat << EOF > ${FILE_ROLE_POLICY_DOC}
{
        "Version": "2012-10-17",
        "Statement": [
          {
            "Effect": "Allow",
            "Action": [
              "mobileanalytics:PutEvents",
              "cognito-sync:*"
            ],
            "Resource": [
              "*"
            ]
          }
        ]
}
EOF

cat ${FILE_ROLE_POLICY_DOC}

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

コマンド
jsonlint -q ${FILE_ROLE_POLICY_DOC}

3.2. ロールポリシーの適用

ロールポリシーをIAMロールに適用します。

変数の確認
cat << ETX

        IAM_ROLE_NAME:        ${IAM_ROLE_NAME}
        IAM_ROLE_POLICY_NAME: ${IAM_ROLE_POLICY_NAME}
        FILE_ROLE_POLICY_DOC: ${FILE_ROLE_POLICY_DOC}

ETX
変数の確認
aws iam put-role-policy \
        --role-name ${IAM_ROLE_NAME} \
        --policy-name ${IAM_ROLE_POLICY_NAME} \
        --policy-document file://${FILE_ROLE_POLICY_DOC}

4. ロールポリシ (s3put)

4.1. IAMロールポリシーの作成

変数の設定
IAM_ROLE_POLICY_NAME='s3put'
FILE_ROLE_POLICY_DOC="${IAM_ROLE_POLICY_NAME}.json" \
        && echo ${FILE_ROLE_POLICY_DOC}
コマンド
cat << EOF > ${FILE_ROLE_POLICY_DOC}
{
        "Version": "2012-10-17",
        "Statement": [
          {
              "Sid": "",
              "Action": [
                  "s3:PutObject",
                  "s3:PutObjectAcl"
              ],
              "Effect": "Allow",
              "Resource": "arn:aws:s3:::${S3_BUCKET_NAME}/*"
          }
        ]
}
EOF

cat ${FILE_ROLE_POLICY_DOC}

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

コマンド
jsonlint -q ${FILE_ROLE_POLICY_DOC}

4.2. ロールポリシーの適用

ロールポリシーをIAMロールに適用します。

変数の確認
cat << ETX

        IAM_ROLE_NAME:        ${IAM_ROLE_NAME}
        IAM_ROLE_POLICY_NAME: ${IAM_ROLE_POLICY_NAME}
        FILE_ROLE_POLICY_DOC: ${FILE_ROLE_POLICY_DOC}

ETX
変数の確認
aws iam put-role-policy \
        --role-name ${IAM_ROLE_NAME} \
        --policy-name ${IAM_ROLE_POLICY_NAME} \
        --policy-document file://${FILE_ROLE_POLICY_DOC}

5. 事後作業

5.1. IAMロールの確認

コマンド
aws iam get-role \
         --role-name ${IAM_ROLE_NAME}
結果(例)
      {
              "AssumeRolePolicyDocument": {
                  "Version": "2012-10-17",
                  "Statement": [
                      {
                          "Action": "sts:AssumeRoleWithWebIdentity",
                          "Principal": {
                              "Federated": "cognito-identity.amazonaws.com"
                          },
                          "Effect": "Allow",
                          "Condition": {
                              "StringEquals": {
                                  "cognito-identity.amazonaws.com:aud": "us-east-1:48b8f559-9c64-40e3-b38f-9860fc0240df"
                              },
                              "ForAnyValue:StringLike": {
                                  "cognito-identity.amazonaws.com:amr": "unauthenticated"
                              }
                          },
                          "Sid": ""
                      }
                  ]
              },
              "RoleId": "AROAI24WZFOE4TPB4D46A",
              "CreateDate": "2015-09-14T21:57:57Z",
              "RoleName": "Cognito_s3putUnauth_Role",
              "Path": "/",
              "Arn": "arn:aws:iam::XXXXXXXXXXXX:role/Cognito_s3putUnauth_Role"
      }

5.2. ロールポリシの確認

コマンド
aws iam list-role-policies   --role-name ${IAM_ROLE_NAME}
結果
      {
        "PolicyNames": [
          "oneClick_Cognito_s3putUnauth_Role",
          "s3put"
        ]
      }
コマンド
IAM_ROLE_POLICY_NAME='oneClick_Cognito_s3putUnauth_Role'
コマンド
aws iam get-role-policy \
        --role-name ${IAM_ROLE_NAME} \
        --policy-name ${IAM_ROLE_POLICY_NAME}
結果(例)
      aws iam get-role-policy \
      {
        "RoleName": "Cognito_s3putUnauth_Role",
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
              {
                  "Action": [
                      "mobileanalytics:PutEvents",
                      "cognito-sync:*"
                  ],
                  "Resource": [
                      "*"
                  ],
                  "Effect": "Allow"
              }
          ]
        },
        "PolicyName": "oneClick_Cognito_s3putUnauth_Role"
      }
コマンド
IAM_ROLE_POLICY_NAME='s3put'
コマンド
aws iam get-role-policy \
        --role-name ${IAM_ROLE_NAME} \
        --policy-name ${IAM_ROLE_POLICY_NAME}
結果(例)
      {
        "RoleName": "Cognito_s3putUnauth_Role",
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
              {
                  "Action": [
                      "s3:PutObject",
                      "s3:PutObjectAcl"
                  ],
                  "Resource": "arn:aws:s3:::example/*",
                  "Effect": "Allow",
                  "Sid": ""
              }
          ]
        },
        "PolicyName": "s3put"
      }

マネジメントコンソール上は、"信頼関係"に cognito-identity.amazonaws.com が表示されています。

5.3. IAMロールのARN

コマンド
IAM_ROLE_ARN=$( \
  aws iam get-role \
    --role-name ${IAM_ROLE_NAME} \
    --query 'Role.Arn' \
    --output text \
) \
  && echo ${IAM_ROLE_ARN}

5.4.

コマンド
aws cognito-identity set-identity-pool-roles \
  --identity-pool-id ${COGNITO_IDPOOL_ID} \
  --roles unauthenticated=${IAM_ROLE_ARN}
コマンド
aws cognito-identity get-identity-pool-roles \
  --identity-pool-id ${COGNITO_IDPOOL_ID} 
結果(例)
{
    "IdentityPoolId": "ap-northeast-1:f08b5f56-4132-4762-ab95-4188346e4b8b", 
    "Roles": {
        "unauthenticated": "arn:aws:iam::XXXXXXXXXXXX:role/Cognito_s3putUnauth_Role"
    }
}

完了

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした