ECR 基本スキャン vs 強化スキャン

項目基本スキャン (Basic)強化スキャン (Enhanced)
スキャン対象OSパッケージの脆弱性のみOS + プログラミング言語パッケージの脆弱性
スキャン頻度手動 / プッシュ時プッシュ時 / 継続的スキャン
スキャンエンジンAWS native (推奨) または Clair (非推奨)Amazon Inspector
料金無料Amazon Inspectorの料金が発生
継続的な更新なし(再スキャンが必要)新しい脆弱性が発見されると自動更新

出典: [Scan images for software vulnerabilities in Amazon ECR](https://docs.aws.amazon.com/AmazonECR/latest/ userguide/image-scanning.html)

Enhanced scanning – Amazon ECR integrates with Amazon Inspector to provide automated, continuous scanning of your repositories. Your container images are scanned for both operating systems and programming language package vulnerabilities.

出典: Scan images for OS vulnerabilities in Amazon ECR

Amazon ECR basic scanning support filters to specify which repositories to scan on push and can scan up to 100,000 images per 24 hours in a given registry.

スキャンのタイミング

  • 基本スキャン (Basic scanning)
タイミング説明
手動 (Manual)aws ecr start-image-scanコマンドやコンソールから手動実行
プッシュ時 (Scan on push)イメージをリポジトリにプッシュしたタイミングで自動実行
  • 拡張スキャン (Enhanced scanning)
タイミング説明
プッシュ時 (Scan on push)イメージをリポジトリにプッシュしたタイミングで自動実行
継続的 (Continuous)新しい脆弱性(CVE)が公開されると自動で再スキャン

出典: [Scan images for software vulnerabilities in Amazon ECR](https://docs.aws.amazon.com/AmazonECR/latest/ userguide/image-scanning.html)

As new vulnerabilities appear, the scan results are updated and Amazon Inspector emits an event to EventBridge to notify you.

ポイント:

  • 基本スキャンは一度スキャンしたら結果は固定(新しいCVEが出ても自動更新されない)
  • 拡張スキャンの継続的スキャンは、イメージを再プッシュしなくても新しい脆弱性が見つかれば結果が更新される

使い分け

  • 基本スキャン: コストを抑えたい、OSレベルの脆弱性チェックで十分な場合
  • 強化スキャン: Node.js、Python、Javaなどのアプリケーション依存関係も含めてスキャンしたい、継続的に脆弱性を監視したい場合

ECR プルスルーキャッシュ

外部のコンテナレジストリ(Docker Hub、Quay など)のイメージを ECR プライベートレジストリにキャッシュする機能

出典: [Sync an upstream registry with an Amazon ECR private registry](https://docs.aws.amazon.com/AmazonECR/ latest/userguide/pull-through-cache.html)

Using pull through cache rules, you can sync the contents of an upstream registry with your Amazon ECR private registry.

嬉しさ

  1. レート制限の回避: Docker Hub などには pull 回数制限があルガ、キャッシュ経由なら AWS IP からの pull になるため、制限を回避できる
  2. レイテンシ削減: 同一リージョン内の ECR から pull するため高速
  3. セキュリティ: ECR の脆弱性スキャン機能を外部イメージにも適用できる
  4. 可用性向上: 外部レジストリがダウンしてもキャッシュから pull 可能
  5. 通信コスト削減: NAT Gateway を通じたパブリックイメージ取得時の通信を削減可能(他方、当該機能の使用に伴うストレージ費用との兼ね合いを要検討

仕組み

┌─────────────┐     pull      ┌─────────────┐   初回 or     ┌─────────────┐
│   Client    │ ────────────> │  ECR Private │  24時間経過時  │ Upstream    │
│  (EKS等)    │               │  Registry    │ ────────────> │ Registry    │
└─────────────┘               └─────────────┘               └─────────────┘
                                    │                        (ECR Public, Docker Hub 等)
                                    │ キャッシュ保存/更新
                                    ▼
                              ┌─────────────┐
                              │  ECR Repo   │
                              │  (キャッシュ) │
                              └─────────────┘

  1. クライアントが ECR プライベートレジストリ URI でイメージを pull
  2. ECR がアップストリームレジストリからイメージを取得
  3. ECR にリポジトリを自動作成してキャッシュ
  4. 以降の pull はキャッシュから提供(24時間以内の場合)
  5. 24時間経過後の pull 時にアップストリームをチェックし、新しいバージョンがあれば更新

出典: Sync an upstream registry with an Amazon ECR private registry

For subsequent pull requests of the cached image with a given tag, Amazon ECR checks the upstream registry for a new version of the image with that specific tag and attempts to update the image in your private registry at least once every 24 hours.

試してみる

ECRライフサイクルポリシー

リポジトリ内のイメージを条件に基づいて自動削除する機能

出典: Automate the cleanup of images by using lifecycle policies in Amazon ECR

“Amazon ECR lifecycle policies provide more control over the lifecycle management of images in a private repository.”

削除条件

countType説明
imageCountMoreThanイメージ数が指定数を超えたら古いものから削除
sinceImagePushedプッシュから指定日数経過で削除
sinceImagePulled最後のpullから指定日数経過で削除

出典: Lifecycle policy properties in Amazon ECR

タグフィルタ

tagStatus説明
tagged特定タグのみ対象
untaggedタグなしのみ対象
anyすべて対象

tagStatus: tagged の場合、tagPrefixList または tagPatternList で対象タグを指定できる

出典: Lifecycle policy properties in Amazon ECR

“tagPatternList: A list of strings that specifies the tags to expire or transition when creating a lifecycle policy for tagged images.”

パラメータ説明
tagPrefixList前方一致でタグを指定["dev-", "test-"]dev-123, test-abc にマッチ
tagPatternListワイルドカードでタグを指定["*-snapshot"]v1-snapshot, prod-snapshot にマッチ

具体例:

{
  "selection": {
    "tagStatus": "tagged",
    "tagPrefixList": ["dev-", "test-"],
    "countType": "imageCountMoreThan",
    "countNumber": 5
  }
}

dev- または test- で始まるタグのイメージを5個残して削除

ポリシー例

{
  "rules": [{
    "rulePriority": 1,
    "description": "最新10個を残して削除",
    "selection": {
      "tagStatus": "any",
      "countType": "imageCountMoreThan",
      "countNumber": 10
    },
    "action": { "type": "expire" }
  }]
}

注意点

  • 条件を満たしたイメージは24時間以内に削除される

出典: Automate the cleanup of images by using lifecycle policies in Amazon ECR

“Based on the expiration criteria in the lifecycle policy, images can be archived or expired based on the criteria specified in the lifecycle policy within 24 hours.”