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 で 1 つのスナップショットだけを削除する手動テストを実行しましたが、問題なく動作しました。ご提案があれば、ぜひお知らせください。

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定数の変数を呼び出していますが、ファイルの内容を1行ずつ渡す必要があります。そのため、以下を置き換えます。

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

関連情報