Jenkins에서 EC2 스냅샷 대량 삭제

Jenkins에서 EC2 스냅샷 대량 삭제

사용 사례:
txt 파일(old_snapshots.txt)의 스냅샷 목록을 S3 버킷으로 내보냅니다. Jenkins에서 aws cp 명령을 사용하여 Jenkins /tmp/directory에 파일 복사

--dry-run did not show any error 

그러나 Jenkins에서 aws delete 명령을 사용하여 다음 bash 라인을 전달하면 SUCCESS가 표시되고 동시에 오류가 발생했으며 스냅샷이 삭제되지 않는다고 표시됩니다.

## actual deletion
        file="/tmp/old_snapshots.txt"
         while read delete_data
         do
        aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $file
        echo "Deleting snapshot $delete_data"
        done <"$file"

산출

An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operation: Value (/tmp/old_snapshots.txt) for parameter snapshotId is invalid. Expected: 'snap-...'.
Deleting snapshot snap-xxxx81xxxxxx7fxxx

An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operation: Value (/tmp/old_snapshots.txt) for parameter snapshotId is invalid. Expected: 'snap-...'.
Deleting snapshot snap-xxxacc49xxxxxxc26


An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operation: Value (/tmp/old_snapshots.txt) for parameter snapshotId is invalid. Expected: 'snap-...'.
Deleting snapshot snap-04xxxxxxxx3fa3cxxxxxxf4

[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

요구:이 오류는 An error occurred (InvalidParameterValue) when calling the DeleteSnapshot operation너무 일반적인 것 같나요? 문제를 경험한 사람이 있나요? 또한 Jenkins에 스냅샷 ID를 수동으로 추가하여 aws cli로 단일 스냅샷만 삭제하는 수동 테스트를 수행했는데 제대로 작동했습니다. 어떤 제안이라도 대단히 감사하겠습니다.

aws 명령 포함

aws ec2 --region eu-west-1 delete-snapshot --snapshot-id snap-01x1618xxxxxxa51x
Found snapshotid: snap-01x1618xxxxxxa51x in the uploaded file: /tmp/old_snapshots.txt
Now deleting snapshot id snap-01x1618xxxxxxa51x

답변1

스크립트를 약간 변경해야 합니다.

$ sh abc.sh

What is in delete_data snap-xxxx81xxxxxx7fxxx
What is in file /tmp/old_snapshots.txt
Deleting snapshot snap-xxxx81xxxxxx7fxxx
What is in delete_data snap-xxxacc49xxxxxxc26
What is in file /tmp/old_snapshots.txt
Deleting snapshot snap-xxxacc49xxxxxxc26
What is in delete_data snap-04xxxxxxxx3fa3cxxxxxxf4
What is in file /tmp/old_snapshots.txt
Deleting snapshot snap-04xxxxxxxx3fa3cxxxxxxf4

$ cat abc.sh
## actual deletion

    file="/tmp/old_snapshots.txt"
    while read delete_data
    do
    #aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $file
    echo "What is in delete_data $delete_data"
    echo "What is in file $file"
    echo "Deleting snapshot $delete_data"
    done < $file

스크립트에서 호출하는 file변수는 상수이지만 파일 내용을 한 줄씩 전달해야 합니다. 그럼 아래에서 교체해 보세요

aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $file

와 함께

aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $delete_data

답변2

다른 커뮤니티 사용자의 이익을 위해 플래그를 사용 --debug하여'\r'스냅-#

디버그 출력

2020-01-28 18:09:02,295 - MainThread - botocore.hooks - DEBUG - Event load-cli-arg.ec2.delete-snapshot.snapshot-id: calling handler <awscli.paramfile.URIArgumentHandler object at >
2020-01-28 18:09:02,296 - MainThread - botocore.hooks - DEBUG - Event process-cli-arg.ec2.delete-snapshot: calling handler <awscli.argprocess.ParamShorthandParser object at >
2020-01-28 18:09:02,296 - MainThread - awscli.arguments - DEBUG - Unpacked value of u'snap-0xxxxxxxxxxxxxxxx\r' for parameter "snapshot_id": u'snap-0xxxxxxxxxxxxxxxx\r'

문제를 해결하려면: tr -d '\r'대화를 만들기 위해 값을 보유하는 변수에 전달했습니다.

:

tr -d '\r' < input > output

업데이트된 스크립트

file="/tmp/old_snapshots.txt"
cat $file | tr -d '\r' | while read -r line; 
do 
aws ec2 --region eu-west-1 delete-snapshot --snapshot-id $line                        
done

관련 정보