[アップデート] EC2 Image Builder のリソースを CloudFormation で作成できるようになりました!

2020.05.11

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

CloudFormation によって EC2 Image Builder のリソースを作成できるようになりました。
Component のみならず Image Pipeline 全体までコードで管理できるようになったため、より利便性が向上しそうです。

EC2 Image Builder now includes support for AWS CloudFormation

EC2 Image Builderとは

EC2 Image Builder は EC2 のイメージの作成・更新を専用のパイプラインによって簡素化・迅速化する AWS サービスです。
サービスの詳細についてはリリース当時のブログや公式ページなどをご参照ください。

EC2のイメージ作成を劇的に効率化するEC2 Image Builderが発表されました!
EC2 Image Builder(AWS公式ページ)

やってみた

こちらのブログで書いたような Image Pipeline を作成するテンプレートを書いてみました。
OS(ディストリビューション)には Amazon Linux 2 を指定しています。

サンプルテンプレート

AWSTemplateFormatVersion: 2010-09-09
Resources:
  Pipeline:
    Type: AWS::ImageBuilder::ImagePipeline
    Properties: 
      ImageRecipeArn: !Ref Recipe
      InfrastructureConfigurationArn: !Ref InfrastructureConfiguration
      Name: CFn-Pipeline
  InfrastructureConfiguration:
    Type: AWS::ImageBuilder::InfrastructureConfiguration
    Properties:
      InstanceProfileName: !Ref InstanceProfile
      InstanceTypes: []
      Name: CFn-InfrastructureConfiguration
      SecurityGroupIds: []
      TerminateInstanceOnFailure: True
  InstanceProfile:
    Type: AWS::IAM::InstanceProfile
    Properties: 
      InstanceProfileName: ImageBuilderInstanceProfile
      Roles: 
        - EC2ImageBuilder
  Component:
    Type: AWS::ImageBuilder::Component
    Properties: 
      Data: |
        name: InstallApache
        description: InstallApache
        schemaVersion: 1.0
        
        phases:
          - name: build
            steps:
              - name: UpdateOS
                action: UpdateOS
              - name: InstallApache
                action: ExecuteBash
                inputs:
                  commands:
                    - sudo yum -y install httpd
                    - sudo systemctl enable httpd
        
          - name: validate
            steps:
              - name: CheckApache
                action: ExecuteBash
                inputs:
                  commands:
                    - rpm -qi httpd
        
          - name: test
            steps:
              - name: TestApache
                action: ExecuteBash
                inputs:
                  commands:
                    - sudo systemctl status httpd
      Name: CFn-Component
      Platform: Linux
      Version: 1.0.0
  Recipe:
    Type: AWS::ImageBuilder::ImageRecipe
    Properties: 
      Components: 
        - ComponentArn: !Ref Component
      Name: CFn-Recipe
      ParentImage: arn:aws:imagebuilder:ap-northeast-1:aws:image/amazon-linux-2-x86/2020.4.7
      Version: 1.0.0

注意点

  • テンプレート内で使用しているIAMロールEC2ImageBuilderはテンプレート外で作成されており、下記の AWS 管理ポリシーが割り当てられています。
    • EC2InstanceProfileForImageBuilder
    • AmazonSSMManagedInstanceCore
  • リソースType: AWS::ImageBuilder::ImageRecipeParentImageには下記のいずれかを設定する必要があります。
    • EC2 Image Builder によって提供されている Managed Images(後述) の ARN
    • SSM Agent がインストールされたカスタムイメージの AMI ID

Managed Images の探し方

マネージメントコンソールから

対象OSを選択しBrowse imagesをクリックすることで一覧できます。

AWS CLIから

aws imagebuilder list-imagesコマンド(list-images)をお使いください。

たとえば、 AWS によって提供されている Linux の Managed Images を探す場合は下記のとおりです。

