5.1. AssumeRoleドキュメントの作成 (handson-cli-cognito-identity-role)

作業の目的 [why]

ロール"handson-cli-cognito-identity-role"の作成で利用するAssumeRoleドキュメントを作成します。

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

完了条件 [after]

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

完了条件1

Assumeロールドキュメント"${HOME}/environment/conf-handson-cli-cognito-identity/handson-cli-cognito-identity-role.json"が存在する。

事前条件 [before]

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

事前条件1

Assumeロールドキュメント用ディレクトリ"${HOME}/environment/conf-handson-cli-cognito-identity"が存在する。

事前条件2

Assumeロールドキュメント"${HOME}/environment/conf-handson-cli-cognito-identity/handson-cli-cognito-identity-role.json"が存在しない。

事前条件3

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

作業対象 [what]

  • IAMサービス

標準時間(合計)

8分

パラメータ設定

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

2分

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

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

リソース1: AssumeRoleドキュメント用ディレクトリ

  • 今回は"${HOME}/environment/conf-handson-cli-cognito-identity"をAssumeRoleドキュメント用ディレクトリとします。

ディレクトリが存在することを確認します。

コマンド:

ls -d ${HOME}/environment/conf-handson-cli-cognito-identity

結果(例:存在する場合):

${HOME}/environment/conf-handson-cli-cognito-identity

存在しない場合は作成します。

コマンド:

mkdir -p ${HOME}/environment/conf-handson-cli-cognito-identity

リソース2: ロール名

  • AssumeRoleドキュメントの作成対象となるロール名です。

  • ここでは、AssumeRoleドキュメントのファイル名で使用します。

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

リソース3: assumeロールポリシドキュメントファイル名

  • ロールの作成に利用するassumeロールポリシドキュメントのファイル名です。

  • 今回は"${HOME}/environment/conf-handson-cli-cognito-identity/handson-cli-cognito-identity-role.json"とします。

リソース4: Cognito Identity プール名

  • 作成するロールを利用するCognito Identity プール名です。

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

パラメータの指定

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

0.0. リージョンの指定

変数の設定

export AWS_DEFAULT_REGION='ap-northeast-1'

0.1. AssumeRoleドキュメントディレクトリの指定

AssumeRoleドキュメントディレクトリを指定します。

変数の設定:

DIR_IAM_ROLE_DOC="${HOME}/environment/conf-handson-cli-cognito-identity"

0.2. IAMロール名の指定

IAMロール名を指定します。

変数の設定:

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

0.3. assumeロールポリシドキュメントファイル名の指定

assumeロールポリシドキュメントのファイル名を指定します。

変数の設定:

FILE_IAM_ROLE_DOC="${DIR_IAM_ROLE_DOC}/${IAM_ROLE_NAME}.json" \
  && echo ${FILE_IAM_ROLE_DOC}

結果(例):

${HOME}/environment/conf-handson-cli-cognito-identity/handson-cli-cognito-identity-role.json

0.4. IDプール名の指定

変数の設定:

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

パラメータの保存

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

変数の設定:

DIR_PARAMETER="${HOME}/tmp/parameter-handson-cli-cognito-identity"
FILE_PARAMETER="${DIR_PARAMETER}/$(date +%Y-%m-%d)-iam-role_document-create-expand-cognito-identity-AssumeRoleWithWebIdentity-unauthenticated.env" \
  && echo ${FILE_PARAMETER}

結果(例):

${HOME}/tmp/parameter-handson-cli-cognito-identity/2019-11-19-iam-role_document-create-expand-cognito-identity-AssumeRoleWithWebIdentity-unauthenticated.env

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

変数の確認:

