6. Cognito IDプールへのIAMロール割り当て (handson-cli-cognito-identity-idpool)

作業の目的 [why]

Cognito Identityプール名"handson-cli-cognito-identity-idpool"IAMロール"handson-cli-cognito-identity-role"を割り当てます。

完了条件/事前条件 [設計者用情報]

完了条件 [after]

主処理は、以下を満たしたときに成功したものとします。

完了条件1

Cognito Identityプール名"handson-cli-cognito-identity-idpool"にIAMロール"handson-cli-cognito-identity-role"が割り当てられている。

事前条件 [before]

主処理の実施は、以下の状態であることを前提とします。

事前条件1

Cognito Identityプール名"handson-cli-cognito-identity-idpool"が存在する。

事前条件2

Cognito Identityプール名"handson-cli-cognito-identity-idpool"にIAMロール"handson-cli-cognito-identity-role"が割り当てられていない。

作業対象 [what]

  • Cognito IDプール

標準時間(合計)

8分

パラメータ設定

パラメータ設定の標準時間

2分

作業に必要なモノ・情報 [resource]

作業開始には、以下が全て揃っていることが必要です。

作業開始には、以下が全て揃っていることが必要です。

リソース1: IDプール名

  • IAMロールを適用するIDプールの名称です。

  • 今回は"handson-cli-cognito-identity-idpool"とします。

リソース2: IAMロール名

  • IDプールに適用するIAMロールの名称です。

  • 今回は"handson-cli-cognito-identity-role"とします。

パラメータの指定

作業に必要なパラメータを変数に格納をします。

0.0. リージョンの指定

変数の設定

export AWS_DEFAULT_REGION='ap-northeast-1'

0.1. IDプール名の指定

IDプール名を指定します。

変数の設定:

COGNITO_IDENTITY_POOL_NAME='handson-cli-cognito-identity-idpool'

0.2. IAMロール名の指定

IDプールに割り当てるIAMロール名を指定します。

変数の設定:

IAM_ROLE_NAME='handson-cli-cognito-identity-role'

パラメータの保存

設定されている変数の保存先となるファイル名を指定します。

変数の設定:

DIR_PARAMETER="${HOME}/tmp/parameter-handson-cli-cognito-identity"
FILE_PARAMETER="${DIR_PARAMETER}/$(date +%Y-%m-%d)-cognito_identity-pool_roles-update.env" \
  && echo ${FILE_PARAMETER}

結果(例):

${HOME}/tmp/parameter-handson-cli-cognito-identity/2019-11-19-cognito_identity-pool_roles-update.env

各変数に正しいパラメータ値が格納されていることを確認しながら保存します。

変数の確認:

cat << EOF > ${FILE_PARAMETER}

  # 0.0. AWS_DEFAULT_REGION:"ap-northeast-1"
         AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"

  # 0.1. COGNITO_IDENTITY_POOL_NAME:"handson-cli-cognito-identity-idpool"
         COGNITO_IDENTITY_POOL_NAME="${COGNITO_IDENTITY_POOL_NAME}"
  # 0.2. IAM_ROLE_NAME:"handson-cli-cognito-identity-role"
         IAM_ROLE_NAME="${IAM_ROLE_NAME}"

EOF

cat ${FILE_PARAMETER}

下段の変数が入っていない、もしくは上段と同等の値が入っていない場合は、それぞれの手順番号に戻って変数の設定を行います。

タスクの実施

タスク標準時間

6分

1. 前処理

1.1. 状態確認に必要な情報の取得

IDプールIDの取得

コマンド:

COGNITO_IDENTITY_POOL_ID=$( \
  aws cognito-identity list-identity-pools \
    --max-results 60 \
    --query "IdentityPools[?IdentityPoolName==\`${COGNITO_IDENTITY_POOL_NAME}\`].IdentityPoolId" \
    --output text \
) \
  && echo ${COGNITO_IDENTITY_POOL_ID}

結果(例):

ap-northeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

1.2. 処理対象の状態確認

主処理の実施は、以下の状態であることを前提とします。

前提と異なることが判明した場合、直ちに処理を中止します。

事前条件1: Cognito Identityプール名"handson-cli-cognito-identity-idpool"が存在する。

「Cognito Identityプール名"handson-cli-cognito-identity-idpool"が存在する。」ことを確認します。

コマンド:

aws cognito-identity list-identity-pools \
  --max-results 60 \
  --query "IdentityPools[?IdentityPoolName==\`${COGNITO_IDENTITY_POOL_NAME}\`].IdentityPoolName" \
  --output text

結果(例):

handson-cli-cognito-identity-idpool

事前条件2: Cognito Identityプール名"handson-cli-cognito-identity-idpool"にIAMロール"handson-cli-cognito-identity-role"が割り当てられていない。

「Cognito Identityプール名"handson-cli-cognito-identity-idpool"にIAMロール"handson-cli-cognito-identity-role"が割り当てられていない。」ことを確認します。

コマンド:

aws cognito-identity get-identity-pool-roles \
  --identity-pool-id ${COGNITO_IDENTITY_POOL_ID} \
  --query 'Roles.unauthenticated' \
  --output text

結果(例):

None

1.3. 主処理に必要な情報の取得

IAMロールARNの取得

コマンド:

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/handson-cli-cognito-identity-role

2. 主処理

IDプールへのIAMロールの割り当て

変数の確認:

cat << ETX

  # COGNITO_IDENTITY_POOL_ID:"ap-northeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    COGNITO_IDENTITY_POOL_ID="${COGNITO_IDENTITY_POOL_ID}"
  # IAM_ROLE_ARN:"arn:aws:iam::XXXXXXXXXXXX:role/handson-cli-cognito-identity-role"
    IAM_ROLE_ARN="${IAM_ROLE_ARN}"

ETX

コマンド:

aws cognito-identity set-identity-pool-roles \
  --identity-pool-id ${COGNITO_IDENTITY_POOL_ID} \
  --roles unauthenticated=${IAM_ROLE_ARN}

結果(例):

(出力なし)

3. 後処理

完了条件の確認

主処理は、以下を満たしたときに成功したものとします。

完了条件1: Cognito Identityプール名"handson-cli-cognito-identity-idpool"にIAMロール"handson-cli-cognito-identity-role"が割り当てられている。

「Cognito Identityプール名"handson-cli-cognito-identity-idpool"にIAMロール"handson-cli-cognito-identity-role"が割り当てられている。」ことを確認します。

コマンド:

aws cognito-identity get-identity-pool-roles \
  --identity-pool-id ${COGNITO_IDENTITY_POOL_ID} \
  --query 'Roles.unauthenticated' \
  --output text

結果(例):

"arn:aws:iam::XXXXXXXXXXXX:role/handson-cli-cognito-identity-role"

完了