LoginSignup
0
3

More than 5 years have passed since last update.

EBS一覧とEBSのStateによるフィルタリングをAWS CLIで

Posted at

EBS一覧とEBSのStateによるフィルタリングをAWS CLIで

in-useの状態の

Filter Name Ref: http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-DescribeVolumes.html

State毎にEBSを表示する(in-use)

$ aws ec2 describe-volumes --query 'Volumes[*].{ID:VolumeId,State:State}' --filter Name=status,Values=in-use | head -n10
[
    {
        "State": "in-use",
        "ID": "vol-XXXXXXXX"
    },
    {
        "State": "in-use",
        "ID": "vol-XXXXXXXX"
    },
    {

State毎にEBSを表示する(available)

$ aws ec2 describe-volumes --query 'Volumes[*].{ID:VolumeId,State:State}' --filter Name=status,Values=available | head -n10
[
    {
        "State": "available",
        "ID": "vol-XXXXXXXX"
    },
    {
        "State": "available",
        "ID": "vol-XXXXXXXX"
    },
    {

未使用のEBSボリュームを一括で消すスクリプト

#!/usr/bin/env ruby

region = 'ap-northeast-1 '
not_use_ebs_list = `aws ec2 --region #{region} describe-volumes --query 'Volumes[*].VolumeId' --filter Name=status,Values=available --output text`.split(/\t/)

not_use_ebs_list.each do | ebs |
  ebs = ebs.chomp
  puts "Deleting #{ebs}"
  `aws ec2 --region #{region} delete-volume --volume-id #{ebs}`
end

未使用のEBSを消すスクリプトを実行する

$ ruby some.rb
Deleting vol-XXXXXXXX
Deleting vol-XXXXXXXX
Deleting vol-XXXXXXXX

attachedなEBSを削除しようとした場合

A client error (VolumeInUse) occurred when calling the DeleteVolume operation: Volume vol-XXXXXXXX is currently attached to i-XXXXXXXX

となる

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