Closed5

Packerを試す

harrythecodeharrythecode

What to do?

AWS, Azureなどで使えるOSイメージをVagrantを用いてIaC化できるツールの使い方を学ぶ。

https://www.packer.io/

Why to do?

AWS Launch Templateを使ってセットアップする場合があるのだが、Custom AMIを作って展開した方が時間の短縮やビルドの安定性に繋がるから。

How to do it?

AWS/Dockerの2パターンがあるのでAWSを選択

harrythecodeharrythecode

Install Packer

$ brew tap hashicorp/tap
$ brew install hashicorp/tap/packer
$ packer -v
1.7.5

Build an image

$ mkdir packer_tutorial
$ cd packer_tutorial
$ vim aws-ubuntu.pkr.hcl
---
packer {
  required_plugins {
    amazon = {
      version = ">= 1.0.1"
      source  = "github.com/hashicorp/amazon"
    }
  }
}

source "amazon-ebs" "ubuntu" {
  ami_name      = "learn-packer-linux-aws"
  instance_type = "t2.micro"
  region        = "us-west-2"
  source_ami_filter {
    filters = {
      name                = "ubuntu/images/*ubuntu-xenial-16.04-amd64-server-*"
      root-device-type    = "ebs"
      virtualization-type = "hvm"
    }
    most_recent = true
    owners      = ["099720109477"]
  }
  ssh_username = "ubuntu"
}

build {
  name    = "learn-packer"
  sources = [
    "source.amazon-ebs.ubuntu"
  ]
}
---

Initialize Packer configuration

  • AWS Profileの設定は各自の好みがあるので省略。
$ packer init .
Installed plugin github.com/hashicorp/amazon v1.0.1 in "/Users/harry/.config/packer/plugins/github.com/hashicorp/amazon/packer-plugin-amazon_v1.0.1_x5.0_darwin_amd64"
harrythecodeharrythecode

(Optional) Format and validate your Packer template

以下はオプション。Formatは、Indexの並びなど見た目が綺麗になり、Validateは、中身のチェックをする。

$ packer fmt .
aws-ubuntu.pkr.hcl
$ packer validate .
Error: Missing newline after argument

  on aws-ubuntu.pkr.hcl line 27, in build:
  27:   name = "learn-packer"s

An argument definition must end with a newline.

Build Packer image

やってることは次の通り。

  1. EC2を立ち上げる
  2. 立ち上がったEC2を元にAMIイメージを作成
  3. 生成AMIを使いEC2が立ち上がるかを検証
$ packer build aws-ubuntu.pkr.hcl
learn-packer.amazon-ebs.ubuntu: output will be in this color.

==> learn-packer.amazon-ebs.ubuntu: Prevalidating any provided VPC information
==> learn-packer.amazon-ebs.ubuntu: Prevalidating AMI Name: learn-packer-linux-aws
    learn-packer.amazon-ebs.ubuntu: Found Image ID: ami-079e7a3f57cc8e0d0
==> learn-packer.amazon-ebs.ubuntu: Creating temporary keypair: packer_614c3e5b-0e44-dfcb-89c6-ab249363307c
==> learn-packer.amazon-ebs.ubuntu: Creating temporary security group for this instance: packer_614c3e60-013b-f055-e6be-58ec61349a63
==> learn-packer.amazon-ebs.ubuntu: Authorizing access to port 22 from [0.0.0.0/0] in the temporary security groups...
==> learn-packer.amazon-ebs.ubuntu: Launching a source AWS instance...
==> learn-packer.amazon-ebs.ubuntu: Adding tags to source instance
    learn-packer.amazon-ebs.ubuntu: Adding tag: "Name": "Packer Builder"
    learn-packer.amazon-ebs.ubuntu: Instance ID: i-0f37cca1948b166fb
==> learn-packer.amazon-ebs.ubuntu: Waiting for instance (i-0f37cca1948b166fb) to become ready...
==> learn-packer.amazon-ebs.ubuntu: Using SSH communicator to connect: 34.209.126.189
==> learn-packer.amazon-ebs.ubuntu: Waiting for SSH to become available...
==> learn-packer.amazon-ebs.ubuntu: Connected to SSH!
==> learn-packer.amazon-ebs.ubuntu: Stopping the source instance...
    learn-packer.amazon-ebs.ubuntu: Stopping instance
==> learn-packer.amazon-ebs.ubuntu: Waiting for the instance to stop...
==> learn-packer.amazon-ebs.ubuntu: Creating AMI learn-packer-linux-aws from instance i-0f37cca1948b166fb
    learn-packer.amazon-ebs.ubuntu: AMI: ami-06fb2a0c6b0e73608
