SJ blog
devops
A

信頼度ランク

S 公式ソース確認済み
A 成功実績多数・失敗例少数
B 賛否両論
C 動作未確認・セキュリティリスク高
Z 個人所感

EC2 Image Builder — ゴールデンAMIのパイプライン自動化

EC2 Image Builderのコンポーネント・レシピ・パイプラインの設計、AMIへのセキュリティパッチ適用自動化、CISベンチマークテスト統合、マルチリージョン配布、コンテナイメージへの対応を解説。

一言結論

EC2 Image Builderはゴールデン AMIのパッチ適用・テスト・マルチリージョン配布を自動化するパイプラインサービスであり、CISベンチマークやInspectorテストに失敗した場合は自動的に配布をキャンセルするため手動検証より安全なAMI管理が実現できる。

EC2 Image Builder の概要

EC2 Image Builderは安全で最新のAMI(およびコンテナイメージ)を自動的に作成・テスト・配布するサービスだ。OSパッチの自動適用からセキュリティテストまでパイプラインで管理できる。

Image Builder の構成要素:
  1. コンポーネント(Component):
     → AMIに適用するビルドステップを定義
     → パッケージインストール、設定変更等
     → AWSマネージドコンポーネントまたはカスタム

  2. レシピ(Recipe):
     → ベースイメージ + コンポーネントの組み合わせ
     → AMIレシピまたはコンテナレシピ

  3. インフラ設定(Infrastructure Configuration):
     → ビルド用のEC2インスタンスタイプ
     → VPC、セキュリティグループ等

  4. 配布設定(Distribution Configuration):
     → 配布先リージョン、アカウント

  5. パイプライン(Pipeline):
     → スケジュール実行またはオンデマンド実行
     → ビルド → テスト → 配布の自動化

パイプラインの設定

# コンポーネントの作成(カスタムソフトウェアインストール)
aws imagebuilder create-component \
  --name "install-monitoring-agent" \
  --semantic-version "1.0.0" \
  --platform Linux \
  --data '{
    "name": "InstallMonitoringAgent",
    "schemaVersion": "1.0",
    "phases": [
      {
        "name": "build",
        "steps": [
          {
            "name": "InstallCloudWatchAgent",
            "action": "ExecuteBash",
            "inputs": {
              "commands": [
                "wget https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm",
                "rpm -U amazon-cloudwatch-agent.rpm",
                "systemctl enable amazon-cloudwatch-agent"
              ]
            }
          }
        ]
      },
      {
        "name": "validate",
        "steps": [
          {
            "name": "ValidateInstallation",
            "action": "ExecuteBash",
            "inputs": {
              "commands": [
                "systemctl status amazon-cloudwatch-agent"
              ]
            }
          }
        ]
      }
    ]
  }'

AMI レシピの設定

# AMI レシピの作成
aws imagebuilder create-image-recipe \
  --name "company-standard-ami" \
  --semantic-version "1.0.0" \
  --parent-image arn:aws:imagebuilder:ap-northeast-1:aws:image/amazon-linux-2-x86/x.x.x \
  --components '[
    {
      "componentArn": "arn:aws:imagebuilder:ap-northeast-1:aws:component/aws-cli-version-2-linux/x.x.x"
    },
    {
      "componentArn": "arn:aws:imagebuilder:ap-northeast-1:aws:component/update-linux/x.x.x"
    },
    {
      "componentArn": "arn:aws:imagebuilder:ap-northeast-1:123456789012:component/install-monitoring-agent/1.0.0/1"
    }
  ]' \
  --block-device-mappings '[
    {
      "deviceName": "/dev/xvda",
      "ebs": {
        "encrypted": true,
        "volumeType": "gp3",
        "volumeSize": 20
      }
    }
  ]'

テストコンポーネント(CIS ベンチマーク)

# CIS ベンチマークテストを追加
aws imagebuilder create-image-recipe \
  --name "cis-hardened-ami" \
  --components '[
    {
      "componentArn": "arn:aws:imagebuilder:ap-northeast-1:aws:component/cis-hardening-benchmark-level-1-linux/x.x.x"
    },
    {
      "componentArn": "arn:aws:imagebuilder:ap-northeast-1:aws:component/inspector-test-linux/x.x.x"
    }
  ]'
テストフェーズでできること:
  → CISベンチマーク準拠チェック
  → Amazon Inspector v2によるOSパッチチェック
  → カスタムスクリプトによるアプリケーションテスト
  → テスト失敗時はAMI配布をキャンセル

パイプラインの実行スケジュール

# パイプラインの作成(週次でAMI更新)
aws imagebuilder create-image-pipeline \
  --name "weekly-ami-update" \
  --image-recipe-arn arn:aws:imagebuilder:...:image-recipe/company-standard-ami/1.0.0 \
  --infrastructure-configuration-arn arn:aws:imagebuilder:...:infrastructure-configuration/my-infra \
  --distribution-configuration-arn arn:aws:imagebuilder:...:distribution-configuration/multi-region \
  --schedule '{
    "scheduleExpression": "cron(0 0 ? * MON *)",
    "pipelineExecutionStartCondition": "EXPRESSION_MATCH_AND_DEPENDENCY_UPDATES_AVAILABLE"
  }'
EXPRESSION_MATCH_AND_DEPENDENCY_UPDATES_AVAILABLE:
  → スケジュール時刻かつ依存するコンポーネントに更新がある場合のみ実行
  → 変更がなければ自動的にスキップ(コスト最適化)

マルチリージョン配布

# 配布設定(マルチリージョン + クロスアカウント)
aws imagebuilder create-distribution-configuration \
  --name "multi-region-distribution" \
  --distributions '[
    {
      "region": "ap-northeast-1",
      "amiDistributionConfiguration": {
        "name": "company-standard-ami-{{ imagebuilder:buildDate }}",
        "amiTags": {"ImageType": "GoldenAMI", "ManagedBy": "ImageBuilder"},
        "launchPermission": {
          "userIds": ["111111111111", "222222222222"]
        }
      }
    },
    {
      "region": "us-east-1",
      "amiDistributionConfiguration": {
        "name": "company-standard-ami-{{ imagebuilder:buildDate }}",
        "encrypted": true,
        "kmsKeyId": "arn:aws:kms:us-east-1:123456789012:key/xxx"
      }
    }
  ]'

試験頻出ポイント

シナリオ回答
最新OSパッチを適用したAMIの定期更新EC2 Image Builder パイプライン
ゴールデンAMIをマルチリージョンに配布配布設定(Distribution Configuration)
AMI作成後にセキュリティテストを自動実行テストコンポーネント(CIS/Inspector統合)
AMIを複数アカウントに共有Distribution Configuration + launchPermission
AMIとコンテナイメージ両方を作成AMIレシピとコンテナレシピで別途設定

まとめ

EC2 Image Builderはゴールデン AMIの作成・テスト・配布を自動化するパイプラインサービスだ。週次スケジュールでOSパッチを適用し、CISベンチマークテストに合格したAMIのみをマルチリージョンに配布するフローを自動化できる。