$ aws imagebuilder list-images --owner Amazon --filters "name=platform,values=Linux" --query "imageVersionList[*].[name,arn,version]" --output table
------------------------------------------------------------------------------------------------------------------------------------------------------
|                                                                     ListImages                                                                     |
+--------------------------------------+-----------------------------------------------------------------------------------------------+-------------+
|  Amazon Linux 2 x86                  |  arn:aws:imagebuilder:ap-northeast-1:aws:image/amazon-linux-2-x86/2019.11.21                  |  2019.11.21 |
|  Amazon Linux 2 x86                  |  arn:aws:imagebuilder:ap-northeast-1:aws:image/amazon-linux-2-x86/2020.1.8                    |  2020.1.8   |
|  Amazon Linux 2 x86                  |  arn:aws:imagebuilder:ap-northeast-1:aws:image/amazon-linux-2-x86/2020.2.13                   |  2020.2.13  |
|  Amazon Linux 2 x86                  |  arn:aws:imagebuilder:ap-northeast-1:aws:image/amazon-linux-2-x86/2020.3.7                    |  2020.3.7   |
|  Amazon Linux 2 x86                  |  arn:aws:imagebuilder:ap-northeast-1:aws:image/amazon-linux-2-x86/2020.4.7                    |  2020.4.7   |
|  CentOS Linux 7 Minimal x86          |  arn:aws:imagebuilder:ap-northeast-1:aws:image/centos-linux-7-minimal-x86/2020.3.9            |  2020.3.9   |
|  Red Hat Enterprise Linux 7 x86      |  arn:aws:imagebuilder:ap-northeast-1:aws:image/red-hat-enterprise-linux-7-x86/2018.8.15       |  2018.8.15  |
|  Red Hat Enterprise Linux 8 x86      |  arn:aws:imagebuilder:ap-northeast-1:aws:image/red-hat-enterprise-linux-8-x86/2019.6.19       |  2019.6.19  |
|  SUSE Linux Enterprise Server 15 x86 |  arn:aws:imagebuilder:ap-northeast-1:aws:image/suse-linux-enterprise-server-15-x86/2020.4.13  |  2020.4.13  |
|  SUSE Linux Enterprise Server 15 x86 |  arn:aws:imagebuilder:ap-northeast-1:aws:image/suse-linux-enterprise-server-15-x86/2020.5.1   |  2020.5.1   |
|  SUSE Linux Enterprise Server 15 x86 |  arn:aws:imagebuilder:ap-northeast-1:aws:image/suse-linux-enterprise-server-15-x86/2020.5.2   |  2020.5.2   |
|  SUSE Linux Enterprise Server 15 x86 |  arn:aws:imagebuilder:ap-northeast-1:aws:image/suse-linux-enterprise-server-15-x86/2020.5.7   |  2020.5.7   |
|  Ubuntu Server 16 LTS x86            |  arn:aws:imagebuilder:ap-northeast-1:aws:image/ubuntu-server-16-lts-x86/2020.4.29             |  2020.4.29  |
|  Ubuntu Server 16 LTS x86            |  arn:aws:imagebuilder:ap-northeast-1:aws:image/ubuntu-server-16-lts-x86/2020.4.9              |  2020.4.9   |
|  Ubuntu Server 18 LTS x86            |  arn:aws:imagebuilder:ap-northeast-1:aws:image/ubuntu-server-18-lts-x86/2020.4.9              |  2020.4.9   |
|  Ubuntu Server 18 LTS x86            |  arn:aws:imagebuilder:ap-northeast-1:aws:image/ubuntu-server-18-lts-x86/2020.5.7              |  2020.5.7   |
+--------------------------------------+-----------------------------------------------------------------------------------------------+-------------+

参考

ImageBuilder Resource Type Reference

終わりに

このブログがほんの少しでも世界を良くできれば嬉しいです。
コンサルティング部の西野(@xiyegen)がお送りしました。