LoginSignup
0
0

More than 5 years have passed since last update.

[JAWS-UG CLI] VPC #20 IGWのデタッチ、削除

Posted at

前提条件

EC2への権限

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

AWS CLIのバージョン

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

  • AWS CLI 1.11.26
コマンド
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

AWSアカウントの属性

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

結果:

  VPC

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. VPCの指定

既存のVPCに割り当てられているCIDRブロックを確認します。

コマンド
aws ec2 describe-vpcs \
        --query 'Vpcs[].CidrBlock'

結果(例):

  [
    "172.31.0.0/16",
    "10.192.0.0/16"
  ]

ここでは、10.192.0.0/16を範囲とするVPCを選択します。

変数の設定
VPC_CIDR='10.192.0.0/16'

VPC IDを取得します。

コマンド
VPC_ID=$( \
        aws ec2 describe-vpcs \
          --filters Name=cidr,Values=${VPC_CIDR} \
          --query 'Vpcs[].VpcId' \
          --output text \
) \
        && echo ${VPC_ID}

結果(例):

  vpc-xxxxxxxx

1. 事前作業

1.1. IGW IDの取得

コマンド
VPC_IGW_ID=$( \
        aws ec2 describe-internet-gateways \
          --filters Name=attachment.vpc-id,Values=${VPC_ID} \
          --query 'InternetGateways[].InternetGatewayId' \
          --output text \
) \
        && echo ${VPC_IGW_ID}

結果(例):

  igw-xxxxxxxx
変数の設定
ARRAY_VPC_IGW_IDS="${VPC_IGW_ID}" \
        && echo ${ARRAY_VPC_IGW_IDS}
コマンド
aws ec2 describe-internet-gateways \
        --internet-gateway-ids ${ARRAY_VPC_IGW_IDS}

結果(例):

  {
    "InternetGateway": {
      "Tags": [],
      "InternetGatewayId": "igw-xxxxxxxx",
      "Attachments": [
          {
              "State": "available",
              "VpcId": "vpc-xxxxxxxx"
          }
      ]
    }
  }

2. IGWのデタッチ

VPCにIGWをデタッチします。

変数の確認
cat << ETX

        VPC_IGW_ID: ${VPC_IGW_ID}
        VPC_ID:     ${VPC_ID}

ETX
コマンド
aws ec2 detach-internet-gateway \
        --internet-gateway-id ${VPC_IGW_ID} \
        --vpc-id ${VPC_ID}

結果(例):

  (戻り値なし)

3. IGWの削除

3.1. IGWの確認

IGWがVPCにアタッチされていないことを確認します。

コマンド
aws ec2 describe-internet-gateways \
        --internet-gateway-ids ${ARRAY_VPC_IGW_IDS}

結果(例):

  {
    "InternetGateway": {
      "Tags": [],
      "InternetGatewayId": "igw-xxxxxxxx",
      "Attachments": []
    }
  }

3.2. 未アタッチのIGWの確認

コマンド
aws ec2 describe-internet-gateways \
        --query 'InternetGateways[?Attachments == `[]`]'

結果(例):

  {
    "InternetGateway": {
      "Tags": [],
      "InternetGatewayId": "igw-xxxxxxxx",
      "Attachments": []
    }
  }

3.3. IGWの削除

変数の確認
cat << ETX

        VPC_IGW_ID: ${VPC_IGW_ID}

ETX
コマンド
aws ec2 delete-internet-gateway \
        --internet-gateway-id ${VPC_IGW_ID}

結果(例):

  (戻り値なし)

4. 事後作業

まだアタッチされていないIGWが存在しないことを確認します。

コマンド
aws ec2 describe-internet-gateways \
        --query 'InternetGateways[?Attachments == `[]`]'

結果(例):

  []

完了

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