0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

DevOpsAdvent Calendar 2018

Day 8

Packer でイメージ作る最中に色々やってみる

Posted at

Packer でイメージ作る最中に色々やってみる

日越してしまった・・・
昨日 の続きです。

nginx をイメージ作成の際に入れてみる

昨日作った example.json をいじってみます。

example.json
{
  "variables": {
    "aws_access_key": "",
    "aws_secret_key": ""
  },
  "builders": [{
    "type": "amazon-ebs",
    "access_key": "{{user `aws_access_key`}}",
    "secret_key": "{{user `aws_secret_key`}}",
    "region": "ap-northeast-1",
    "source_ami_filter": {
      "filters": {
        "virtualization-type": "hvm",
        "name": "ubuntu/images/*ubuntu-bionic-18.04-amd64-server-*",
        "root-device-type": "ebs"
      },
      "owners": ["099720109477"],
      "most_recent": true
    },
    "instance_type": "t3.micro",
    "ssh_username": "ubuntu",
    "ami_name": "packer-example {{timestamp}}"
  }],
  "provisioners": [
    {
      "type": "shell",
      "inline": [
        "sleep 30",
        "sudo apt-get update",
        "sudo apt-get install -y nginx"
      ]
    }
  ]
}

"name": "ubuntu/images/*ubuntu-bionic-18.04-amd64-server-*"

ちょっと、ubuntu を 16.04 から 18.04 にしてみました。

"instance_type": "t3.micro",

インスタンスタイプを新しいやつに・・・
⇒これは後で気づきましたが、課金かかるので、無料枠でやってる人は注意してください・・・

provisioners の項目が、今日の主体です。
シェルで、nginx をインストールしてます。
apt-getsudo で動かさないと絶賛エラーを吐きます。

やってみた

> ./packer build -var 'aws_access_key=<access key>' -var 'aws_secret_key=<secret key>' example.json
amazon-ebs output will be in this color.

==> amazon-ebs: Prevalidating AMI Name: packer-example 1544282532
    amazon-ebs: Found Image ID: ami-0f2c38ac2e37197be