==> learn-packer.amazon-ebs.ubuntu: Waiting for AMI to become ready...
==> learn-packer.amazon-ebs.ubuntu: Skipping Enable AMI deprecation...
==> learn-packer.amazon-ebs.ubuntu: Terminating the source AWS instance...
==> learn-packer.amazon-ebs.ubuntu: Cleaning up any extra volumes...
==> learn-packer.amazon-ebs.ubuntu: No volumes to clean up, skipping
==> learn-packer.amazon-ebs.ubuntu: Deleting temporary security group...
==> learn-packer.amazon-ebs.ubuntu: Deleting temporary keypair...
Build 'learn-packer.amazon-ebs.ubuntu' finished after 3 minutes 52 seconds.

==> Wait completed after 3 minutes 52 seconds

==> Builds finished. The artifacts of successful builds are:
--> learn-packer.amazon-ebs.ubuntu: AMIs were created:
us-west-2: ami-06fb2a0c6b0e73608

非常にStraight-Forwardなやり方でつまづくことがなさそうです。

harrythecodeharrythecode

Provision

先ほどのBuildにProvisionerを追加します。

build {
  name = "learn-packer"
  sources = [
    "source.amazon-ebs.ubuntu"
  ]

  provisioner "shell" {
    environment_vars = [
      "FOO=hello world",
    ]
    inline = [
      "echo Installing Redis",
      "sleep 30",
      "sudo apt-get update",
      "sudo apt-get install -y redis-server",
      "echo \"FOO is $FOO\" > example.txt",
    ]
  }
}

Tutorialでは新しいイメージを作ってますが、今回は前回のを削除して作り直すオプション -force を使います。

$ packer build -h
Options:
  -force   Force a build to continue if artifacts exist, deletes existing artifacts.

$ packer build -force .
learn-packer.amazon-ebs.ubuntu: output will be in this color.

==> learn-packer.amazon-ebs.ubuntu: Force Deregister flag found, skipping prevalidating AMI Name
    learn-packer.amazon-ebs.ubuntu: Found Image ID: ami-079e7a3f57cc8e0d0
==> learn-packer.amazon-ebs.ubuntu: Creating temporary keypair: packer_614c4101-147e-0b81-0d2c-e45dc750b81f
==> learn-packer.amazon-ebs.ubuntu: Creating temporary security group for this instance: packer_614c4105-3263-3b9c-c6ba-df65f1be83dd
==> learn-packer.amazon-ebs.ubuntu: Authorizing access to port 22 from [0.0.0.0/0] in the temporary security groups...
==> learn-packer.amazon-ebs.ubuntu: Launching a source AWS instance...
==> learn-packer.amazon-ebs.ubuntu: Adding tags to source instance
    learn-packer.amazon-ebs.ubuntu: Adding tag: "Name": "Packer Builder"
    learn-packer.amazon-ebs.ubuntu: Instance ID: i-00ed741b7513990fb