cat << EOF > ${FILE_PARAMETER}

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

  # 0.1. DIR_IAM_ROLE_DOC:"${HOME}/environment/conf-handson-cli-cognito-identity"
         DIR_IAM_ROLE_DOC="${DIR_IAM_ROLE_DOC}"
  # 0.2. IAM_ROLE_NAME:"handson-cli-cognito-identity-role"
         IAM_ROLE_NAME="${IAM_ROLE_NAME}"
  # 0.3. FILE_IAM_ROLE_DOC:"${HOME}/environment/conf-handson-cli-cognito-identity/handson-cli-cognito-identity-role.json"
         FILE_IAM_ROLE_DOC="${FILE_IAM_ROLE_DOC}"
  # 0.4. COGNITO_IDENTITY_POOL_NAME:"handson-cli-cognito-identity-idpool"
         COGNITO_IDENTITY_POOL_NAME="${COGNITO_IDENTITY_POOL_NAME}"

EOF
cat ${FILE_PARAMETER}

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

タスクの実施

タスク標準時間

6分

1. 前処理

1.1. 処理対象の状態確認

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

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

事前条件1: Assumeロールドキュメント用ディレクトリ"${HOME}/environment/conf-handson-cli-cognito-identity"が存在する。

「Assumeロールドキュメント用ディレクトリ"${HOME}/environment/conf-handson-cli-cognito-identity"が存在する。」ことを確認します。

コマンド:

ls -d ${DIR_IAM_ROLE_DOC}

結果(例):

${HOME}/environment/conf-handson-cli-cognito-identity

事前条件2: Assumeロールドキュメント"${HOME}/environment/conf-handson-cli-cognito-identity/handson-cli-cognito-identity-role.json"が存在しない。

「Assumeロールドキュメント"${HOME}/environment/conf-handson-cli-cognito-identity/handson-cli-cognito-identity-role.json"が存在しない。」ことを確認します。

コマンド:

! ls ${FILE_IAM_ROLE_DOC}

結果(例):

ls: ${HOME}/environment/conf-handson-cli-cognito-identity/handson-cli-cognito-identity-role.json No such file or directory

事前条件3: 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

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

Cognito Identiryプール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

2. 主処理

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

変数の設定:

FILE_IAM_ROLE_DOC="${DIR_IAM_ROLE_DOC}/${IAM_ROLE_NAME}.json" \
  && echo ${FILE_IAM_ROLE_DOC}

変数の確認:

cat << ETX

  # FILE_IAM_ROLE_DOC:"${HOME}/environment/conf-handson-cli-cognito-identity/handson-cli-cognito-identity-role.json"
    FILE_IAM_ROLE_DOC="${FILE_IAM_ROLE_DOC}"
  # COGNITO_IDENTITY_POOL_ID:"ap-northeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
    COGNITO_IDENTITY_POOL_ID="${COGNITO_IDENTITY_POOL_ID}"

ETX

コマンド:

cat << EOF > ${FILE_IAM_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_IDENTITY_POOL_ID}"
        },
        "ForAnyValue:StringLike": {
          "cognito-identity.amazonaws.com:amr": "unauthenticated"
        }
      },
      "Sid": ""
    }
  ]
}
EOF

cat ${FILE_IAM_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": "ap-northeast-1:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
        },
        "ForAnyValue:StringLike": {
          "cognito-identity.amazonaws.com:amr": "unauthenticated"
        }
      },
      "Sid": ""
    }
  ]
}

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

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

コマンド:

jsonlint -q ${FILE_IAM_ROLE_DOC}

結果(例):

(出力なし)

3. 後処理

完了条件の確認

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

完了条件1: Assumeロールドキュメント"${HOME}/environment/conf-handson-cli-cognito-identity/handson-cli-cognito-identity-role.json"が存在する。

「Assumeロールドキュメント"${HOME}/environment/conf-handson-cli-cognito-identity/handson-cli-cognito-identity-role.json"が存在する。」ことを確認します。

コマンド:

ls ${FILE_IAM_ROLE_DOC}

結果(例):

${HOME}/environment/conf-handson-cli-cognito-identity/handson-cli-cognito-identity-role.json

完了