ハンズオン(簡易版): Cloud9 (AWS CLI環境)入門 CloudFormation版

3. EC2インスタンスへのIAMインスタンスプロファイルの関連付け(CloudShell: aws-cloud9-handson-cli-env:handson-cloud9-instance-profile)

手順の目的

Nameタグのプレフィックスが"aws-cloud9-handson-cli-env"のEC2インスタンスにIAMインスタンスプロファイル"handson-cloud9-instance-profile"を関連付けます。

設定値の指定

手順に必要な設定値を変数に格納をします。

1. EC2インスタンスのNameタグのプレフィックス

EC2インスタンスのNameタグのプレフィックスを指定します。

変数の設定:

EC2_INSTANCE_TAG_NAME_PREFIX='aws-cloud9-handson-cli-env'

2. IAMインスタンスプロファイル名

IAMインスタンスプロファイル名を指定します。

変数の設定:

IAM_INSTANCE_PROFILE_NAME='handson-cloud9-instance-profile'

設定値の確認

各変数に正しい設定値が格納されていることを確認します。

変数の確認:

cat << END

  # 0. AWS_REGION:"ap-northeast-1"
       AWS_REGION="${AWS_REGION}"

  # 1. EC2_INSTANCE_TAG_NAME_PREFIX:"aws-cloud9-handson-cli-env"
       EC2_INSTANCE_TAG_NAME_PREFIX="${EC2_INSTANCE_TAG_NAME_PREFIX}"
  # 2. IAM_INSTANCE_PROFILE_NAME:"handson-cloud9-instance-profile"
       IAM_INSTANCE_PROFILE_NAME="${IAM_INSTANCE_PROFILE_NAME}"

END

各変数について、上の行と下の行の値の内容もしくは形式が同じであることを確認します。 もし異なる場合は、それぞれの手順番号に戻って変数の設定を行います。

処理の実行

EC2インスタンスIDを取得します。

コマンド:

array_ec2_instance_ids=$( \
  aws ec2 describe-instances \
    --filters Name=tag-key,Values=Name \
              Name=tag-value,Values=${EC2_INSTANCE_TAG_PREFIX}* \
              Name=instance-state-name,Values=running \
    --query Reservations[].Instances[].InstanceId \
    --output text \
) \
  && echo ${array_ec2_instance_ids}

結果(例):

i-xxxxxxxxxxxxxxxxx

注釈

本手順では、同一のEC2インスタンスタグ名プレフィックスを持つEC2インスタンスが1台しか存在しないことを前提にしています。

EC2インスタンスのインスタンスIDを変数に格納します。

変数の設定:

ec2_instance_id=$( \
  echo ${array_ec2_instance_ids} \
    | sed 's/ .*$//' \
) \
  && echo ${ec2_instance_id}

結果(例):

i-xxxxxxxxxxxxxxxxx

インスタンスプロファイルのARNを取得します。

コマンド:

iam_instance_profile_arn=$( \
  aws iam get-instance-profile \
    --instance-profile-name ${IAM_INSTANCE_PROFILE_NAME} \
    --query 'InstanceProfile.Arn' \
    --output text \
) \
  && echo ${iam_instance_profile_arn}

結果(例):

arn:aws:iam::XXXXXXXXXXXX:instance-profile/handson-cloud9/handson-cloud9-instance-profile

インスタンスプロファイル設定文字列を生成します。

変数の設定:

string_ec2_instance_profile="Arn=${iam_instance_profile_arn},Name=${IAM_INSTANCE_PROFILE_NAME}" \
  && echo ${string_ec2_instance_profile}

変数の設定:

Arn=arn:aws:iam::XXXXXXXXXXXX:instance-profile/handson-cloud9/handson-cloud9-instance-profile,Name=handson-cloud9-instance-profile

インスタンスプロファイルの関連付け

EC2インスタンスにインスタンスプロファイルを関連付けます。

変数の確認:

cat << END

  # string_ec2_instance_profile:"Arn=arn:aws:iam::XXXXXXXXXXXX:instance-profile/handson-cloud9/handson-cloud9-instance-profile,Name=handson-cloud9-instance-profile"
    string_ec2_instance_profile="${string_ec2_instance_profile}"
  # ec2_instance_id:"i-xxxxxxxxxxxxxxxxx"
    ec2_instance_id="${ec2_instance_id}"

END

コマンド:

aws ec2 associate-iam-instance-profile \
  --iam-instance-profile ${string_ec2_instance_profile} \
  --instance-id ${ec2_instance_id}

結果(例):

{
    "IamInstanceProfileAssociation": {
        "AssociationId": "iip-assoc-xxxxxxxxxxxxxxxxx",
        "InstanceId": "i-xxxxxxxxxxxxxxxxx",
        "IamInstanceProfile": {
            "Arn": "arn:aws:iam::XXXXXXXXXXXX:instance-profile/handson-cloud9/handson-cloud9-instance-profile",
            "Id": "AIPAXXXXXXXXXXXXXXXXX"
        },
        "State": "associating"
    }
}

完了確認

「Nameタグのプレフィックスが"aws-cloud9-handson-cli-env"のEC2インスタンスにIAMインスタンスプロファイルが関連付けられている。」ことを確認します。

コマンド:

aws ec2 describe-iam-instance-profile-associations \
  --filters "Name=instance-id,Values=${ec2_instance_id}" \
            "Name=state,Values=associated" \
  --query "IamInstanceProfileAssociations[].IamInstanceProfile.Arn" \
  --output text

結果(例):

arn:aws:iam::XXXXXXXXXXXX:instance-profile/handson-cloud9/handson-cloud9-instance-profile

手順の完了

(参考) マネジメントコンソールの確認

  • 左ペインの"インスタンス"の下位にある"インスタンス"をクリックします。

インスタンス一覧(画面)

  • 検索欄にEC2インスタンスのNameタグのプレフィックス"aws-cloud9-handson-cli-env"を入力して、エンターキーを押します。

  • Nameタグのプレフィックスが"aws-cloud9-handson-cli-env-xxxx"のEC2インスタンスのチェックボックスをオンにします。

  • "詳細"(タブ)の"インスタンス概要"(セクション)の"インスタンスプロファイルARN"arn:aws:iam::XXXXXXXXXXXX:instance-profile/handson-cloud9/handson-cloud9-instance-profile"が表示されていることを確認します。