3.2. IAMロールの作成 (cognito-identity: unauthenticated)¶
作業の目的 [why]¶
IAMロール"cognito-identity-handson20170904pool-role"を作成します。
作業対象 [what]¶
- IAMサービス
標準時間¶
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.122
コマンド:
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: 設定ファイル用ディレクトリ
今回は"${HOME}/tmp/conf-iam"を設定ファイル用ディレクトリとします。
ls ${HOME}/tmp/conf-iam
存在しない場合は作成します。
mkdir -p ${HOME}/tmp/conf-iam
リソース2: IAMロール名
- 作成するIAMロールの名称です。
- 今回は"cognito-identity-handson20170904pool-role"とします。
リソース3: プリンシパル
- 作成するIAMロールの利用を許可する対象です。
- 今回は"cognito-identity.amazonaws.com"とします。
リソース4: IDプール名
- 作成するIAMロールを利用するIDプールの名称です。
- 今回は"handson20170904pool"とします。
タスクの実施¶
0. パラメータの指定¶
まず変数の確認をします。
変数の確認:
cat << ETX 0.a. AWS_DEFAULT_PROFILE: actual: ${AWS_DEFAULT_PROFILE} expect: <IAMのフル権限を許可されたプロファイル> 0.1. DIR_CONF: actual: ${DIR_CONF} expect: ${HOME}/tmp/conf-iam 0.2. IAM_ROLE_NAME: actual: ${IAM_ROLE_NAME} expect: cognito-identity-handson20170904pool-role 0.3. IAM_PRINCIPAL: actual: ${IAM_PRINCIPAL} expect: cognito-identity.amazonaws.com 0.4. COGNITO_IDPOOL_NAME: actual: ${COGNITO_IDPOOL_NAME} expect: handson20170904pool ETX
変数が入っていない、適切でない場合は、それぞれの手順番号について作業を行います。
0.a. プロファイルの指定¶
プロファイルの一覧を確認します。
コマンド:
cat ~/.aws/credentials \ | grep '\[' \ | sed 's/\[//g' | sed 's/\]//g'
結果(例):
iamFull-prjz-mbpr13 <IAMのフル権限を許可されたプロファイル>
変数の設定:
export AWS_DEFAULT_PROFILE='<IAMのフル権限を許可されたプロファイル>'
再確認¶
設定されている変数の内容を再確認します。
変数の確認:
cat << ETX 0.a. AWS_DEFAULT_PROFILE: actual: ${AWS_DEFAULT_PROFILE} expect: <IAMのフル権限を許可されたプロファイル> 0.1. DIR_CONF: actual: ${DIR_CONF} expect: ${HOME}/tmp/conf-iam 0.2. IAM_ROLE_NAME: actual: ${IAM_ROLE_NAME} expect: cognito-identity-handson20170904pool-role 0.3. IAM_PRINCIPAL: actual: ${IAM_PRINCIPAL} expect: cognito-identity.amazonaws.com 0.4. COGNITO_IDPOOL_NAME: actual: ${COGNITO_IDPOOL_NAME} expect: handson20170904pool ETX
1. 前処理¶
1.1. 作業対象の状態確認¶
主処理の実施は、以下の状態であることを前提とします。
前提と異なることが判明した場合、直ちに処理を中止します。
事前条件1: 設定ファイル用ディレクトリが存在する。
コマンド:
ls -d ${DIR_CONF}
結果(例):
${HOME}/tmp/conf-iam
事前条件2: IAMロール"cognito-identity-handson20170904pool-role"は存在しない。
「IAMロール"cognito-identity-handson20170904pool-role"は存在しない。」ことを確認します。
コマンド:
aws iam list-roles \ --query "Roles[?RoleName == \`${IAM_ROLE_NAME}\`].RoleName"
結果(例):
[]
事前条件3: IAMロールを利用するIDプール"handson20170904pool"が存在する。
「IAMロールを利用するIDプール"handson20170904pool"が存在する。」ことを確認します。
コマンド:
aws cognito-identity list-identity-pools \ --max-results 60 \ --query "IdentityPools[?IdentityPoolName==\`${COGNITO_IDPOOL_NAME}\`].IdentityPoolName"
結果(例):
[ "handson20170904pool" ]
1.2. 主処理に必要な情報の取得¶
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.3. assumeロールポリシドキュメントの作成¶
変数の設定:
FILE_INPUT="${DIR_CONF}/${IAM_ROLE_NAME}.json" \ && echo ${FILE_INPUT}
変数の確認:
cat << ETX FILE_INPUT: actual: ${FILE_INPUT} expect: ${HOME}/tmp/conf-iam/cognito-identity-handson20170904pool-role.json IAM_PRINCIPAL: actual: ${IAM_PRINCIPAL} expect: cognito-identity.amazonaws.com COGNITO_IDPOOL_ID: actual: ${COGNITO_IDPOOL_ID} expect: ap-northeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx ETX
コマンド:
cat << EOF > ${FILE_INPUT} { "Version": "2008-10-17", "Statement": [ { "Action": "sts:AssumeRoleWithWebIdentity", "Principal": { "Federated": "${IAM_PRINCIPAL}" }, "Effect": "Allow", "Condition": { "StringEquals": { "cognito-identity.amazonaws.com:aud": "${COGNITO_IDPOOL_ID}" }, "ForAnyValue:StringLike": { "cognito-identity.amazonaws.com:amr": "unauthenticated" } }, "Sid": "" } ] } EOF cat ${FILE_INPUT}
JSONファイルを作成したら、フォーマットが壊れてないか必ず確認します。
コマンド:
jsonlint -q ${FILE_INPUT}
エラーが出力されなければOKです。
2. 主処理¶
IAMロールの作成¶
変数の確認:
cat << ETX IAM_ROLE_NAME: actual: ${IAM_ROLE_NAME} expect: cognito-identity-handson20170904pool-role FILE_INPUT: actual: ${FILE_INPUT} expect: ${HOME}/tmp/conf-iam/cognito-identity-handson20170904pool-role.json ETX
コマンド:
aws iam create-role \ --role-name ${IAM_ROLE_NAME} \ --assume-role-policy-document file://${FILE_INPUT}
結果(例):
{ "Role": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Action": "sts:AssumeRoleWithWebIdentity", "Principal": { "Federated": "cognito-identity.amazonaws.com" }, "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" } }, "Sid": "" } ] }, "RoleId": "AROAxxxxxxxxxxxxxxxxx", "CreateDate": "2017-09-04T01:23:45.678Z", "RoleName": "cognito-identity-handson20170904pool-role", "Path": "/", "Arn": "arn:aws:iam::XXXXXXXXXXXX:role/cognito-identity-handson20170904pool-role" } }
3. 後処理¶
完了条件の確認¶
主処理は、以下を満たしたときに成功したものとします。
完了条件1: IAMロール"cognito-identity-handson20170904pool-role"が存在する。
「IAMロール"cognito-identity-handson20170904pool-role"が存在する。」ことを確認します。
コマンド:
aws iam list-roles \ --query "Roles[?RoleName == \`${IAM_ROLE_NAME}\`].RoleName"
結果(例):
[ "cognito-identity-handson20170904pool-role" ]
完了条件2: IAMロールを利用する"cognito-identity.amazonaws.com"がプリンシパルに指定されている。
「IAMロールを利用する"cognito-identity.amazonaws.com"がプリンシパルに指定されている。」ことを確認します。
コマンド:
aws iam get-role \ --role-name ${IAM_ROLE_NAME} \ --query 'Role.AssumeRolePolicyDocument.Statement[].Principal.Federated'
結果(例):
[ "cognito-identity.amazonaws.com" ]