LoginSignup
1
1

More than 5 years have passed since last update.

[JAWS-UG CLI] EC2 # スナップショットの削除

Last updated at Posted at 2017-02-13

前提条件

EC2への権限

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

AWS CLIのバージョン

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

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

結果(例):

  aws-cli/1.11.34 Python/2.7.10 Darwin/15.6.0 botocore/1.4.91

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

コマンド
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'が表示される場合、別のアカウントを作成もしくは
利用し てください。

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. AWS IDの取得

変数の設定
AWS_ID=$( \
        aws sts get-caller-identity \
          --query 'Account' \
          --output text \
) && echo ${AWS_ID}

結果(例)

  XXXXXXXXXXXX
変数の設定
EC2_SNAPSHOT_OWNER_ID="${AWS_ID"

1. 事前作業

1.1. AMIの指定

変数の設定
EC2_IMAGE_ID='<削除するスナップショットを作成したAMIのイメージID>'

すでにAMIを削除済みの場合は、下記コマンド結果から探してください。

コマンド
aws ec2 describe-snapshots \
        --filters Name=owner-id,Values=${EC2_SNAPSHOT_OWNER_ID}

AMIを削除前であれば、下記コマンドで取得できます。

コマンド
EC2_IMAGE_ID=$( \
      aws ec2 describe-images \
        --filters Name=name,Values="${EC2_IMAGE_NAME}" \
        --query 'Images[].ImageId' \
        --output text \
) \
      && echo ${EC2_IMAGE_ID}

1.2. スナップショットIDの取得

コマンド
EC2_SNAPSHOT_ID=$( \
        aws ec2 describe-snapshots \
          --filters Name=owner-id,Values=${EC2_SNAPSHOT_OWNER_ID} \
          --query "Snapshots[?contains(Description, \`${EC2_IMAGE_ID}\`)].SnapshotId" \
          --output text \
) \
        && echo ${EC2_SNAPSHOT_ID}

結果(例):

  snap-xxxxxxxx
変数の設定
ARRAY_EC2_SNAPSHOT_IDS="${EC2_SNAPSHOT_ID}" \
        && echo ${ARRAY_EC2_SNAPSHOT_IDS}
コマンド
aws ec2 describe-snapshots \
        --snapshot-ids ${ARRAY_EC2_SNAPSHOT_IDS}

結果(例):

  {
    "Snapshots": [
      {
          "Description": "Created by CreateImage(i-xxxxxxxxxxxxxxxxx) for ami-xxxxxxxx from vol-xxxxxxxxxxxxxxxxx",
          "Encrypted": false,
          "VolumeId": "vol-xxxxxxxxxxxxxxxxx",
          "State": "completed",
          "VolumeSize": 8,
          "Progress": "100%",
          "StartTime": "2017-02-13T01:23:45.000Z",
          "SnapshotId": "snap-xxxxxxxx",
          "OwnerId": "XXXXXXXXXXXX"
      }
    ]
  }

2. スナップショットの削除

変数の確認
cat << ETX

        EC2_SNAPSHOT_ID: ${EC2_SNAPSHOT_ID}

ETX
コマンド
aws ec2 delete-snapshot \
        --snapshot-id ${EC2_SNAPSHOT_ID}

結果(例):

  >>|yet|<<

3. 事後作業

コマンド
aws ec2 describe-snapshots \
        --owner-ids ${AWS_ID}

結果(例):

  {
    "Snapshots": []
  }

完了

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