Packerを使ってAmazon EC2のAMIを作る(1)
こんにちは、@oko_changです。
すでに色々な方がブログで記事にしたりしていますが、僕もPackerを使った記録を残しておきます。
Packerは公式サイトにもある通り、ひとつの設定ファイルで様々なプラットフォームのマシンイメージを作成するようなツールです。
私の場合はAWSを使う事が多いので、自分専用のAMIを簡単に作るツールという感じで使えそうですね。
ダウンロードページ
ダウンロードは公式サイトのダウンロードページからお使いのプラットフォームに対応したものを選べばOKです。
今回は僕のMBAにセットアップするのでMac OS Xをダウンロードしましたが、色々な環境で使うことが出来るのも良いですね。
※現時点でのバージョンは0.3.7
セットアップ
セットアップは簡単で、ダウンロードしたファイルを解凍してPATHを通してあげればOKです。
$ mv ~/Downloads/0.3.7_darwin_amd64.zip ~/src/ $ mkdir ~/bin/packer $ unzip ~/src/0.3.7_darwin_amd64.zip -d ~/bin/packer/ $ vi .bash_profile export PATH=$PATH:$HOME/bin/packer
設定ファイルの作成
さっそく設定ファイルを作ります。
ルートボリュームがEBSのAMIを作成しましたが、最低限以下のような設定が必要なようです。
今回作成した設定ファイルが以下ですが、上記の設定に加えてエフェメラルストレージの追加とタグの設定をしています。
また、標準設定ではSSHがタイムアウトしてしまったので、ssh_timeoutの設定も追加しています。
※access_keyとsecret_keyはそれぞれ自分のものに置き換えて下さい。
$ mkdir ~/.packer
$ vi ~/.packer/my_first_template.json
{
"builders": [{
"type": "amazon-ebs",
"access_key": "XXXXXXXXXXXXXXX",
"secret_key": "YYYYYYYYYYYYYYYYYYYYYYYYYY",
"region": "ap-northeast-1",
"source_ami": "ami-39b23d38",
"instance_type": "m1.small",
"ssh_username": "ec2-user",
"ssh_timeout": "5m",
"ami_name": "okochang-packer-quick-start",
"ami_block_device_mappings": [
{
"device_name": "/dev/sdb",
"virtual_name": "ephemeral0"
}
],
"tags": {
"OS_Version": "Amazon Linux",
"Release": "Latest"
}
}]
}
実行
あとは実行すれば数分でAMI作成が完了します、楽ちんです。
$ packer build ~/.packer/my_first_template.json
amazon-ebs output will be in this color.
==> amazon-ebs: Creating temporary keypair: packer f46700972a114df72357763e9cbd2890
==> amazon-ebs: Creating temporary security group for this instance...
==> amazon-ebs: Authorizing SSH access on the temporary security group...
==> amazon-ebs: Launching a source AWS instance...
==> amazon-ebs: Waiting for instance (i-a6cf6ba3) to become ready...
==> amazon-ebs: Waiting for SSH to become available...
==> amazon-ebs: Connected to SSH!
==> amazon-ebs: Stopping the source instance...
==> amazon-ebs: Waiting for the instance to stop...
==> amazon-ebs: Creating the AMI: okochang-packer-quick-start
==> amazon-ebs: AMI: ami-c9ec70c8
==> amazon-ebs: Waiting for AMI to become ready...
==> amazon-ebs: Adding tags to AMI (ami-c9ec70c8)...
amazon-ebs: Adding tag: "OS_Version": "Amazon Linux"
amazon-ebs: Adding tag: "Release": "Latest"
==> amazon-ebs: Terminating the source AWS instance...
==> 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-c9ec70c8
確認
念のためawscliで作成されたAMI IDを調べましたが、しっかり出来あがっています。
$ aws ec2 describe-images --region ap-northeast-1 --image-ids ami-c3f26ec2 | jq '.'
{
"Images": [
{
"ImageType": "machine",
"Public": false,
"RootDeviceName": "/dev/sda1",
"OwnerId": "1234567890",
"KernelId": "aki-44992845",
"ImageLocation": "1234567890/okochang-packer-quick-start",
"Architecture": "x86_64",
"VirtualizationType": "paravirtual",
"Name": "okochang-packer-quick-start",
"Tags": [
{
"Key": "OS_Version",
"Value": "Amazon Linux"
},
{
"Key": "Release",
"Value": "Latest"
}
],
"Hypervisor": "xen",
"ImageId": "ami-c9ec70c8",
"RootDeviceType": "ebs",
"State": "available",
"BlockDeviceMappings": [
{
"Ebs": {
"VolumeType": "standard",
"VolumeSize": 8,
"SnapshotId": "snap-ebdc01ce",
"DeleteOnTermination": true
},
"DeviceName": "/dev/sda1"
},
{
"VirtualName": "ephemeral0",
"DeviceName": "/dev/sdb"
}
]
}
]
}
まとめ
思った以上に簡単だし、設定ファイルの再利用も出来そうなので作業時間の短縮になりそうです。