==> learn-packer.amazon-ebs.ubuntu: Waiting for instance (i-00ed741b7513990fb) to become ready...
==> learn-packer.amazon-ebs.ubuntu: Using SSH communicator to connect: 54.212.2.94
==> learn-packer.amazon-ebs.ubuntu: Waiting for SSH to become available...
==> learn-packer.amazon-ebs.ubuntu: Connected to SSH!
==> learn-packer.amazon-ebs.ubuntu: Provisioning with shell script: /var/folders/29/5tx9cmnd53n9_6sklwhcp0hr0000gp/T/packer-shell206006348
    learn-packer.amazon-ebs.ubuntu: Installing Redis
    learn-packer.amazon-ebs.ubuntu: Hit:1 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial InRelease
    learn-packer.amazon-ebs.ubuntu: Get:2 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial-updates InRelease [109 kB]
    learn-packer.amazon-ebs.ubuntu: Get:3 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial-backports InRelease [107 kB]
    learn-packer.amazon-ebs.ubuntu: Get:4 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial/universe amd64 Packages [7,532 kB]
    learn-packer.amazon-ebs.ubuntu: Get:5 http://security.ubuntu.com/ubuntu xenial-security InRelease [109 kB]
    learn-packer.amazon-ebs.ubuntu: Get:6 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial/universe Translation-en [4,354 kB]
    learn-packer.amazon-ebs.ubuntu: Get:7 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial/multiverse amd64 Packages [144 kB]
    learn-packer.amazon-ebs.ubuntu: Get:8 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial/multiverse Translation-en [106 kB]
    learn-packer.amazon-ebs.ubuntu: Get:9 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [2,049 kB]
    learn-packer.amazon-ebs.ubuntu: Get:10 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial-updates/main Translation-en [482 kB]
    learn-packer.amazon-ebs.ubuntu: Get:11 https://esm.ubuntu.com/infra/ubuntu xenial-infra-security InRelease [7,509 B]
    learn-packer.amazon-ebs.ubuntu: Get:12 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [1,219 kB]
    learn-packer.amazon-ebs.ubuntu: Get:13 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial-updates/universe Translation-en [358 kB]
    learn-packer.amazon-ebs.ubuntu: Get:14 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 Packages [22.6 kB]
    learn-packer.amazon-ebs.ubuntu: Get:15 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial-updates/multiverse Translation-en [8,476 B]
    learn-packer.amazon-ebs.ubuntu: Get:16 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial-backports/main amd64 Packages [9,812 B]
    learn-packer.amazon-ebs.ubuntu: Get:17 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial-backports/main Translation-en [4,456 B]
    learn-packer.amazon-ebs.ubuntu: Get:18 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial-backports/universe amd64 Packages [11.3 kB]
    learn-packer.amazon-ebs.ubuntu: Get:19 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial-backports/universe Translation-en [4,476 B]
    learn-packer.amazon-ebs.ubuntu: Get:20 https://esm.ubuntu.com/infra/ubuntu xenial-infra-updates InRelease [7,475 B]
    learn-packer.amazon-ebs.ubuntu: Get:21 http://security.ubuntu.com/ubuntu xenial-security/main amd64 Packages [1,648 kB]
    learn-packer.amazon-ebs.ubuntu: Get:22 https://esm.ubuntu.com/infra/ubuntu xenial-infra-security/main amd64 Packages [182 kB]
    learn-packer.amazon-ebs.ubuntu: Get:23 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [785 kB]
    learn-packer.amazon-ebs.ubuntu: Get:24 http://security.ubuntu.com/ubuntu xenial-security/universe Translation-en [225 kB]
    learn-packer.amazon-ebs.ubuntu: Get:25 http://security.ubuntu.com/ubuntu xenial-security/multiverse amd64 Packages [7,864 B]
    learn-packer.amazon-ebs.ubuntu: Get:26 http://security.ubuntu.com/ubuntu xenial-security/multiverse Translation-en [2,672 B]
    learn-packer.amazon-ebs.ubuntu: Fetched 19.5 MB in 3s (4,896 kB/s)
    learn-packer.amazon-ebs.ubuntu: Reading package lists...
    learn-packer.amazon-ebs.ubuntu: Reading package lists...
    learn-packer.amazon-ebs.ubuntu: Building dependency tree...
    learn-packer.amazon-ebs.ubuntu: Reading state information...
    learn-packer.amazon-ebs.ubuntu: The following additional packages will be installed:
    learn-packer.amazon-ebs.ubuntu:   libjemalloc1 redis-tools
    learn-packer.amazon-ebs.ubuntu: Suggested packages:
    learn-packer.amazon-ebs.ubuntu:   ruby-redis
    learn-packer.amazon-ebs.ubuntu: The following NEW packages will be installed:
    learn-packer.amazon-ebs.ubuntu:   libjemalloc1 redis-server redis-tools
    learn-packer.amazon-ebs.ubuntu: 0 upgraded, 3 newly installed, 0 to remove and 6 not upgraded.
    learn-packer.amazon-ebs.ubuntu: Need to get 519 kB of archives.
    learn-packer.amazon-ebs.ubuntu: After this operation, 1,507 kB of additional disk space will be used.
    learn-packer.amazon-ebs.ubuntu: Get:1 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial/universe amd64 libjemalloc1 amd64 3.6.0-9ubuntu1 [78.9 kB]
    learn-packer.amazon-ebs.ubuntu: Get:2 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 redis-tools amd64 2:3.0.6-1ubuntu0.4 [95.5 kB]
    learn-packer.amazon-ebs.ubuntu: Get:3 http://us-west-2.ec2.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 redis-server amd64 2:3.0.6-1ubuntu0.4 [344 kB]
