LoginSignup
0
0

More than 5 years have passed since last update.

[JAWS-UG CLI] ELB:#8 ロードバランサへのセキュリティグループ追加

Posted at

前提条件

ELBへの権限

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

EC2への権限

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

AWS CLIのバージョン

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

  • AWS CLI 1.10.63
コマンド
aws --version
結果(例)
      aws-cli/1.10.63 Python/2.7.11 Darwin/15.6.0 botocore/1.4.53

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

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

AWSアカウントの属性

AWSアカウントがEC2-Classicに対応していないこと。

コマンド
AWS_SUPPORT_PLATFORMS=$( \
         aws ec2 describe-account-attributes \
           --query 'AccountAttributes[?AttributeName == `supported-platforms`].AttributeValues[].AttributeValue' \
           --output text \
) && echo ${AWS_SUPPORT_PLATFORMS}
結果
      VPC

'VPC'の他に'EC2'が表示される場合、別のアカウントを作成もしくは利用して
ください。

デフォルトVPCの存在

デフォルトVPCが存在すること。

コマンド
VPC_ID=$( \
        aws ec2 describe-vpcs \
          --filters Name=isDefault,Values=true \
          --query 'Vpcs[].VpcId' \
          --output text \
) \
        && echo ${VPC_ID}
結果(例)
      vpc-xxxxxxxx

0. 準備

0.1. リージョンの決定

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

0.2. 変数の確認:

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

変数の確認
aws configure list
結果(例)
            Name                    Value             Type    Location
            ----                    -----             ----    --------
         profile       ec2elb_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. 変数の初期化

変数の確認
cat << ETX

        ARRAY_VPC_SG_ID: ${ARRAY_VPC_SG_ID}

ETX

値が入っている場合は、初期化しておきます。

コマンド
ARRAY_VPC_SG_ID=""

0.4. ロードバランサ名の指定

変数の設定
ELB_LB_NAME='lb-handson-20160912'

1. 事前作業

1.1. セキュリティグループの指定

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

コマンド
aws ec2 describe-security-groups \
        --query 'SecurityGroups[].GroupName'
結果(例)
      [
        "default",
        "ec2-http-https-global-inbound"
      ]

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

変数の設定
VPC_SG_NAME='ec2-http-https-global-inbound'

セキュリティグループのIDを取得します。

コマンド
VPC_SG_ID=$( \
        aws ec2 describe-security-groups \
          --filter Name=group-name,Values=${VPC_SG_NAME} \
          --query 'SecurityGroups[].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}

1.2. セキュリティグループの確認

ロードバランサの現在のセキュリティグループを確認します。

コマンド
aws elb describe-load-balancers \
        --load-balancer-names ${ELB_LB_NAME} \
        --query 'LoadBalancerDescriptions[].SourceSecurityGroup.GroupName'
結果(例)
      [
        "default"
      ]

2. セキュリティグループの適用

コマンド
aws elb apply-security-groups-to-load-balancer \
        --load-balancer-name ${ELB_LB_NAME} \
        --security-groups ${ARRAY_VPC_SG_ID}
結果(例)
      {
        "SecurityGroups": [
          "sg-xxxxxxxx"
        ]
      }

3. 事後作業

3.1. セキュリティグループの確認

ロードバランサの現在のセキュリティグループを確認します。

コマンド
aws elb describe-load-balancers \
        --load-balancer-names ${ELB_LB_NAME} \
        --query 'LoadBalancerDescriptions[].SourceSecurityGroup.GroupName'
結果(例)
      [
        "ec2-http-https-global-inbound"
      ]

完了

0
0
0

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
0
0