IAMロール"handson-cli-ssm-session-manager-role"を作成します。
手順に必要な設定値を変数に格納をします。
1. IAMロール名
IAMロール名を指定します。
変数の設定: IAM_ROLE_NAME='handson-cli-ssm-session-manager-role'
変数の設定:
IAM_ROLE_NAME='handson-cli-ssm-session-manager-role'
2. IAMロールパス
IAMロールパスを指定します。
変数の設定: IAM_ROLE_PATH='/handson-cli/'
IAM_ROLE_PATH='/handson-cli/'
3. 信頼ポリシードキュメントファイル
信頼ポリシードキュメント用ディレクトリを指定します。
変数の設定: DIR_IAM_ROLE_DOC="${HOME}/environment/conf-handson-cli-ssm-session-manager"
DIR_IAM_ROLE_DOC="${HOME}/environment/conf-handson-cli-ssm-session-manager"
信頼ポリシードキュメント名を指定します。
変数の設定: IAM_ROLE_DOC_NAME='handson-cli-ssm-session-manager-role'
IAM_ROLE_DOC_NAME='handson-cli-ssm-session-manager-role'
信頼ポリシードキュメントのファイル名を指定します。
変数の設定: FILE_IAM_ROLE_DOC="${DIR_IAM_ROLE_DOC}/${IAM_ROLE_DOC_NAME}.json" \ && echo ${FILE_IAM_ROLE_DOC} 結果(例): ${HOME}/environment/conf-handson-cli-ssm-session-manager/handson-cli-ssm-session-manager-role.json
FILE_IAM_ROLE_DOC="${DIR_IAM_ROLE_DOC}/${IAM_ROLE_DOC_NAME}.json" \ && echo ${FILE_IAM_ROLE_DOC}
結果(例):
${HOME}/environment/conf-handson-cli-ssm-session-manager/handson-cli-ssm-session-manager-role.json
各変数に正しい設定値が格納されていることを確認しながら保存します。
変数の確認: cat << END # 1. IAM_ROLE_NAME:"handson-cli-ssm-session-manager-role" IAM_ROLE_NAME="${IAM_ROLE_NAME}" # 2. IAM_ROLE_PATH:"/handson-cli/" IAM_ROLE_PATH="${IAM_ROLE_PATH}" # 3. FILE_IAM_ROLE_DOC:"${HOME}/environment/conf-handson-cli-ssm-session-manager/handson-cli-ssm-session-manager-role.json" FILE_IAM_ROLE_DOC="${FILE_IAM_ROLE_DOC}" END
変数の確認:
cat << END # 1. IAM_ROLE_NAME:"handson-cli-ssm-session-manager-role" IAM_ROLE_NAME="${IAM_ROLE_NAME}" # 2. IAM_ROLE_PATH:"/handson-cli/" IAM_ROLE_PATH="${IAM_ROLE_PATH}" # 3. FILE_IAM_ROLE_DOC:"${HOME}/environment/conf-handson-cli-ssm-session-manager/handson-cli-ssm-session-manager-role.json" FILE_IAM_ROLE_DOC="${FILE_IAM_ROLE_DOC}" END
下段の変数が入っていない、もしくは上段と同等の値が入っていない場合は、それぞれの手順番号に戻って変数の設定を行います。
IAMロールを作成します。
変数の確認: cat << END # IAM_ROLE_NAME:"handson-cli-ssm-session-manager-role" IAM_ROLE_NAME="${IAM_ROLE_NAME}" # IAM_ROLE_PATH:"/handson-cli/" IAM_ROLE_PATH="${IAM_ROLE_PATH}" # FILE_IAM_ROLE_DOC:"${HOME}/environment/conf-handson-cli-ssm-session-manager/handson-cli-ssm-session-manager-role.json" FILE_IAM_ROLE_DOC="${FILE_IAM_ROLE_DOC}" END コマンド: aws iam create-role \ --role-name ${IAM_ROLE_NAME} \ --path "${IAM_ROLE_PATH}" \ --assume-role-policy-document file://${FILE_IAM_ROLE_DOC} 結果(例): { "Role": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Action": "sts:AssumeRole", "Sid": "", "Effect": "Allow", "Principal": { "Service": "ec2.amazonaws.com" } } ] }, "RoleId": "AROAxxxxxxxxxxxxxxxxx", "CreateDate": "2021-04-09T01:23:45.678Z", "RoleName": "handson-cli-ssm-session-manager-role", "Path": "/", "Arn": "arn:aws:iam::XXXXXXXXXXXX:role/handson-cli-ssm-session-manager-role" } }
cat << END # IAM_ROLE_NAME:"handson-cli-ssm-session-manager-role" IAM_ROLE_NAME="${IAM_ROLE_NAME}" # IAM_ROLE_PATH:"/handson-cli/" IAM_ROLE_PATH="${IAM_ROLE_PATH}" # FILE_IAM_ROLE_DOC:"${HOME}/environment/conf-handson-cli-ssm-session-manager/handson-cli-ssm-session-manager-role.json" FILE_IAM_ROLE_DOC="${FILE_IAM_ROLE_DOC}" END
コマンド:
aws iam create-role \ --role-name ${IAM_ROLE_NAME} \ --path "${IAM_ROLE_PATH}" \ --assume-role-policy-document file://${FILE_IAM_ROLE_DOC}
{ "Role": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [ { "Action": "sts:AssumeRole", "Sid": "", "Effect": "Allow", "Principal": { "Service": "ec2.amazonaws.com" } } ] }, "RoleId": "AROAxxxxxxxxxxxxxxxxx", "CreateDate": "2021-04-09T01:23:45.678Z", "RoleName": "handson-cli-ssm-session-manager-role", "Path": "/", "Arn": "arn:aws:iam::XXXXXXXXXXXX:role/handson-cli-ssm-session-manager-role" } }
「IAMロール"handson-cli-ssm-session-manager-role"が存在する。」ことを確認します。
コマンド: aws iam list-roles \ --query "Roles[?RoleName == \`${IAM_ROLE_NAME}\`].RoleName" \ --output text 結果(例): handson-cli-ssm-session-manager-role
aws iam list-roles \ --query "Roles[?RoleName == \`${IAM_ROLE_NAME}\`].RoleName" \ --output text
handson-cli-ssm-session-manager-role