==> learn-packer.amazon-ebs.ubuntu: debconf: unable to initialize frontend: Dialog
==> learn-packer.amazon-ebs.ubuntu: debconf: (Dialog frontend will not work on a dumb terminal, an emacs shell buffer, or without a controlling terminal.)
==> learn-packer.amazon-ebs.ubuntu: debconf: falling back to frontend: Readline
==> learn-packer.amazon-ebs.ubuntu: debconf: unable to initialize frontend: Readline
==> learn-packer.amazon-ebs.ubuntu: debconf: (This frontend requires a controlling tty.)
==> learn-packer.amazon-ebs.ubuntu: debconf: falling back to frontend: Teletype
==> learn-packer.amazon-ebs.ubuntu: dpkg-preconfigure: unable to re-open stdin:
    learn-packer.amazon-ebs.ubuntu: Fetched 519 kB in 0s (13.4 MB/s)
    learn-packer.amazon-ebs.ubuntu: Selecting previously unselected package libjemalloc1.
    learn-packer.amazon-ebs.ubuntu: (Reading database ... 51558 files and directories currently installed.)
    learn-packer.amazon-ebs.ubuntu: Preparing to unpack .../libjemalloc1_3.6.0-9ubuntu1_amd64.deb ...
    learn-packer.amazon-ebs.ubuntu: Unpacking libjemalloc1 (3.6.0-9ubuntu1) ...
    learn-packer.amazon-ebs.ubuntu: Selecting previously unselected package redis-tools.
    learn-packer.amazon-ebs.ubuntu: Preparing to unpack .../redis-tools_2%3a3.0.6-1ubuntu0.4_amd64.deb ...
    learn-packer.amazon-ebs.ubuntu: Unpacking redis-tools (2:3.0.6-1ubuntu0.4) ...
    learn-packer.amazon-ebs.ubuntu: Selecting previously unselected package redis-server.
    learn-packer.amazon-ebs.ubuntu: Preparing to unpack .../redis-server_2%3a3.0.6-1ubuntu0.4_amd64.deb ...
    learn-packer.amazon-ebs.ubuntu: Unpacking redis-server (2:3.0.6-1ubuntu0.4) ...
    learn-packer.amazon-ebs.ubuntu: Processing triggers for libc-bin (2.23-0ubuntu11.3) ...
    learn-packer.amazon-ebs.ubuntu: Processing triggers for man-db (2.7.5-1) ...
    learn-packer.amazon-ebs.ubuntu: Processing triggers for ureadahead (0.100.0-19.1) ...
    learn-packer.amazon-ebs.ubuntu: Processing triggers for systemd (229-4ubuntu21.31) ...
    learn-packer.amazon-ebs.ubuntu: Setting up libjemalloc1 (3.6.0-9ubuntu1) ...
    learn-packer.amazon-ebs.ubuntu: Setting up redis-tools (2:3.0.6-1ubuntu0.4) ...
    learn-packer.amazon-ebs.ubuntu: Setting up redis-server (2:3.0.6-1ubuntu0.4) ...
    learn-packer.amazon-ebs.ubuntu: Processing triggers for libc-bin (2.23-0ubuntu11.3) ...
    learn-packer.amazon-ebs.ubuntu: Processing triggers for ureadahead (0.100.0-19.1) ...
    learn-packer.amazon-ebs.ubuntu: Processing triggers for systemd (229-4ubuntu21.31) ...
==> learn-packer.amazon-ebs.ubuntu: Stopping the source instance...
    learn-packer.amazon-ebs.ubuntu: Stopping instance
==> learn-packer.amazon-ebs.ubuntu: Waiting for the instance to stop...
==> learn-packer.amazon-ebs.ubuntu: Deregistered AMI learn-packer-linux-aws, id: ami-06fb2a0c6b0e73608
==> learn-packer.amazon-ebs.ubuntu: Creating AMI learn-packer-linux-aws from instance i-00ed741b7513990fb
    learn-packer.amazon-ebs.ubuntu: AMI: ami-0e15fa2080db6744f
==> learn-packer.amazon-ebs.ubuntu: Waiting for AMI to become ready...
==> learn-packer.amazon-ebs.ubuntu: Skipping Enable AMI deprecation...
==> learn-packer.amazon-ebs.ubuntu: Terminating the source AWS instance...
==> learn-packer.amazon-ebs.ubuntu: Cleaning up any extra volumes...
==> learn-packer.amazon-ebs.ubuntu: No volumes to clean up, skipping
==> learn-packer.amazon-ebs.ubuntu: Deleting temporary security group...
==> learn-packer.amazon-ebs.ubuntu: Deleting temporary keypair...
Build 'learn-packer.amazon-ebs.ubuntu' finished after 6 minutes 2 seconds.

==> Wait completed after 6 minutes 2 seconds

==> Builds finished. The artifacts of successful builds are:
--> learn-packer.amazon-ebs.ubuntu: AMIs were created:
us-west-2: ami-0e15fa2080db6744f
harrythecodeharrythecode

知っておくと良いこと

variable "ami_prefix" {
  type    = string
  default = "learn-packer-linux-aws-redis"
}

locals {
  timestamp = regex_replace(timestamp(), "[- TZ:]", "")
}

source "amazon-ebs" "ubuntu" {
-  ami_name      = "learn-packer-linux-aws-redis-msg"
+  ami_name      = "${var.ami_prefix}-${local.timestamp}"
   ## ...
}
このスクラップは2021/09/28にクローズされました