==> amazon-ebs: Creating temporary keypair: packer_5c0be1a4-d087-d394-3b9d-26182f55fd9e
==> amazon-ebs: Creating temporary security group for this instance: packer_5c0be1a6-6df5-0494-8621-d7699f5986c1
==> amazon-ebs: Authorizing access to port 22 from 0.0.0.0/0 in the temporary security group...
==> 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-0c0c59321671f31a9
==> amazon-ebs: Waiting for instance (i-0c0c59321671f31a9) to become ready...
==> amazon-ebs: Using ssh communicator to connect: 52.195.14.252
==> amazon-ebs: Waiting for SSH to become available...
==> amazon-ebs: Connected to SSH!
==> amazon-ebs: Provisioning with shell script: /tmp/packer-shell918288955
    amazon-ebs: Hit:1 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic InRelease
    amazon-ebs: Get:2 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
    amazon-ebs: Get:3 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
    amazon-ebs: Get:4 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/multiverse Sources [181 kB]
    amazon-ebs: Get:5 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/main Sources [829 kB]
    amazon-ebs: Get:6 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/universe Sources [9051 kB]
    amazon-ebs: Get:7 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/restricted Sources [5324 B]
    amazon-ebs: Get:8 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/universe amd64 Packages [8570 kB]
    amazon-ebs: Get:9 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]
    amazon-ebs: Get:10 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/universe Translation-en [4941 kB]
    amazon-ebs: Get:11 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/multiverse amd64 Packages [151 kB]
    amazon-ebs: Get:12 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/multiverse Translation-en [108 kB]
    amazon-ebs: Get:13 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/universe Sources [106 kB]
    amazon-ebs: Get:14 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/restricted Sources [2064 B]
    amazon-ebs: Get:15 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/main Sources [219 kB]
    amazon-ebs: Get:16 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/multiverse Sources [3824 B]
    amazon-ebs: Get:17 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [454 kB]
    amazon-ebs: Get:18 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/main Translation-en [170 kB]
    amazon-ebs: Get:19 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [592 kB]
    amazon-ebs: Get:20 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/universe Translation-en [165 kB]
    amazon-ebs: Get:21 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [6372 B]
    amazon-ebs: Get:22 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/multiverse Translation-en [3356 B]
    amazon-ebs: Get:23 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-backports/universe Sources [2068 B]
    amazon-ebs: Get:24 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-backports/universe amd64 Packages [3468 B]
    amazon-ebs: Get:25 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-backports/universe Translation-en [1604 B]
    amazon-ebs: Get:26 http://security.ubuntu.com/ubuntu bionic-security/universe Sources [26.7 kB]
    amazon-ebs: Get:27 http://security.ubuntu.com/ubuntu bionic-security/multiverse Sources [1336 B]
    amazon-ebs: Get:28 http://security.ubuntu.com/ubuntu bionic-security/main Sources [64.1 kB]
    amazon-ebs: Get:29 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [218 kB]
    amazon-ebs: Get:30 http://security.ubuntu.com/ubuntu bionic-security/main Translation-en [84.0 kB]
    amazon-ebs: Get:31 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [106 kB]
    amazon-ebs: Get:32 http://security.ubuntu.com/ubuntu bionic-security/universe Translation-en [60.3 kB]
    amazon-ebs: Get:33 http://security.ubuntu.com/ubuntu bionic-security/multiverse amd64 Packages [1440 B]
    amazon-ebs: Get:34 http://security.ubuntu.com/ubuntu bionic-security/multiverse Translation-en [996 B]
    amazon-ebs: Fetched 26.4 MB in 4s (6162 kB/s)
    amazon-ebs: Reading package lists...
    amazon-ebs: Reading package lists...
    amazon-ebs: Building dependency tree...
    amazon-ebs: Reading state information...
    amazon-ebs: The following additional packages will be installed:
    amazon-ebs:   fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
    amazon-ebs:   libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip
    amazon-ebs:   libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter
    amazon-ebs:   libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx-common
    amazon-ebs:   nginx-core
    amazon-ebs: Suggested packages:
    amazon-ebs:   libgd-tools fcgiwrap nginx-doc ssl-cert
    amazon-ebs: The following NEW packages will be installed:
    amazon-ebs:   fontconfig-config fonts-dejavu-core libfontconfig1 libgd3 libjbig0
    amazon-ebs:   libjpeg-turbo8 libjpeg8 libnginx-mod-http-geoip
    amazon-ebs:   libnginx-mod-http-image-filter libnginx-mod-http-xslt-filter
    amazon-ebs:   libnginx-mod-mail libnginx-mod-stream libtiff5 libwebp6 libxpm4 nginx
    amazon-ebs:   nginx-common nginx-core
    amazon-ebs: 0 upgraded, 18 newly installed, 0 to remove and 11 not upgraded.
    amazon-ebs: Need to get 2460 kB of archives.
    amazon-ebs: After this operation, 8194 kB of additional disk space will be used.
    amazon-ebs: Get:1 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libjpeg-turbo8 amd64 1.5.2-0ubuntu5.18.04.1 [110 kB]
    amazon-ebs: Get:2 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/main amd64 fonts-dejavu-core all 2.37-1 [1041 kB]
    amazon-ebs: Get:3 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/main amd64 fontconfig-config all 2.12.6-0ubuntu2 [55.8 kB]
    amazon-ebs: Get:4 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/main amd64 libfontconfig1 amd64 2.12.6-0ubuntu2 [137 kB]
    amazon-ebs: Get:5 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/main amd64 libjpeg8 amd64 8c-2ubuntu8 [2194 B]
    amazon-ebs: Get:6 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/main amd64 libjbig0 amd64 2.1-3.1build1 [26.7 kB]
    amazon-ebs: Get:7 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/main amd64 libtiff5 amd64 4.0.9-5 [152 kB]
    amazon-ebs: Get:8 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/main amd64 libwebp6 amd64 0.6.1-2 [185 kB]
    amazon-ebs: Get:9 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic/main amd64 libxpm4 amd64 1:3.5.12-1 [34.0 kB]
    amazon-ebs: Get:10 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libgd3 amd64 2.2.5-4ubuntu0.2 [119 kB]
    amazon-ebs: Get:11 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/main amd64 nginx-common all 1.14.0-0ubuntu1.2 [37.4 kB]
    amazon-ebs: Get:12 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libnginx-mod-http-geoip amd64 1.14.0-0ubuntu1.2 [11.2 kB]
    amazon-ebs: Get:13 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libnginx-mod-http-image-filter amd64 1.14.0-0ubuntu1.2 [14.5 kB]
    amazon-ebs: Get:14 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libnginx-mod-http-xslt-filter amd64 1.14.0-0ubuntu1.2 [13.0 kB]
    amazon-ebs: Get:15 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libnginx-mod-mail amd64 1.14.0-0ubuntu1.2 [41.8 kB]
    amazon-ebs: Get:16 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libnginx-mod-stream amd64 1.14.0-0ubuntu1.2 [63.6 kB]
    amazon-ebs: Get:17 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/main amd64 nginx-core amd64 1.14.0-0ubuntu1.2 [413 kB]
    amazon-ebs: Get:18 http://ap-northeast-1.ec2.archive.ubuntu.com/ubuntu bionic-updates/main amd64 nginx all 1.14.0-0ubuntu1.2 [3596 B]
    amazon-ebs: debconf: unable to initialize frontend: Dialog
    amazon-ebs: debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
    amazon-ebs: debconf: falling back to frontend: Readline
    amazon-ebs: debconf: unable to initialize frontend: Readline
    amazon-ebs: debconf: (This frontend requires a controlling tty.)
    amazon-ebs: debconf: falling back to frontend: Teletype
    amazon-ebs: dpkg-preconfigure: unable to re-open stdin:
    amazon-ebs: Fetched 2460 kB in 0s (16.0 MB/s)
    amazon-ebs: Selecting previously unselected package libjpeg-turbo8:amd64.
    amazon-ebs: (Reading database ... 56545 files and directories currently installed.)
    amazon-ebs: Preparing to unpack .../00-libjpeg-turbo8_1.5.2-0ubuntu5.18.04.1_amd64.deb ...
    amazon-ebs: Unpacking libjpeg-turbo8:amd64 (1.5.2-0ubuntu5.18.04.1) ...
    amazon-ebs: Selecting previously unselected package fonts-dejavu-core.
    amazon-ebs: Preparing to unpack .../01-fonts-dejavu-core_2.37-1_all.deb ...
    amazon-ebs: Unpacking fonts-dejavu-core (2.37-1) ...
    amazon-ebs: Selecting previously unselected package fontconfig-config.
    amazon-ebs: Preparing to unpack .../02-fontconfig-config_2.12.6-0ubuntu2_all.deb ...
    amazon-ebs: Unpacking fontconfig-config (2.12.6-0ubuntu2) ...
    amazon-ebs: Selecting previously unselected package libfontconfig1:amd64.
    amazon-ebs: Preparing to unpack .../03-libfontconfig1_2.12.6-0ubuntu2_amd64.deb ...
    amazon-ebs: Unpacking libfontconfig1:amd64 (2.12.6-0ubuntu2) ...
    amazon-ebs: Selecting previously unselected package libjpeg8:amd64.
    amazon-ebs: Preparing to unpack .../04-libjpeg8_8c-2ubuntu8_amd64.deb ...
    amazon-ebs: Unpacking libjpeg8:amd64 (8c-2ubuntu8) ...
    amazon-ebs: Selecting previously unselected package libjbig0:amd64.
    amazon-ebs: Preparing to unpack .../05-libjbig0_2.1-3.1build1_amd64.deb ...
    amazon-ebs: Unpacking libjbig0:amd64 (2.1-3.1build1) ...
    amazon-ebs: Selecting previously unselected package libtiff5:amd64.
    amazon-ebs: Preparing to unpack .../06-libtiff5_4.0.9-5_amd64.deb ...
    amazon-ebs: Unpacking libtiff5:amd64 (4.0.9-5) ...
    amazon-ebs: Selecting previously unselected package libwebp6:amd64.
    amazon-ebs: Preparing to unpack .../07-libwebp6_0.6.1-2_amd64.deb ...
    amazon-ebs: Unpacking libwebp6:amd64 (0.6.1-2) ...
    amazon-ebs: Selecting previously unselected package libxpm4:amd64.
    amazon-ebs: Preparing to unpack .../08-libxpm4_1%3a3.5.12-1_amd64.deb ...
    amazon-ebs: Unpacking libxpm4:amd64 (1:3.5.12-1) ...
    amazon-ebs: Selecting previously unselected package libgd3:amd64.
    amazon-ebs: Preparing to unpack .../09-libgd3_2.2.5-4ubuntu0.2_amd64.deb ...
    amazon-ebs: Unpacking libgd3:amd64 (2.2.5-4ubuntu0.2) ...
    amazon-ebs: Selecting previously unselected package nginx-common.
    amazon-ebs: Preparing to unpack .../10-nginx-common_1.14.0-0ubuntu1.2_all.deb ...
    amazon-ebs: Unpacking nginx-common (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Selecting previously unselected package libnginx-mod-http-geoip.
    amazon-ebs: Preparing to unpack .../11-libnginx-mod-http-geoip_1.14.0-0ubuntu1.2_amd64.deb ...
    amazon-ebs: Unpacking libnginx-mod-http-geoip (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Selecting previously unselected package libnginx-mod-http-image-filter.
    amazon-ebs: Preparing to unpack .../12-libnginx-mod-http-image-filter_1.14.0-0ubuntu1.2_amd64.deb ...
    amazon-ebs: Unpacking libnginx-mod-http-image-filter (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Selecting previously unselected package libnginx-mod-http-xslt-filter.
    amazon-ebs: Preparing to unpack .../13-libnginx-mod-http-xslt-filter_1.14.0-0ubuntu1.2_amd64.deb ...
    amazon-ebs: Unpacking libnginx-mod-http-xslt-filter (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Selecting previously unselected package libnginx-mod-mail.
    amazon-ebs: Preparing to unpack .../14-libnginx-mod-mail_1.14.0-0ubuntu1.2_amd64.deb ...
    amazon-ebs: Unpacking libnginx-mod-mail (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Selecting previously unselected package libnginx-mod-stream.
    amazon-ebs: Preparing to unpack .../15-libnginx-mod-stream_1.14.0-0ubuntu1.2_amd64.deb ...
    amazon-ebs: Unpacking libnginx-mod-stream (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Selecting previously unselected package nginx-core.
    amazon-ebs: Preparing to unpack .../16-nginx-core_1.14.0-0ubuntu1.2_amd64.deb ...
    amazon-ebs: Unpacking nginx-core (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Selecting previously unselected package nginx.
    amazon-ebs: Preparing to unpack .../17-nginx_1.14.0-0ubuntu1.2_all.deb ...
    amazon-ebs: Unpacking nginx (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Processing triggers for ufw (0.35-5) ...
    amazon-ebs: Processing triggers for ureadahead (0.100.0-20) ...
    amazon-ebs: Setting up libjbig0:amd64 (2.1-3.1build1) ...
    amazon-ebs: Setting up fonts-dejavu-core (2.37-1) ...
    amazon-ebs: Setting up nginx-common (1.14.0-0ubuntu1.2) ...
    amazon-ebs: debconf: unable to initialize frontend: Dialog
    amazon-ebs: debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
    amazon-ebs: debconf: falling back to frontend: Readline
    amazon-ebs: Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /lib/systemd/system/nginx.service.
    amazon-ebs: Setting up libjpeg-turbo8:amd64 (1.5.2-0ubuntu5.18.04.1) ...
    amazon-ebs: Processing triggers for libc-bin (2.27-3ubuntu1) ...
    amazon-ebs: Processing triggers for systemd (237-3ubuntu10.9) ...
    amazon-ebs: Setting up libnginx-mod-mail (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Setting up libxpm4:amd64 (1:3.5.12-1) ...
    amazon-ebs: Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
    amazon-ebs: Setting up libnginx-mod-http-xslt-filter (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Setting up libnginx-mod-http-geoip (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Setting up libwebp6:amd64 (0.6.1-2) ...
    amazon-ebs: Setting up libjpeg8:amd64 (8c-2ubuntu8) ...
    amazon-ebs: Setting up fontconfig-config (2.12.6-0ubuntu2) ...
    amazon-ebs: Setting up libnginx-mod-stream (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Setting up libtiff5:amd64 (4.0.9-5) ...
    amazon-ebs: Setting up libfontconfig1:amd64 (2.12.6-0ubuntu2) ...
    amazon-ebs: Setting up libgd3:amd64 (2.2.5-4ubuntu0.2) ...
    amazon-ebs: Setting up libnginx-mod-http-image-filter (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Setting up nginx-core (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Setting up nginx (1.14.0-0ubuntu1.2) ...
    amazon-ebs: Processing triggers for ureadahead (0.100.0-20) ...
    amazon-ebs: Processing triggers for ufw (0.35-5) ...
    amazon-ebs: Processing triggers for libc-bin (2.27-3ubuntu1) ...
==> amazon-ebs: Stopping the source instance...
    amazon-ebs: Stopping instance, attempt 1
==> amazon-ebs: Waiting for the instance to stop...
==> amazon-ebs: Creating unencrypted AMI packer-example 1544282532 from instance i-0c0c59321671f31a9
    amazon-ebs: AMI: ami-036f026bf22d796b9
==> 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-036f026bf22d796b9

お、なんかできたっぽいですね。

nginx 接続確認

今作ったAMIを使用して、インスタンスを適当に上げてみましょう。
HTTP(80)が開いていればとりあえずアクセスできます。
で、やってみると・・・

Welcome to nginx.png

いけてますね。

まとめ

工夫すれば、オリジナルのイメージが簡単に作れることがわかりました。
結構さくさく作れますので、遊んでみてください。
くれぐれも課金には注意です(笑)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?