[AWSCLI] delete ebs volumes(bash script)

Seunghyun Moon·2022년 10월 11일
0

aws

목록 보기
2/5

a huge amount was billed from AWS

I looked it up using opsnow service(msp billing service) and found that the usage was from ec2 service. Although I didn't have much resource on it except I had run some tests on ASG(auto scaling group).

among ec2 items, the culprit was the ebs volumes.

found thousands of available ebs volumes...

웹 콘솔에서 삭제하려니.. 엄청난 노가다라서 cli 이용한 자동화 스크립트를 만들었다.
I couldn't be bothered to manually delete each volume on AWS web consle. built a simple, sufficient enough bash script.

Not the best way, had to go through a for loop and wait for responsed from AWS, although it works.

function deleteAvailableVolumes()
{
    for volume in `aws ec2 describe-volumes --filter "Name=status,Values=available" --query "Volumes[*].{ID:VolumeId}" --output text`
    do                                                                                
    aws ec2 delete-volume --volume-id $volume
    done   
}

클라우드 요금 관리를 위해서 공용으로 사용하는 계정의 경우 cron에 등록해서 주기적으로 삭제해줄 예정이다.
I have a lot on my hands at work, which means I can't just log in to AWS console and do this every day. So my team and I have decided to run this using cron at one point.

later discovery. yet...

when creating asg, you can give 'delete volumes on instance delete' option on which will solve this from the beginnig.

profile
I live fullest

0개의 댓글