ぺい

渋谷系アドテクエンジニアの落書き

AWS ECRのuntaggedを定期的に消す(シェルスクリプト)

定期的にuntaggedを消したい

f:id:tikasan0804:20190111145141p:plain

jenkinsなどで、以下のコードを定期実行するだけ。
コードのrepository idはECRのコンソールから確認してコピってください。

f:id:tikasan0804:20190111143756p:plain

echo start
aws ecr get-login --no-include-email --region ap-northeast-1 --registry-ids !ここにrepository id!

aws ecr describe-repositories --output text | awk '{print $5}' \
  | while read rname; do echo $rname; aws ecr list-images --repository-name $rname --filter tagStatus=UNTAGGED --query 'imageIds[*]' --output text \
  | while read imageId; do aws ecr batch-delete-image --repository-name $rname --image-ids imageDigest=$imageId; \
  done; \
done
echo done

ライフサイクルでもよかったけど、毎度人間が設定するとは思えなかったので、却下した。 docs.aws.amazon.com