Cloud9でPackerを実行する

「Packer」とは、TerraformでおなじみのHashiCorp社が出しているマシンイメージの作成ツールで、AWS、Azure、GCPなどマルチクラウドに対応している点が売りです。
AWSですと最近「EC2 Image Builder」というサービスが出ましたが、これに近いでしょう。

今回、Cloud9でPackerをインストール&実行して、AMIを作成してみました。

# 手順
## 1. Homebrewをインストール
以下リンク先の手順で一撃です。
**「Cloud9にHomebrewをインストールする手順」**
https://qiita.com/nasuvitz/items/5eec6ab9444cff8e9467

## 2. Packerをインストール
一撃です。

```bash
$ brew install packer
```

## 3. Packerテンプレートを書く
「[Amazon AMI Builder](https://www.packer.io/docs/builders/amazon.html)」を見ながらテンプレートを作ります。
サンプルとして最新のAmazon Linux 2のAMIに、Apacheを追加したAMIを作成します。
AWS CLIで`packertest`というプロファイルを作ってから、以下を作成します。

```json:template.json
{
	"builders": [
		{
			"profile": "packertest",
			"type": "amazon-ebs",
			"region": "ap-northeast-1",
			"ami_name": "packer-test-amzn2",
			"source_ami_filter": {
				"filters": {
					"name": "amzn2-ami-hvm-*-x86_64-gp2"
				},
				"owners": [
					"137112412989"
				],
				"most_recent": true
			},
			"instance_type": "t3.micro",
			"ssh_username": "ec2-user",
			"launch_block_device_mappings": [
				{
					"delete_on_termination": true,
					"device_name": "/dev/sda1",
					"volume_size": 10,
					"volume_type": "gp2"
				}
			]
		}
	],
	"provisioners": [
		{
			"type": "shell",
			"inline": [
				"sudo yum -y install httpd",
				"sudo systemctl start httpd && sudo systemctl enable httpd"
			]
		}
	]
}
```

## 4. Packerテンプレートを実行
まずは正しく書けているか構文チェックします。
以下のように出力されればOKです。

```bash
$ packer validate template.json 
Template validated successfully.
```

構文チェックが済んだら、AMIを作成します。

```bash
$ packer build template.json 

amazon-ebs: output will be in this color.

==> amazon-ebs: Prevalidating any provided VPC information
==> amazon-ebs: Prevalidating AMI Name: packer-test-amzn2
    amazon-ebs: Found Image ID: ami-011facbea5ec0363b
==> amazon-ebs: Creating temporary keypair: packer_5e21a1bb-ab35-2328-38ac-9a12a73d9285
==> amazon-ebs: Creating temporary security group for this instance: packer_5e21a1bc-3ac8-8db3-8477-db550e3c7338
==> amazon-ebs: Authorizing access to port 22 from [0.0.0.0/0] in the temporary security groups...
==> amazon-ebs: Launching a source AWS instance...
==> amazon-ebs: Adding tags to source instance
    amazon-ebs: Adding tag: "Name": "Packer Builder"
    amazon-ebs: Instance ID: i-01775277fbda2e451
==> amazon-ebs: Waiting for instance (i-01775277fbda2e451) to become ready...
==> amazon-ebs: Using ssh communicator to connect: 3.115.22.XXX
==> amazon-ebs: Waiting for SSH to become available...
==> amazon-ebs: Connected to SSH!
==> amazon-ebs: Provisioning with shell script: /tmp/packer-shell846982988
    amazon-ebs: Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
==> amazon-ebs: Existing lock /var/run/yum.pid: another copy is running as pid 2609.
==> amazon-ebs: Another app is currently holding the yum lock; waiting for it to exit...
==> amazon-ebs:   The other application is: yum
==> amazon-ebs:     Memory : 109 M RSS (400 MB VSZ)
==> amazon-ebs:     Started: Fri Jan 17 12:00:21 2020 - 00:06 ago
==> amazon-ebs:     State  : Running, pid: 2609
==> amazon-ebs: Existing lock /var/run/yum.pid: another copy is running as pid 2613.
==> amazon-ebs: Another app is currently holding the yum lock; waiting for it to exit...
==> amazon-ebs:   The other application is: yum
==> amazon-ebs:     Memory :  34 M RSS (251 MB VSZ)
==> amazon-ebs:     Started: Fri Jan 17 12:00:21 2020 - 00:08 ago
==> amazon-ebs:     State  : Running, pid: 2613
    amazon-ebs: Resolving Dependencies
    amazon-ebs: --> Running transaction check
    amazon-ebs: ---> Package awscli.noarch 0:1.16.102-1.amzn2.0.1 will be updated
    amazon-ebs: ---> Package awscli.noarch 0:1.16.300-1.amzn2.0.1 will be an update
    amazon-ebs: ---> Package ca-certificates.noarch 0:2018.2.22-70.0.amzn2.0.1 will be updated
    amazon-ebs: ---> Package ca-certificates.noarch 0:2019.2.32-76.amzn2.0.1 will be an update
    amazon-ebs: ---> Package cloud-init.noarch 0:18.5-2.amzn2 will be updated
    amazon-ebs: ---> Package cloud-init.noarch 0:19.3-2.amzn2 will be an update
    amazon-ebs: ---> Package ec2-net-utils.noarch 0:1.1-1.1.amzn2 will be updated
    amazon-ebs: ---> Package ec2-net-utils.noarch 0:1.2-2.1.amzn2 will be an update
    amazon-ebs: ---> Package glibc.x86_64 0:2.26-32.amzn2.0.2 will be updated
    amazon-ebs: ---> Package glibc.x86_64 0:2.26-33.amzn2 will be an update
    amazon-ebs: ---> Package glibc-all-langpacks.x86_64 0:2.26-32.amzn2.0.2 will be updated
    amazon-ebs: ---> Package glibc-all-langpacks.x86_64 0:2.26-33.amzn2 will be an update
    amazon-ebs: ---> Package glibc-common.x86_64 0:2.26-32.amzn2.0.2 will be updated
    amazon-ebs: ---> Package glibc-common.x86_64 0:2.26-33.amzn2 will be an update
    amazon-ebs: ---> Package glibc-locale-source.x86_64 0:2.26-32.amzn2.0.2 will be updated
    amazon-ebs: ---> Package glibc-locale-source.x86_64 0:2.26-33.amzn2 will be an update
    amazon-ebs: ---> Package glibc-minimal-langpack.x86_64 0:2.26-32.amzn2.0.2 will be updated
    amazon-ebs: ---> Package glibc-minimal-langpack.x86_64 0:2.26-33.amzn2 will be an update
    amazon-ebs: ---> Package irqbalance.x86_64 2:1.5.0-2.amzn2.0.1 will be updated
    amazon-ebs: ---> Package irqbalance.x86_64 2:1.5.0-4.amzn2.0.1 will be an update
    amazon-ebs: ---> Package kernel.x86_64 0:4.14.158-129.185.amzn2 will be installed
    amazon-ebs: ---> Package kernel-tools.x86_64 0:4.14.154-128.181.amzn2 will be updated
    amazon-ebs: ---> Package kernel-tools.x86_64 0:4.14.158-129.185.amzn2 will be an update
    amazon-ebs: ---> Package libcrypt.x86_64 0:2.26-32.amzn2.0.2 will be updated
    amazon-ebs: ---> Package libcrypt.x86_64 0:2.26-33.amzn2 will be an update
    amazon-ebs: ---> Package microcode_ctl.x86_64 2:2.1-47.amzn2.0.4 will be updated
    amazon-ebs: ---> Package microcode_ctl.x86_64 2:2.1-47.amzn2.0.5 will be an update
    amazon-ebs: ---> Package nss-softokn.x86_64 0:3.44.0-5.amzn2.0.2 will be updated
    amazon-ebs: ---> Package nss-softokn.x86_64 0:3.44.0-8.amzn2 will be an update
    amazon-ebs: ---> Package nss-softokn-freebl.x86_64 0:3.44.0-5.amzn2.0.2 will be updated
    amazon-ebs: ---> Package nss-softokn-freebl.x86_64 0:3.44.0-8.amzn2 will be an update
    amazon-ebs: ---> Package nss-util.x86_64 0:3.44.0-3.amzn2.0.2 will be updated
    amazon-ebs: ---> Package nss-util.x86_64 0:3.44.0-4.amzn2 will be an update
    amazon-ebs: ---> Package python.x86_64 0:2.7.16-4.amzn2 will be updated
    amazon-ebs: ---> Package python.x86_64 0:2.7.16-5.amzn2 will be an update
    amazon-ebs: ---> Package python-devel.x86_64 0:2.7.16-4.amzn2 will be updated
    amazon-ebs: ---> Package python-devel.x86_64 0:2.7.16-5.amzn2 will be an update
    amazon-ebs: ---> Package python-libs.x86_64 0:2.7.16-4.amzn2 will be updated
    amazon-ebs: ---> Package python-libs.x86_64 0:2.7.16-5.amzn2 will be an update
    amazon-ebs: ---> Package python2-botocore.noarch 0:1.12.92-2.amzn2.0.1 will be updated
    amazon-ebs: ---> Package python2-botocore.noarch 0:1.13.36-2.amzn2.0.1 will be an update
    amazon-ebs: ---> Package sysstat.x86_64 0:10.1.5-13.amzn2 will be updated
    amazon-ebs: ---> Package sysstat.x86_64 0:10.1.5-18.amzn2.0.1 will be an update
    amazon-ebs: ---> Package systemtap-runtime.x86_64 0:4.1-0.20190208git4e76869512d2.amzn2.0.2 will be updated
    amazon-ebs: ---> Package systemtap-runtime.x86_64 0:4.2-1.amzn2.0.1 will be an update
    amazon-ebs: ---> Package tcpdump.x86_64 14:4.9.2-3.amzn2 will be updated
    amazon-ebs: ---> Package tcpdump.x86_64 14:4.9.2-4.amzn2.1 will be an update
    amazon-ebs: --> Finished Dependency Resolution
    amazon-ebs:
    amazon-ebs: Dependencies Resolved
    amazon-ebs:
    amazon-ebs: ================================================================================
    amazon-ebs:  Package                  Arch     Version                   Repository    Size
    amazon-ebs: ================================================================================
    amazon-ebs: Installing:
    amazon-ebs:  kernel                   x86_64   4.14.158-129.185.amzn2    amzn2-core    20 M
    amazon-ebs: Updating:
    amazon-ebs:  awscli                   noarch   1.16.300-1.amzn2.0.1      amzn2-core   1.6 M
    amazon-ebs:  ca-certificates          noarch   2019.2.32-76.amzn2.0.1    amzn2-core   399 k
    amazon-ebs:  cloud-init               noarch   19.3-2.amzn2              amzn2-core   923 k
    amazon-ebs:  ec2-net-utils            noarch   1.2-2.1.amzn2             amzn2-core    15 k
    amazon-ebs:  glibc                    x86_64   2.26-33.amzn2             amzn2-core   3.3 M
    amazon-ebs:  glibc-all-langpacks      x86_64   2.26-33.amzn2             amzn2-core   7.0 M
    amazon-ebs:  glibc-common             x86_64   2.26-33.amzn2             amzn2-core   768 k
    amazon-ebs:  glibc-locale-source      x86_64   2.26-33.amzn2             amzn2-core   3.2 M
    amazon-ebs:  glibc-minimal-langpack   x86_64   2.26-33.amzn2             amzn2-core    27 k
    amazon-ebs:  irqbalance               x86_64   2:1.5.0-4.amzn2.0.1       amzn2-core    50 k
    amazon-ebs:  kernel-tools             x86_64   4.14.158-129.185.amzn2    amzn2-core   122 k
    amazon-ebs:  libcrypt                 x86_64   2.26-33.amzn2             amzn2-core    47 k
    amazon-ebs:  microcode_ctl            x86_64   2:2.1-47.amzn2.0.5        amzn2-core   157 k
    amazon-ebs:  nss-softokn              x86_64   3.44.0-8.amzn2            amzn2-core   327 k
    amazon-ebs:  nss-softokn-freebl       x86_64   3.44.0-8.amzn2            amzn2-core   225 k
    amazon-ebs:  nss-util                 x86_64   3.44.0-4.amzn2            amzn2-core    78 k
    amazon-ebs:  python                   x86_64   2.7.16-5.amzn2            amzn2-core    92 k
    amazon-ebs:  python-devel             x86_64   2.7.16-5.amzn2            amzn2-core   403 k
    amazon-ebs:  python-libs              x86_64   2.7.16-5.amzn2            amzn2-core   7.4 M
    amazon-ebs:  python2-botocore         noarch   1.13.36-2.amzn2.0.1       amzn2-core   3.8 M
    amazon-ebs:  sysstat                  x86_64   10.1.5-18.amzn2.0.1       amzn2-core   317 k
    amazon-ebs:  systemtap-runtime        x86_64   4.2-1.amzn2.0.1           amzn2-core   472 k
    amazon-ebs:  tcpdump                  x86_64   14:4.9.2-4.amzn2.1        amzn2-core   424 k
    amazon-ebs:
    amazon-ebs: Transaction Summary
    amazon-ebs: ================================================================================
    amazon-ebs: Install   1 Package
    amazon-ebs: Upgrade  23 Packages
    amazon-ebs:
    amazon-ebs: Total download size: 52 M
    amazon-ebs: Downloading packages:
    amazon-ebs: Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
    amazon-ebs: --------------------------------------------------------------------------------
    amazon-ebs: Total                                               48 MB/s |  52 MB  00:01
    amazon-ebs: Running transaction check
    amazon-ebs: Running transaction test
    amazon-ebs: Transaction test succeeded
    amazon-ebs: Running transaction
    amazon-ebs:   Updating   : glibc-common-2.26-33.amzn2.x86_64                           1/47
    amazon-ebs:   Updating   : glibc-minimal-langpack-2.26-33.amzn2.x86_64                 2/47
    amazon-ebs:   Updating   : glibc-2.26-33.amzn2.x86_64                                  3/47
    amazon-ebs:   Updating   : nss-util-3.44.0-4.amzn2.x86_64                              4/47
    amazon-ebs:   Updating   : nss-softokn-freebl-3.44.0-8.amzn2.x86_64                    5/47
    amazon-ebs:   Updating   : 2:microcode_ctl-2.1-47.amzn2.0.5.x86_64                     6/47
    amazon-ebs:   Updating   : libcrypt-2.26-33.amzn2.x86_64                               7/47
    amazon-ebs:   Updating   : python-libs-2.7.16-5.amzn2.x86_64                           8/47
    amazon-ebs:   Updating   : python-2.7.16-5.amzn2.x86_64                                9/47
    amazon-ebs:   Updating   : python-devel-2.7.16-5.amzn2.x86_64                         10/47
    amazon-ebs:   Updating   : python2-botocore-1.13.36-2.amzn2.0.1.noarch                11/47
    amazon-ebs:   Updating   : awscli-1.16.300-1.amzn2.0.1.noarch                         12/47
    amazon-ebs:   Updating   : cloud-init-19.3-2.amzn2.noarch                             13/47
    amazon-ebs:   Installing : kernel-4.14.158-129.185.amzn2.x86_64                       14/47
    amazon-ebs:   Updating   : nss-softokn-3.44.0-8.amzn2.x86_64                          15/47
    amazon-ebs:   Updating   : systemtap-runtime-4.2-1.amzn2.0.1.x86_64                   16/47
    amazon-ebs:   Updating   : sysstat-10.1.5-18.amzn2.0.1.x86_64                         17/47
    amazon-ebs:   Updating   : 2:irqbalance-1.5.0-4.amzn2.0.1.x86_64                      18/47
    amazon-ebs:   Updating   : glibc-all-langpacks-2.26-33.amzn2.x86_64                   19/47
    amazon-ebs:   Updating   : kernel-tools-4.14.158-129.185.amzn2.x86_64                 20/47
    amazon-ebs:   Updating   : 14:tcpdump-4.9.2-4.amzn2.1.x86_64                          21/47
    amazon-ebs:   Updating   : glibc-locale-source-2.26-33.amzn2.x86_64                   22/47
    amazon-ebs:   Updating   : ec2-net-utils-1.2-2.1.amzn2.noarch                         23/47
    amazon-ebs:   Updating   : ca-certificates-2019.2.32-76.amzn2.0.1.noarch              24/47
    amazon-ebs:   Cleanup    : cloud-init-18.5-2.amzn2.noarch                             25/47
    amazon-ebs:   Cleanup    : python-devel-2.7.16-4.amzn2.x86_64                         26/47
    amazon-ebs:   Cleanup    : glibc-locale-source-2.26-32.amzn2.0.2.x86_64               27/47
    amazon-ebs:   Cleanup    : awscli-1.16.102-1.amzn2.0.1.noarch                         28/47
    amazon-ebs:   Cleanup    : glibc-all-langpacks-2.26-32.amzn2.0.2.x86_64               29/47
    amazon-ebs:   Cleanup    : nss-softokn-3.44.0-5.amzn2.0.2.x86_64                      30/47
    amazon-ebs:   Cleanup    : nss-softokn-freebl-3.44.0-5.amzn2.0.2.x86_64               31/47
    amazon-ebs:   Cleanup    : systemtap-runtime-4.1-0.20190208git4e76869512d2.amzn2.0.   32/47
    amazon-ebs:   Cleanup    : python2-botocore-1.12.92-2.amzn2.0.1.noarch                33/47
    amazon-ebs:   Cleanup    : python-2.7.16-4.amzn2.x86_64                               34/47
    amazon-ebs:   Cleanup    : python-libs-2.7.16-4.amzn2.x86_64                          35/47
    amazon-ebs:   Cleanup    : libcrypt-2.26-32.amzn2.0.2.x86_64                          36/47
    amazon-ebs:   Cleanup    : nss-util-3.44.0-3.amzn2.0.2.x86_64                         37/47
    amazon-ebs:   Cleanup    : 14:tcpdump-4.9.2-3.amzn2.x86_64                            38/47
    amazon-ebs:   Cleanup    : kernel-tools-4.14.154-128.181.amzn2.x86_64                 39/47
    amazon-ebs:   Cleanup    : 2:irqbalance-1.5.0-2.amzn2.0.1.x86_64                      40/47
    amazon-ebs:   Cleanup    : 2:microcode_ctl-2.1-47.amzn2.0.4.x86_64                    41/47
    amazon-ebs:   Cleanup    : sysstat-10.1.5-13.amzn2.x86_64                             42/47
    amazon-ebs:   Cleanup    : ec2-net-utils-1.1-1.1.amzn2.noarch                         43/47
    amazon-ebs:   Cleanup    : ca-certificates-2018.2.22-70.0.amzn2.0.1.noarch            44/47
    amazon-ebs:   Cleanup    : glibc-common-2.26-32.amzn2.0.2.x86_64                      45/47
    amazon-ebs:   Cleanup    : glibc-minimal-langpack-2.26-32.amzn2.0.2.x86_64            46/47
    amazon-ebs:   Cleanup    : glibc-2.26-32.amzn2.0.2.x86_64                             47/47
    amazon-ebs: /sbin/dracut: line 655: warning: setlocale: LC_MESSAGES: cannot change locale (en_US.UTF-8): No such file or directory
    amazon-ebs: /sbin/dracut: line 656: warning: setlocale: LC_CTYPE: cannot change locale (en_US.UTF-8): No such file or directory
    amazon-ebs: /sbin/dracut: line 655: warning: setlocale: LC_MESSAGES: cannot change locale (en_US.UTF-8): No such file or directory
    amazon-ebs: /sbin/dracut: line 656: warning: setlocale: LC_CTYPE: cannot change locale (en_US.UTF-8): No such file or directory
    amazon-ebs:   Verifying  : sysstat-10.1.5-18.amzn2.0.1.x86_64                          1/47
    amazon-ebs:   Verifying  : 2:microcode_ctl-2.1-47.amzn2.0.5.x86_64                     2/47
    amazon-ebs:   Verifying  : 2:irqbalance-1.5.0-4.amzn2.0.1.x86_64                       3/47
    amazon-ebs:   Verifying  : glibc-2.26-33.amzn2.x86_64                                  4/47
    amazon-ebs:   Verifying  : glibc-all-langpacks-2.26-33.amzn2.x86_64                    5/47
    amazon-ebs:   Verifying  : nss-util-3.44.0-4.amzn2.x86_64                              6/47
    amazon-ebs:   Verifying  : kernel-tools-4.14.158-129.185.amzn2.x86_64                  7/47
    amazon-ebs:   Verifying  : glibc-common-2.26-33.amzn2.x86_64                           8/47
    amazon-ebs:   Verifying  : awscli-1.16.300-1.amzn2.0.1.noarch                          9/47
    amazon-ebs:   Verifying  : glibc-minimal-langpack-2.26-33.amzn2.x86_64                10/47
    amazon-ebs:   Verifying  : python-devel-2.7.16-5.amzn2.x86_64                         11/47
    amazon-ebs:   Verifying  : ca-certificates-2019.2.32-76.amzn2.0.1.noarch              12/47
    amazon-ebs:   Verifying  : python-2.7.16-5.amzn2.x86_64                               13/47
    amazon-ebs:   Verifying  : 14:tcpdump-4.9.2-4.amzn2.1.x86_64                          14/47
    amazon-ebs:   Verifying  : ec2-net-utils-1.2-2.1.amzn2.noarch                         15/47
    amazon-ebs:   Verifying  : systemtap-runtime-4.2-1.amzn2.0.1.x86_64                   16/47
    amazon-ebs:   Verifying  : python-libs-2.7.16-5.amzn2.x86_64                          17/47
    amazon-ebs:   Verifying  : python2-botocore-1.13.36-2.amzn2.0.1.noarch                18/47
    amazon-ebs:   Verifying  : glibc-locale-source-2.26-33.amzn2.x86_64                   19/47
    amazon-ebs:   Verifying  : cloud-init-19.3-2.amzn2.noarch                             20/47
    amazon-ebs:   Verifying  : kernel-4.14.158-129.185.amzn2.x86_64                       21/47
    amazon-ebs:   Verifying  : libcrypt-2.26-33.amzn2.x86_64                              22/47
    amazon-ebs:   Verifying  : nss-softokn-freebl-3.44.0-8.amzn2.x86_64                   23/47
    amazon-ebs:   Verifying  : nss-softokn-3.44.0-8.amzn2.x86_64                          24/47
    amazon-ebs:   Verifying  : kernel-tools-4.14.154-128.181.amzn2.x86_64                 25/47
    amazon-ebs:   Verifying  : sysstat-10.1.5-13.amzn2.x86_64                             26/47
    amazon-ebs:   Verifying  : glibc-common-2.26-32.amzn2.0.2.x86_64                      27/47
    amazon-ebs:   Verifying  : python-libs-2.7.16-4.amzn2.x86_64                          28/47
    amazon-ebs:   Verifying  : python-2.7.16-4.amzn2.x86_64                               29/47
    amazon-ebs:   Verifying  : cloud-init-18.5-2.amzn2.noarch                             30/47
    amazon-ebs:   Verifying  : glibc-all-langpacks-2.26-32.amzn2.0.2.x86_64               31/47
    amazon-ebs:   Verifying  : libcrypt-2.26-32.amzn2.0.2.x86_64                          32/47
    amazon-ebs:   Verifying  : glibc-minimal-langpack-2.26-32.amzn2.0.2.x86_64            33/47
    amazon-ebs:   Verifying  : ca-certificates-2018.2.22-70.0.amzn2.0.1.noarch            34/47
    amazon-ebs:   Verifying  : 2:irqbalance-1.5.0-2.amzn2.0.1.x86_64                      35/47
    amazon-ebs:   Verifying  : 14:tcpdump-4.9.2-3.amzn2.x86_64                            36/47
    amazon-ebs:   Verifying  : python2-botocore-1.12.92-2.amzn2.0.1.noarch                37/47
    amazon-ebs:   Verifying  : nss-softokn-freebl-3.44.0-5.amzn2.0.2.x86_64               38/47
    amazon-ebs:   Verifying  : nss-util-3.44.0-3.amzn2.0.2.x86_64                         39/47
    amazon-ebs:   Verifying  : 2:microcode_ctl-2.1-47.amzn2.0.4.x86_64                    40/47
    amazon-ebs:   Verifying  : ec2-net-utils-1.1-1.1.amzn2.noarch                         41/47
    amazon-ebs:   Verifying  : systemtap-runtime-4.1-0.20190208git4e76869512d2.amzn2.0.   42/47
    amazon-ebs:   Verifying  : nss-softokn-3.44.0-5.amzn2.0.2.x86_64                      43/47
    amazon-ebs:   Verifying  : glibc-2.26-32.amzn2.0.2.x86_64                             44/47
    amazon-ebs:   Verifying  : python-devel-2.7.16-4.amzn2.x86_64                         45/47
    amazon-ebs:   Verifying  : awscli-1.16.102-1.amzn2.0.1.noarch                         46/47
    amazon-ebs:   Verifying  : glibc-locale-source-2.26-32.amzn2.0.2.x86_64               47/47
    amazon-ebs:
    amazon-ebs: Installed:
    amazon-ebs:   kernel.x86_64 0:4.14.158-129.185.amzn2
    amazon-ebs:
    amazon-ebs: Updated:
    amazon-ebs:   awscli.noarch 0:1.16.300-1.amzn2.0.1
    amazon-ebs:   ca-certificates.noarch 0:2019.2.32-76.amzn2.0.1
    amazon-ebs:   cloud-init.noarch 0:19.3-2.amzn2
    amazon-ebs:   ec2-net-utils.noarch 0:1.2-2.1.amzn2
    amazon-ebs:   glibc.x86_64 0:2.26-33.amzn2
    amazon-ebs:   glibc-all-langpacks.x86_64 0:2.26-33.amzn2
    amazon-ebs:   glibc-common.x86_64 0:2.26-33.amzn2
    amazon-ebs:   glibc-locale-source.x86_64 0:2.26-33.amzn2
    amazon-ebs:   glibc-minimal-langpack.x86_64 0:2.26-33.amzn2
    amazon-ebs:   irqbalance.x86_64 2:1.5.0-4.amzn2.0.1
    amazon-ebs:   kernel-tools.x86_64 0:4.14.158-129.185.amzn2
    amazon-ebs:   libcrypt.x86_64 0:2.26-33.amzn2
    amazon-ebs:   microcode_ctl.x86_64 2:2.1-47.amzn2.0.5
    amazon-ebs:   nss-softokn.x86_64 0:3.44.0-8.amzn2
    amazon-ebs:   nss-softokn-freebl.x86_64 0:3.44.0-8.amzn2
    amazon-ebs:   nss-util.x86_64 0:3.44.0-4.amzn2
    amazon-ebs:   python.x86_64 0:2.7.16-5.amzn2
    amazon-ebs:   python-devel.x86_64 0:2.7.16-5.amzn2
    amazon-ebs:   python-libs.x86_64 0:2.7.16-5.amzn2
    amazon-ebs:   python2-botocore.noarch 0:1.13.36-2.amzn2.0.1
    amazon-ebs:   sysstat.x86_64 0:10.1.5-18.amzn2.0.1
    amazon-ebs:   systemtap-runtime.x86_64 0:4.2-1.amzn2.0.1
    amazon-ebs:   tcpdump.x86_64 14:4.9.2-4.amzn2.1
    amazon-ebs:
    amazon-ebs: Complete!
    amazon-ebs: Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
    amazon-ebs: Resolving Dependencies
    amazon-ebs: --> Running transaction check
    amazon-ebs: ---> Package httpd.x86_64 0:2.4.41-1.amzn2.0.1 will be installed
    amazon-ebs: --> Processing Dependency: httpd-tools = 2.4.41-1.amzn2.0.1 for package: httpd-2.4.41-1.amzn2.0.1.x86_64
    amazon-ebs: --> Processing Dependency: httpd-filesystem = 2.4.41-1.amzn2.0.1 for package: httpd-2.4.41-1.amzn2.0.1.x86_64
    amazon-ebs: --> Processing Dependency: system-logos-httpd for package: httpd-2.4.41-1.amzn2.0.1.x86_64
    amazon-ebs: --> Processing Dependency: mod_http2 for package: httpd-2.4.41-1.amzn2.0.1.x86_64
    amazon-ebs: --> Processing Dependency: httpd-filesystem for package: httpd-2.4.41-1.amzn2.0.1.x86_64
    amazon-ebs: --> Processing Dependency: /etc/mime.types for package: httpd-2.4.41-1.amzn2.0.1.x86_64
    amazon-ebs: --> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.41-1.amzn2.0.1.x86_64
    amazon-ebs: --> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.41-1.amzn2.0.1.x86_64
    amazon-ebs: --> Running transaction check
    amazon-ebs: ---> Package apr.x86_64 0:1.6.3-5.amzn2.0.2 will be installed
    amazon-ebs: ---> Package apr-util.x86_64 0:1.6.1-5.amzn2.0.2 will be installed
    amazon-ebs: --> Processing Dependency: apr-util-bdb(x86-64) = 1.6.1-5.amzn2.0.2 for package: apr-util-1.6.1-5.amzn2.0.2.x86_64
    amazon-ebs: ---> Package generic-logos-httpd.noarch 0:18.0.0-4.amzn2 will be installed
    amazon-ebs: ---> Package httpd-filesystem.noarch 0:2.4.41-1.amzn2.0.1 will be installed
    amazon-ebs: ---> Package httpd-tools.x86_64 0:2.4.41-1.amzn2.0.1 will be installed
    amazon-ebs: ---> Package mailcap.noarch 0:2.1.41-2.amzn2 will be installed
    amazon-ebs: ---> Package mod_http2.x86_64 0:1.15.3-2.amzn2 will be installed
    amazon-ebs: --> Running transaction check
    amazon-ebs: ---> Package apr-util-bdb.x86_64 0:1.6.1-5.amzn2.0.2 will be installed
    amazon-ebs: --> Finished Dependency Resolution
    amazon-ebs:
    amazon-ebs: Dependencies Resolved
    amazon-ebs:
    amazon-ebs: ================================================================================
    amazon-ebs:  Package                 Arch       Version                Repository      Size
    amazon-ebs: ================================================================================
    amazon-ebs: Installing:
    amazon-ebs:  httpd                   x86_64     2.4.41-1.amzn2.0.1     amzn2-core     1.3 M
    amazon-ebs: Installing for dependencies:
    amazon-ebs:  apr                     x86_64     1.6.3-5.amzn2.0.2      amzn2-core     118 k
    amazon-ebs:  apr-util                x86_64     1.6.1-5.amzn2.0.2      amzn2-core      99 k
    amazon-ebs:  apr-util-bdb            x86_64     1.6.1-5.amzn2.0.2      amzn2-core      19 k
    amazon-ebs:  generic-logos-httpd     noarch     18.0.0-4.amzn2         amzn2-core      19 k
    amazon-ebs:  httpd-filesystem        noarch     2.4.41-1.amzn2.0.1     amzn2-core      23 k
    amazon-ebs:  httpd-tools             x86_64     2.4.41-1.amzn2.0.1     amzn2-core      87 k
    amazon-ebs:  mailcap                 noarch     2.1.41-2.amzn2         amzn2-core      31 k
    amazon-ebs:  mod_http2               x86_64     1.15.3-2.amzn2         amzn2-core     146 k
    amazon-ebs:
    amazon-ebs: Transaction Summary
    amazon-ebs: ================================================================================
    amazon-ebs: Install  1 Package (+8 Dependent packages)
    amazon-ebs:
    amazon-ebs: Total download size: 1.8 M
    amazon-ebs: Installed size: 5.1 M
    amazon-ebs: Downloading packages:
    amazon-ebs: --------------------------------------------------------------------------------
    amazon-ebs: Total                                               11 MB/s | 1.8 MB  00:00
    amazon-ebs: Running transaction check
    amazon-ebs: Running transaction test
    amazon-ebs: Transaction test succeeded
    amazon-ebs: Running transaction
    amazon-ebs:   Installing : apr-1.6.3-5.amzn2.0.2.x86_64                                 1/9
    amazon-ebs:   Installing : apr-util-bdb-1.6.1-5.amzn2.0.2.x86_64                        2/9
    amazon-ebs:   Installing : apr-util-1.6.1-5.amzn2.0.2.x86_64                            3/9
    amazon-ebs:   Installing : httpd-tools-2.4.41-1.amzn2.0.1.x86_64                        4/9
    amazon-ebs:   Installing : generic-logos-httpd-18.0.0-4.amzn2.noarch                    5/9
    amazon-ebs:   Installing : mailcap-2.1.41-2.amzn2.noarch                                6/9
    amazon-ebs:   Installing : httpd-filesystem-2.4.41-1.amzn2.0.1.noarch                   7/9
    amazon-ebs:   Installing : mod_http2-1.15.3-2.amzn2.x86_64                              8/9
    amazon-ebs:   Installing : httpd-2.4.41-1.amzn2.0.1.x86_64                              9/9
    amazon-ebs:   Verifying  : apr-util-1.6.1-5.amzn2.0.2.x86_64                            1/9
    amazon-ebs:   Verifying  : apr-util-bdb-1.6.1-5.amzn2.0.2.x86_64                        2/9
    amazon-ebs:   Verifying  : httpd-2.4.41-1.amzn2.0.1.x86_64                              3/9
    amazon-ebs:   Verifying  : httpd-filesystem-2.4.41-1.amzn2.0.1.noarch                   4/9
    amazon-ebs:   Verifying  : mod_http2-1.15.3-2.amzn2.x86_64                              5/9
    amazon-ebs:   Verifying  : apr-1.6.3-5.amzn2.0.2.x86_64                                 6/9
    amazon-ebs:   Verifying  : mailcap-2.1.41-2.amzn2.noarch                                7/9
    amazon-ebs:   Verifying  : generic-logos-httpd-18.0.0-4.amzn2.noarch                    8/9
    amazon-ebs:   Verifying  : httpd-tools-2.4.41-1.amzn2.0.1.x86_64                        9/9
    amazon-ebs:
    amazon-ebs: Installed:
    amazon-ebs:   httpd.x86_64 0:2.4.41-1.amzn2.0.1
    amazon-ebs:
    amazon-ebs: Dependency Installed:
    amazon-ebs:   apr.x86_64 0:1.6.3-5.amzn2.0.2
    amazon-ebs:   apr-util.x86_64 0:1.6.1-5.amzn2.0.2
    amazon-ebs:   apr-util-bdb.x86_64 0:1.6.1-5.amzn2.0.2
    amazon-ebs:   generic-logos-httpd.noarch 0:18.0.0-4.amzn2
    amazon-ebs:   httpd-filesystem.noarch 0:2.4.41-1.amzn2.0.1
    amazon-ebs:   httpd-tools.x86_64 0:2.4.41-1.amzn2.0.1
    amazon-ebs:   mailcap.noarch 0:2.1.41-2.amzn2
    amazon-ebs:   mod_http2.x86_64 0:1.15.3-2.amzn2
    amazon-ebs:
    amazon-ebs: Complete!
==> amazon-ebs: Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
==> amazon-ebs: Stopping the source instance...
    amazon-ebs: Stopping instance
==> amazon-ebs: Waiting for the instance to stop...
==> amazon-ebs: Creating AMI packer-test-amzn2 from instance i-01775277fbda2e451
    amazon-ebs: AMI: ami-0c4844c78732a5179
==> amazon-ebs: Waiting for AMI to become ready...
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: Cleaning up any extra volumes...
==> amazon-ebs: No volumes to clean up, skipping
==> amazon-ebs: Deleting temporary security group...
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' finished.

==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs: AMIs were created:
ap-northeast-1: ami-0c4844c78732a5179
```

実行中、Packer BuliderというEC2が起動し、このEC2のAMIが取られることで、新たなAMIが出来上がります。
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211986/c12369bd-49a7-2c0a-142e-dd9359fe4292.png)

