Packer で AMI イメージを5分で作成する
Packer は Hashicorp の出しているイメージ作成ツールです。
aws で使われる AMI などのイメージを作るためのツールとなります。
今日の環境は CentOS 7.6 ですが、Ubuntu でも特に問題ないと思います。
0:00
Packer をダウンロードして、さくっと解凍しましょう。
> cd /tmp
> wget https://releases.hashicorp.com/packer/1.3.3/packer_1.3.3_linux_amd64.zip
--2018-12-07 20:57:01-- https://releases.hashicorp.com/packer/1.3.3/packer_1.3.3_linux_amd64.zip
Resolving releases.hashicorp.com (releases.hashicorp.com)... 2a04:4e42:11::439, 151.101.229.183
Connecting to releases.hashicorp.com (releases.hashicorp.com)|2a04:4e42:11::439|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 27658559 (26M) [application/zip]
Saving to: ‘packer_1.3.3_linux_amd64.zip’
100%[============================================================================================================================>] 27,658,559 56.4MB/s in 0.5s
2018-12-07 20:57:01 (56.4 MB/s) - ‘packer_1.3.3_linux_amd64.zip’ saved [27658559/27658559]
> unzip packer_1.3.3_linux_amd64.zip
Archive: packer_1.3.3_linux_amd64.zip
inflating: packer
0:30
このページにあったサンプルの region
だけ ap-northeast-1
にして 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-xenial-16.04-amd64-server-*",
"root-device-type": "ebs"
},
"owners": ["099720109477"],
"most_recent": true
},
"instance_type": "t2.micro",
"ssh_username": "ubuntu",
"ami_name": "packer-example {{timestamp}}"
}]
}
1:00
ビルドします。
access key
と secret key
は事前に用意しておいてください。
ec2
の権限ついてれば大丈夫なはずです。
> ./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 1544185004
amazon-ebs: Found Image ID: ami-032f516e93380b8e6
==> amazon-ebs: Creating temporary keypair: packer_5c0a64ac-98ec-f0a6-66d2-00352100b298
==> amazon-ebs: Creating temporary security group for this instance: packer_5c0a64ad-c80c-d1cf-3b58-9d7fec2fbc08
==> 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-03550cbcf5c65ec37
==> amazon-ebs: Waiting for instance (i-03550cbcf5c65ec37) to become ready...
==> amazon-ebs: Using ssh communicator to connect: 54.168.48.122
==> amazon-ebs: Waiting for SSH to become available...
==> amazon-ebs: Connected to SSH!
==> 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 1544185004 from instance i-03550cbcf5c65ec37
amazon-ebs: AMI: ami-085a4ca7ef8fffeca
==> 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-085a4ca7ef8fffeca
サーバ立てて落としてます
AMI作成してます
結構、時間かかりましたね。
サーバ立てて、そのスナップショットからAMIを作っているので、その分の時間ですね。
4:00
なんとか、5分以内にAMIイメージできましたね。
DevOps の流れとして、イメージの作成も自動化したい場合などは、こういうツールを使って作る感じになるようですね。
5:00
おわり