LoginSignup
1
0

More than 5 years have passed since last update.

[JAWS-UG CLI] EC2 #10 インスタンス属性の更新 (セキュリティグループの追加)

Last updated at Posted at 2016-12-05

前提条件

EC2への権限

EC2に対してフル権限があること。

AWS CLIのバージョン

以下のバージョンで動作確認済

  • AWS CLI 1.11.24
コマンド
aws --version

結果(例):

  aws-cli/1.11.19 Python/2.7.10 Darwin/15.6.0 botocore/1.4.76

バージョンが古い場合は最新版に更新しましょう。

コマンド
sudo -H pip install -U awscli

0. 準備

0.1. リージョンの決定

変数の設定
export AWS_DEFAULT_REGION='ap-northeast-1'

0.2. 変数の確認

プロファイルが想定のものになっていることを確認します。

変数の確認
aws configure list

結果(例):

        Name                    Value             Type    Location
        ----                    -----             ----    --------
     profile       ec2as_full-prjZ-mbp13        env    AWS_DEFAULT_PROFILE
  access_key     ****************XXXX shared-credentials-file
  secret_key     ****************XXXX shared-credentials-file
      region        ap-northeast-1        env    AWS_DEFAULT_REGION

0.3. インスタンスIDの指定

既存のインスタンスに割り当てられているIPアドレスを確認します。

ここでは、10.192.0.8を割り当てられているインスタンスを選択します。

変数の設定
EC2_PRIVATE_ADDR='10.192.0.8'

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

コマンド
EC2_INSTANCE_ID=$( \
        aws ec2 describe-instances \
          --filters Name=private-ip-address,Values=${EC2_PRIVATE_ADDR} \
          --query 'Reservations[].Instances[].InstanceId' \
          --output text \
) \
        && echo ${EC2_INSTANCE_ID}

結果(例):

  i-xxxxxxxxxxxxxxxxx

0.4. セキュリティグループIDの取得

コマンド
ARRAY_VPC_SG_ID=$( \
        aws ec2 describe-instances \
          --filters Name=private-ip-address,Values=${EC2_PRIVATE_ADDR} \
          --query 'Reservations[].Instances[].SecurityGroups[].GroupId' \
        --output text \
) \
        && echo ${ARRAY_VPC_SG_ID}

結果(例):

  sg-xxxxxxxx

1. 事前作業

1.1. 追加するセキュリティグループの決定

セキュリティグループの一覧を確認します。

コマンド
aws ec2 describe-security-groups \
        --query "SecurityGroups[?VpcId == \` ${VPC_ID}\`].GroupName"

結果(例):

  [
    "default",
    "ec2-http-global-inbound"
  ]

利用するセキュリティグループ名を指定します。

変数の設定
VPC_SG_NAME='ec2-http-global-inbound'
コマンド
VPC_SG_ID=$( \
        aws ec2 describe-security-groups \
          --filter Name=group-name,Values=${VPC_SG_NAME} \
          --query "SecurityGroups[?VpcId == \` ${VPC_ID}\`].GroupId" \
          --output text \
) \
        && echo ${VPC_SG_ID}

結果(例):

  sg-xxxxxxxx
変数の設定
ARRAY_VPC_SG_ID="${VPC_SG_ID} ${ARRAY_VPC_SG_ID}" \
        && echo ${ARRAY_VPC_SG_ID}

2. インスタンス属性の変更

変数の確認
cat << ETX

        EC2_INSTANCE_ID: ${EC2_INSTANCE_ID}
        ARRAY_VPC_SG_ID: ${ARRAY_VPC_SG_ID}

ETX
コマンド
aws ec2 modify-instance-attribute \
        --instance-id ${EC2_INSTANCE_ID} \
        --groups ${ARRAY_VPC_SG_ID}

結果(例):

  (戻り値なし)

3. 事後作業

セキュリティグループIDの取得

コマンド
ARRAY_VPC_SG_ID=$( \
        aws ec2 describe-instances \
          --filters Name=private-ip-address,Values=${EC2_PRIVATE_ADDR} \
          --query 'Reservations[].Instances[].SecurityGroups[].GroupId' \
        --output text \
) \
        && echo ${ARRAY_VPC_SG_ID}

結果(例):

  sg-xxxxxxxx sg-xxxxxxxx

完了

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0