【AWS】Amazon ECR ~ AWS CLI ~

■ はじめに

https://dk521123.hatenablog.com/entry/2020/05/22/165711
https://dk521123.hatenablog.com/entry/2020/05/26/142645
https://dk521123.hatenablog.com/entry/2023/12/02/024631

の続き。

今回は、Amazon ECR の AWS CLI について扱う。
主に、CI/CD で使おうとしているActionを取り上げる。

目次

【0】AWS CLIs of ECR
 1)aws ecr
 2)aws ecr wait
【1】全般
 1)get-login-password
【2】イメージ関連
 1)list-images
 2)batch-delete-image
【3】スキャン関連
 1)wait image-scan-complete
 2)describe-image-scan-findings
 3)start-image-scan

【0】AWS CLIs of ECR

* 以下の公式ドキュメント参照

1)aws ecr

https://docs.aws.amazon.com/cli/latest/reference/ecr/

2)aws ecr wait

* こんなんあったんだ、、、

https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ecr/wait/index.html

【1】全般

1)get-login-password

* docker login のためのパスワード取得

https://docs.aws.amazon.com/cli/latest/reference/ecr/get-login-password.html

コマンド例

# docker login [オプション] [サーバ]
# (--username:ユーザ名、--password-stdin:標準出力からパスワードを設定)
aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin xxxx.xxx.ecr.us-west-2.amazonaws.com

【2】イメージ関連

1)list-images

* リポジトリ内のイメージ一覧表示

https://docs.aws.amazon.com/cli/latest/reference/ecr/list-images.html
コマンド例

aws ecr list-images \
  --repository-name sample-repo

2)batch-delete-image

* イメージの削除

https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ecr/batch-delete-image.html

* 以下の公式ドキュメントに、「イメージの削除」の「イメージを削除するには (AWS CLI)」
 に記載があるので、一読しておいてもいいかも

https://docs.aws.amazon.com/ja_jp/AmazonECR/latest/userguide/delete_image.html

コマンド例

aws ecr batch-delete-image \
    --repository-name sample-repo --region us-west-2 \
    --image-ids imageDigest=sha256:74b2c688c700ec95a93e478cdb959737c148df3fbf5ea706abe0318726e885e6

# Tag指定して削除する場合
aws ecr batch-delete-image \
     --repository-name sample-repo --region us-west-2 \
     --image-ids imageTag=tag1 imageTag=tag2

補足:実行後の確認について

* 実行後の確認で、AWS Management コンソール上で
 Reloadボタン押下してもキャッシュされているのか反映されていなかった
 => なので、確認する際は、実行後にブラウザのリロードを行った方がいいい

【3】スキャン関連

1)wait image-scan-complete

* スキャンするまで待つ

https://docs.aws.amazon.com/cli/latest/reference/ecr/wait/image-scan-complete.html
コマンド例

while true
do
  aws ecr wait image-scan-complete \
  --repository-name sample-repo \
  --image-id imageDigest=sha256:74b2c688c700ec95a93e478cdb959737c148df3fbf5ea706abe0318726e885e6
  [ $? -eq 0 ] && break
done

2)describe-image-scan-findings

* スキャン結果取得

https://docs.aws.amazon.com/cli/latest/reference/ecr/describe-image-scan-findings.html

コマンド例

aws ecr describe-image-scan-findings \
  --repository-name sample-repo \
  --no-paginate \
  --image-id imageDigest=sha256:74b2c688c700ec95a93e478cdb959737c148df3fbf5ea706abe0318726e885e6

オプション

Options Explanation
--no-paginate ページネーション(ページネーション)を無効にする

3)start-image-scan

* スキャン開始

https://docs.aws.amazon.com/cli/latest/reference/ecr/start-image-scan.html
使用上の注意

* 24時間で1回だけしかスキャン開始されないという制限がある

https://docs.aws.amazon.com/cli/latest/reference/ecr/start-image-scan.html#description

より抜粋

Starts an image vulnerability scan.
[訳] イメージの脆弱性スキャンを開始する

An image scan can only be started once per 24 hours on an individual image.
[訳]イメージスキャンは、独立したイメージで、24時間に1回のみだけである

This limit includes if an image was scanned on initial push.
[訳] この制限は、もし初期Pushでスキャンされたイメージも含んでいます。

関連記事

Amazon ECR ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2020/05/22/165711
Amazon ECR ~ 基本編 ~
https://dk521123.hatenablog.com/entry/2020/05/26/142645
Amazon ECR ~ boto3 ~
https://dk521123.hatenablog.com/entry/2024/01/31/014010
Amazon ECR ~ Dockerイメージを Pull & Push ~
https://dk521123.hatenablog.com/entry/2023/12/02/024631
Amazon ECR ~ 脆弱性診断 / Amazon Inspector ~
https://dk521123.hatenablog.com/entry/2024/01/22/210831
Terraform ~ AWS ECR ~
https://dk521123.hatenablog.com/entry/2023/05/23/002314
Amazon ECR でのトラブルシューティング
https://dk521123.hatenablog.com/entry/2020/05/24/000000
AWS CLI ~ --query / JMESPath ~
https://dk521123.hatenablog.com/entry/2024/01/07/000000