後始末2.5.1. IGWのデタッチ (handson-cli-instance-stateful-ssh-igw)

作業の目的 [why]

IGW"handson-cli-instance-stateful-ssh-igw"をデタッチします。

完了条件/事前条件 [設計者用情報]

完了条件 [after]

主処理は、以下を満たしたときに成功したものとします。

完了条件1

VPC"handson-cli-instance-stateful-ssh-vpc"にIGW"handson-cli-instance-stateful-ssh-igw"がアタッチされていない。

事前条件 [before]

主処理の実施は、以下の状態であることを前提とします。

事前条件1

VPC"handson-cli-instance-stateful-ssh-vpc"が存在する。

事前条件2

IGW"handson-cli-instance-stateful-ssh-igw"が存在する。

事前条件3

VPC"handson-cli-instance-stateful-ssh-vpc"にIGW"handson-cli-instance-stateful-ssh-igw"がアタッチされている。

前提と異なることが判明した場合、直ちに処理を中止します。

作業対象 [what]

  • VPC

  • Internet Gateway

標準時間(合計)

8分

パラメータ設定

パラメータ設定の標準時間

2分

作業に必要なモノ・情報 [resource]

作業開始には、以下が全て揃っていることが必要です。

リソース1: VPCタグ名

  • IGWをアタッチするVPCのタグ名称です。

  • 今回は"handson-cli-instance-stateful-ssh-vpc"とします。

リソース2: IGWタグ名

  • 作成するIGWのタグ名称です。

  • 今回は"handson-cli-instance-stateful-ssh-igw"とします。

パラメータの指定

作業に必要なパラメータを変数に格納をします。

0.0. リージョンの指定

変数の設定

export AWS_DEFAULT_REGION='ap-northeast-1'

0.1. VPCタグ名の指定

VPCタグ名を指定します。

変数の設定:

VPC_TAG_NAME='handson-cli-instance-stateful-ssh-vpc'

0.2. IGWタグ名の指定

IGWタグ名を指定します。

変数の設定:

VPC_IGW_TAG_NAME='handson-cli-instance-stateful-ssh-igw'

パラメータの保存

設定されている変数の保存先となるファイル名を指定します。

変数の設定:

DIR_PARAMETER="${HOME}/tmp/parameter-handson-cli-instance-stateful-ssh"
FILE_PARAMETER="${DIR_PARAMETER}/$(date +%Y-%m-%d)-ec2-igw_detach-update.env" \
  && echo ${FILE_PARAMETER}

結果(例):

${HOME}/tmp/parameter-handson-cli-instance-stateful-ssh/2019-09-16-ec2-igw_detach-update.env

各変数に正しいパラメータ値が格納されていることを確認しながら保存します。

変数の確認:

cat << EOF > ${FILE_PARAMETER}

  # 0.0. AWS_DEFAULT_REGION:"ap-northeast-1"
         AWS_DEFAULT_REGION="${AWS_DEFAULT_REGION}"

  # 0.1. VPC_TAG_NAME:"handson-cli-instance-stateful-ssh-vpc"
         VPC_TAG_NAME="${VPC_TAG_NAME}"
  # 0.2. VPC_IGW_TAG_NAME:"handson-cli-instance-stateful-ssh-igw"
         VPC_IGW_TAG_NAME="${VPC_IGW_TAG_NAME}"

EOF

cat ${FILE_PARAMETER}

下段の変数が入っていない、もしくは上段と同等の値が入っていない場合は、それぞれの手順番号に戻って変数の設定を行います。

タスクの実施

タスク標準時間

6分

1. 前処理

1.1. 処理対象の状態確認

主処理の実施は、以下の状態であることを前提とします。

前提と異なることが判明した場合、直ちに処理を中止します。

事前条件1: VPC"handson-cli-instance-stateful-ssh-vpc"が存在する。

「VPC"handson-cli-instance-stateful-ssh-vpc"が存在する。」ことを確認します。

コマンド:

aws ec2 describe-vpcs \
  --filters Name=tag:Name,Values=${VPC_TAG_NAME}  \
  --query 'Vpcs[].Tags[?Key == `Name`].Value' \
  --output text

結果(例):

handson-cli-instance-stateful-ssh-vpc

VPC IDを取得します。

コマンド:

VPC_ID=$( \
  aws ec2 describe-vpcs \
    --filters Name=tag:Name,Values=${VPC_TAG_NAME}  \
    --query 'Vpcs[].VpcId' \
    --output text \
) \
  && echo ${VPC_ID}

結果(例):

vpc-xxxxxxxxxxxxxxxxx

事前条件2: IGW"handson-cli-instance-stateful-ssh-igw"が存在する。

「IGW"handson-cli-instance-stateful-ssh-igw"が存在する。」ことを確認します。

コマンド:

aws ec2 describe-internet-gateways \
  --filters Name=tag:Name,Values=${VPC_IGW_TAG_NAME}  \
  --query "InternetGateways[].Tags[].Value" \
  --output text

結果(例):

handson-cli-instance-stateful-ssh-igw

事前条件3: VPC"handson-cli-instance-stateful-ssh-vpc"にIGW"handson-cli-instance-stateful-ssh-igw"がアタッチされている。

「VPC"handson-cli-instance-stateful-ssh-vpc"にIGW"handson-cli-instance-stateful-ssh-igw"がアタッチされている。」ことを確認します。

コマンド:

aws ec2 describe-internet-gateways \
  --filters Name=tag:Name,Values=${VPC_IGW_TAG_NAME}  \
  --query "InternetGateways[].Attachments[?VpcId == \`${VPC_ID}\`].VpcId" \
  --output text

結果(例):

vpc-xxxxxxxxxxxxxxxxx

1.2. 主処理に必要な情報の取得

IGW IDの取得

コマンド:

VPC_IGW_ID=$( \
  aws ec2 describe-internet-gateways \
    --filters Name=tag:Name,Values=${VPC_IGW_TAG_NAME} \
    --query "InternetGateways[].InternetGatewayId" \
    --output text \
) \
  && echo ${VPC_IGW_ID}

結果(例):

igw-xxxxxxxxxxxxxxxxx

2. 主処理

IGWのデタッチ

変数の確認:

cat << ETX

  # VPC_ID:"vpc-xxxxxxxxxxxxxxxxx"
    VPC_ID="${VPC_ID}"
  # VPC_IGW_ID:"igw-xxxxxxxxxxxxxxxxx"
    VPC_IGW_ID="${VPC_IGW_ID}"

ETX

コマンド:

aws ec2 detach-internet-gateway \
  --internet-gateway-id ${VPC_IGW_ID} \
  --vpc-id ${VPC_ID}

結果(例):

(出力なし)

3. 後処理

完了条件の確認

主処理は、以下を満たしたときに成功したものとします。

完了条件1: VPC"handson-cli-instance-stateful-ssh-vpc"にIGW"handson-cli-instance-stateful-ssh-igw"がアタッチされていない。

「VPC"handson-cli-instance-stateful-ssh-vpc"にIGW"handson-cli-instance-stateful-ssh-igw"がアタッチされていない。」ことを確認します。

コマンド:

aws ec2 describe-internet-gateways \
  --filters Name=tag:Name,Values=${VPC_IGW_TAG_NAME}  \
  --query "InternetGateways[].Attachments[?VpcId == \`${VPC_ID}\`].VpcId" \
  --output text

結果(例):

(出力なし)

完了