【AWS】EC2 Image builder ~ 入門編 ~

■ はじめに

https://dk521123.hatenablog.com/entry/2024/01/23/235028

の続き。

今回は、EC2 Image builder の Hello World をやって
EC2 Image builder がどういう感じなのか雰囲気を味わってみる

目次

【0】作成の流れ
【1】コンポーネント作成
【2】イメージレシピ作成
【3】イメージパイプライン作成
【4】AMI作成

【0】作成の流れ

[1] コンポーネントを作成
[2] コンポーネントからイメージレシピを作成
[3] イメージレシピからイメージパイプラインを作成
[4] イメージパイプラインからAMIを作成

【1】コンポーネント作成

復習:コンポーネントとは?
https://dk521123.hatenablog.com/entry/2024/01/23/235028

* 実際にサーバ構築するためのコマンドを定義するもの

1)手順

[1] AWS Management Console の [EC2 Image builder]-[Components]を選択
[2] 「Create component」ボタン押下
[3] 以下を入力し、「Create component」ボタン押下

Component type

* Type: Build

Component details (以下のもの以外は特に何もせず)

* Image operating system(OS): Linux/Windows (今回は「Linux」)
* Compatible OS Versions: 使用するOS (今回は「Amazon Linux 2023」)
* Component name: 任意の文字列(今回は「demo-component」)
* KMS keys - optional: 使用するKMS (今回は、特に何もせず、デフォルトのまま)
* Component version: 指定するVersion(今回は「0.0.1」)

Definition document

* "Define document content" or "Use example"(今回は「"Use example"」)

Tags (任意だが、今回は以下を行った)

* Name: demo-component

【2】イメージレシピ作成

* コンポーネントからイメージレシピを作成する

復習:イメージレシピとは?
https://dk521123.hatenablog.com/entry/2024/01/23/235028

* 作成するイメージの設計書
* イメージレシピ=ソースイメージ+コンポーネント(ビルド/テスト)

1)手順

[1] AWS Management Console の [EC2 Image builder]-[Image recipes]を選択
[2] 「Create image recipe」ボタン押下
[3] 以下を入力し、「Create recipe」ボタン押下

Recipe details

* Name: 任意の文字列(今回は「demo-recipe」)
* Version: バージョン(今回は「0.0.1」)
* Description - optional:任意(今回は「Hello world」)

Base image

* Select image: 今回は以下の通り
 + "Select manage images"
 + Image Operating System(OS): "Amazon Linux"
 + Image origin: Quick start (Amazon-managed)
 + Image name: Amazon Linux 2023 ARM64
 + Auto-versioning options: Use latest available OS version

Instance configuration

* 特に何もせず(SSM agent も使いたいので)

Working directory

* 特に何もせず (Working directory path: ”/tmp” のまま)

Components

[Step1: Choose build components to produce the desired output AMI]

* Build comments -Amazon Linux
[1] Filter owner: "Owned by me" を選択
[2] Textbox欄に「【1】コンポーネント作成」で
 作成したComponent名を入力し選択(今回は「demo-component」)

[Step2: Optional Select tests to verify the output AMI (post-build)
* 特に何もせず(本当に使う場合はセットしそう、、、)

Storage (volumes) - optional

* 特に何もせず

Tags - optional

今回は、、、
* Name: demo-recipe

【3】イメージパイプライン作成

* イメージレシピからイメージパイプラインを作成する

復習:イメージパイプラインとは?
https://dk521123.hatenablog.com/entry/2024/01/23/235028

* イメージ作成を自動化するための設定
* パイプライン=イメージレシピ+インフラの設定+配布設定+実行契機

1)手順

[1] AWS Management Console の [EC2 Image builder]-[Image pipelines]を選択
[2] 「Create image pipeline」ボタン押下
[3] 以下「2)手順[3] の入力項目」を入力し、「Next」ボタン押下
[4] 以下「3)手順[4] の入力項目」を入力し、「Next」ボタン押下
[5] 「Type: Default workflows」を入力し、「Next」ボタン押下
[6] 「Define infrastructure configuration - optional」で
  "Create infrastructure configuration using service defaults" を入力し、
 「Next」ボタン押下
[7] 「Define distribution settings - optional」で
  "Create distribution settings using service defaults" を入力し、
 「Next」ボタン押下
[8] 「Review」で入力した値を確認し、問題がなければ「Create pipeline」ボタン押下

2)手順[3] の入力項目

General (以下のもの以外は特に何もせず)

* Pipeline name: 任意の文字列(今回は「demo-pipeline」)
* Description: 任意(今回は「Hello world」)

Build schedule

* Schedule options: "Schedule builder" or "CRON expression" or "Manual"
(今回は「Manual」)

Tags - optional

今回は、、、
* Name: demo-pipeline

3)手順[4] の入力項目

Choose recipe - Recipe

* Configuration options: "Use existing recipe" or "Create new recipe"
(今回は「"Use existing recipe"」)

Choose recipe - Recipe

* Recipe name: 「【2】」で作成したレシピ(今回は「demo-recipe」)

【4】AMI作成

* イメージパイプラインからAMIを作成する

復習:AMI (Amazon Machin Image)とは?
https://dk521123.hatenablog.com/entry/2024/01/23/235028

* EC2作成するためのVMイメージ

1)手順

[1] AWS Management Console の [EC2 Image builder]-[Image recipes]を選択
[2] 「【3】イメージパイプライン作成」で作成したパイプラインを選択
(今回は「demo-pipeline」)
[3] 右上に位置する[Actions]-[Run pipeline]を選択
 => 選択後に、「Output images」タブに、
  実行したパイプラインの状態を確認することができる
 => 問題なければ、AMI取得のためEC2が起動する
[4] 「Output images」タブの「Image status」が「Available」
 になったことを確認
 => 相当の時間(1時間以上?)が掛かる。。。
[5] [4]のイメージのバージョンのリンク(今回の場合「0.0.1/1」)を押下
 => 選択後に、「Output resources」タブに、
  実行したイメージのAMIが確認することができる

参考文献

https://qiita.com/miyuki_samitani/items/6d3715ddb5ed8e6cc41f
https://dev.classmethod.jp/articles/ec2-image-builder-primer/

関連記事

EC2 Image builder ~ 基礎知識編 ~
https://dk521123.hatenablog.com/entry/2024/01/23/235028
Amazon EC2 ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2017/02/16/235120