3.5. Cognito IDプールへのIAMロール割り当て¶
作業の目的 [why]¶
IDプール"handson20170904pool"にIAMロール"cognito-identity-handson20170904pool-role"を割り当てます。
課題
(未検証)
作業対象 [what]¶
- Cognito IDプール
標準時間¶
8分
前提条件¶
作業環境条件 [where]¶
本作業は、以下の作業環境で行います。
作業環境条件1: OSとバージョン
Amazon Linuxの以下のバージョンで動作確認済
コマンド:
cat /etc/issue | head -1
結果(例):
Amazon Linux AMI release 2016.09
作業環境条件2: シェルとバージョン
bashの以下のバージョンで動作確認済
コマンド:
bash --version -v | head -1
結果(例):
GNU bash, バージョン 4.2.46(1)-release (x86_64-redhat-linux-gnu)
作業環境条件3: AWS CLIのバージョン
以下のバージョンで動作確認済
- AWS CLI 1.11.117
コマンド:
aws --version
結果(例):
aws-cli/1.11.144 Python/2.7.12 Linux/4.4.11-23.53.amzn1.x86_64 botocore/1.7.2
バージョンが古い場合は最新版に更新しましょう。
コマンド:
sudo -H pip install -U awscli
開始条件¶
作業に必要なモノ・情報 [resource]¶
作業開始には、以下が全て揃っていることが必要です。
リソース1: IDプール名
- IAMロールを適用するIDプールの名称です。
- 今回は"handson20170904pool"とします。
リソース2: IAMロール名
- IDプールに適用するIAMロールの名称です。
- 今回は"cognito-identity-handson20170904pool-role"とします。
タスクの実施¶
0. パラメータの指定¶
まず変数の確認をします。
変数の確認:
cat << ETX 0.a. AWS_DEFAULT_PROFILE: actual: ${AWS_DEFAULT_PROFILE} expect: <Cognitoのフル権限を許可されたプロファイル> 0.1. COGNITO_IDPOOL_NAME: actual: ${COGNITO_IDPOOL_NAME} expect: handson20170904pool 0.2. IAM_ROLE_NAME: actual: ${IAM_ROLE_NAME} expect: cognito-identity-handson20170904pool-role ETX
変数が入っていない、適切でない場合は、それぞれの手順番号について作業を行います。
0.a. プロファイルの指定¶
プロファイルの一覧を確認します。
コマンド:
cat ~/.aws/credentials \ | grep '\[' \ | sed 's/\[//g' | sed 's/\]//g'
結果(例):
iamFull-prjz-mbpr13 <Cognitoのフル権限を許可されたプロファイル>
変数の設定:
export AWS_DEFAULT_PROFILE='<Cognitoのフル権限を許可されたプロファイル>'
0.2. IAMロール名の指定¶
IDプールに割り当てるIAMロール名を指定します。
変数の設定:
IAM_ROLE_NAME='cognito-identity-handson20170904pool-role'
再確認¶
設定されている変数の内容を再確認します。
変数の確認:
cat << ETX 0.a. AWS_DEFAULT_PROFILE: actual: ${AWS_DEFAULT_PROFILE} expect: <Cognitoのフル権限を許可されたプロファイル> 0.1. COGNITO_IDPOOL_NAME: actual: ${COGNITO_IDPOOL_NAME} expect: handson20170904pool 0.2. IAM_ROLE_NAME: actual: ${IAM_ROLE_NAME} expect: cognito-identity-handson20170904pool-role ETX
1. 前処理¶
1.1. 状態確認に必要な情報の取得¶
IDプールIDの取得
コマンド:
COGNITO_IDPOOL_ID=$( \ aws cognito-identity list-identity-pools \ --max-results 60 \ --query "IdentityPools[?IdentityPoolName==\`${COGNITO_IDPOOL_NAME}\`].IdentityPoolId" \ --output text \ ) \ && echo ${COGNITO_IDPOOL_ID}
結果(例):
ap-northeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
1.2. 処理対象の状態確認¶
主処理の実施は、以下の状態であることを前提とします。
前提と異なることが判明した場合、直ちに処理を中止します。
事前条件1: IDプール"handson20170904pool"が存在する。
「IDプール"handson20170904pool"が存在する。」ことを確認します。
コマンド:
aws cognito-identity list-identity-pools \ --max-results 60 \ --query "IdentityPools[?IdentityPoolName==\`${COGNITO_IDPOOL_NAME}\`].IdentityPoolName"
結果(例):
[ "handson20170904pool" ]
事前条件2: IDプール"handson20170904pool"にIAMロール"cognito-identity-handson20170904pool-role"が割り当てられていない。
「IDプール"handson20170904pool"にIAMロール"cognito-identity-handson20170904pool-role"が割り当てられていない。」ことを確認します。
コマンド:
aws cognito-identity get-identity-pool-roles \ --identity-pool-id ${COGNITO_IDPOOL_ID} \ --query 'Roles.unauthenticated'
結果(例):
null
1.3. 主処理に必要な情報の取得¶
コマンド:
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/cognito-identity-handson20170904pool-role
2. 主処理¶
IDプールへのIAMロールの割り当て¶
変数の確認:
cat << ETX COGNITO_IDPOOL_ID: actual: ${COGNITO_IDPOOL_ID} expect: ap-northeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx IAM_ROLE_ARN: actual: ${IAM_ROLE_ARN} expect: arn:aws:iam::XXXXXXXXXXXX:role/cognito-identity-handson20170904pool-role ETX
コマンド:
aws cognito-identity set-identity-pool-roles \ --identity-pool-id ${COGNITO_IDPOOL_ID} \ --roles unauthenticated=${IAM_ROLE_ARN}
結果(例):
(出力なし)
3. 後処理¶
完了条件の確認¶
主処理は、以下を満たしたときに成功したものとします。
完了条件1: IDプール"handson20170904pool"にIAMロール"cognito-identity-handson20170904pool-role"が割り当てられている。
「IDプール"handson20170904pool"にIAMロール"cognito-identity-handson20170904pool-role"が割り当てられている。」ことを確認します。
コマンド:
aws cognito-identity get-identity-pool-roles \ --identity-pool-id ${COGNITO_IDPOOL_ID} \ --query 'Roles.unauthenticated'
結果(例):
"arn:aws:iam::XXXXXXXXXXXX:role/cognito-identity-handson20170904pool-role"