出来上がったAMIを確認してみます。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/211986/05cf7324-acfc-47f7-1eda-78e5e471f4ff.png)

Packerを使って、いとも簡単に、カスタムAMIを作成することができました。


# サンプル実行時に気になるところ
* **一瞬、SSHが全開放になっている**
    * 「Authorizing access to port 22 from [0.0.0.0/0] in the temporary security groups」 という実行ログがあります。
    * Packerのssh communicatorが、Packer BuliderのEC2に接続するようです。
* **一瞬、yumが競合している**
    * 「Another app is currently holding the yum lock; waiting for it to exit...」という実行ログがあります。
    * Amazon Linux 2をAMIから初回起動するとき、[最新セキュリティパッチが必ず当たるようになっています](https://qiita.com/nasuvitz/items/7a1f0d657fd5ea7bd950#amazon-linux-2)ので、その処理と競合したと思われます。
    * OSの初回起動時のセキュリティパッチ適用処理の方が先に実行され、その後、Packerで定義した`provisioners`の処理が実行されるようです。

# まとめ
Terraformと同じく、AWS、Azure、GCPなどマルチクラウドに対応する際に、非常に有効なツールです。
AWSだけならEC2 Image Builderの選択肢もありますが、Packerは操作が簡単であり、AWS専用でも選択肢に入ります。

# 参考文献
* [Amazon AMI Builder](https://www.packer.io/docs/builders/amazon.html)