LoginSignup
0
0

More than 5 years have passed since last update.

[JAWS-UG CLI] ACM入門 #10 EC2インスタンスの削除

Posted at

これはAWS Certificate Manager (ACM)入門ハンズオン資料の一部です。
最初から続けて行っている場合「0.準備」の各項は作業不要です。第1項へ進んでください

0. 準備

0.1. リージョンの決定

今回はバージニアリージョン(us-east-1)で作業してください。

変数の設定
export AWS_DEFAULT_REGION='us-east-1'

0.2. EC2インスタンスIDの変数への格納

コマンド
EC2_INSTANCE_ID=`aws ec2 describe-instances \
 --query Reservations[].Instances[].InstanceId \
 --output text` && echo ${EC2_INSTANCE_ID}
結果(例)
      i-77777777777777777

0.3. セキュリティグループIDの変数への格納

コマンド
SG_NAME="ec2-handson-sg"
SG_ID=`aws ec2 describe-security-groups --group-names ${SG_NAME} \
 --query SecurityGroups[].GroupId --output text` && echo ${SG_ID}
結果
      sg-acf623d1

0.4. 鍵ペア名と鍵ファイル名の変数への格納

コマンド
EC2_KEY_NAME="ec2-handson" && echo ${EC2_KEY_NAME}
FILE_SSH_KEY="${HOME}/.ssh/${EC2_KEY_NAME}.pem"
ls ${FILE_SSH_KEY}
結果(例)
      ec2-handson
      /home/ec2-user/.ssh/ec2-handson.pem

1. EC2インスタンスの削除

1.1. インスタンスの削除(ターミネート)

コマンド
aws ec2 terminate-instances --instance-ids ${EC2_INSTANCE_ID}
結果(例)
      {
          "TerminatingInstances": [
              {
                  "InstanceId": "i-77777777777777777",
                  "CurrentState": {
                      "Code": 32,
                      "Name": "shutting-down"
                  },
                  "PreviousState": {
                      "Code": 16,
                      "Name": "running"
                  }
              }
          ]
      }

インスタンスの削除が完了しないと他のリソースを削除できないため、ここで3分ほど待ちます。

1.2. インスタンスが削除されたか確認

コマンド
aws ec2 describe-instances --instance-ids ${EC2_INSTANCE_ID} \
 --query Reservations[].Instances[].State.Name
結果
      [
          "terminated"
      ]

terminated と出力されたか必ず確認してから次に進んでください。
shutting-down の場合はもう数分待って再実行します。
running の場合は削除に失敗しています。前の手順を再試行してください。

1.3. セキュリティグループの削除

コマンド
aws ec2 delete-security-group --group-id ${SG_ID}
結果
      返り値なし

1.4. セキュリティグループ削除の確認

コマンド
aws ec2 describe-security-groups --group-ids ${SG_ID}
結果(例)
      An error occurred (InvalidGroup.NotFound) when calling the DescribeSecurityGroups operation: The security group 'sg-acf623d1' does not exist

このようにエラーになれば削除完了しています。

1.5. 鍵ペアの削除

コマンド
rm -f ${FILE_SSH_KEY}
aws ec2 delete-key-pair --key-name ${EC2_KEY_NAME}
結果
      返り値なし

1.6. 鍵ペア削除の確認

コマンド
aws ec2 describe-key-pairs --key-names ${EC2_KEY_NAME}
結果(例)
      An error occurred (InvalidKeyPair.NotFound) when calling the DescribeKeyPairs operation: The key pair 'ec2-handson' does not exist

このようにエラーになれば削除完了しています。

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