HashiCorp's Packer is a useful tool to build some VM images for multiple platforms and softwares. Using builders like
virtualbox-iso allows building images from scratch; installing systems into empty disk. It supports AWS EC2 AMI, but it doesn't allow building from scratch straightly.
So I've discovered the following 2 ways to build AMI from scratch using packer:
Plan A: use customized builder amazon-scratch
First I developed https://github.com/sorah/packer-builder-amazon-scratch . This allows attaching additional disk on source instance, then creates AMI from additional disk. This works well but this can't be used on Atlas, because it can't install any plugins.
Plan B: boot from tmpfs using user_data
amazon-ebs builder supports user_data for source instance. Ubuntu images have cloud-init that do some initialization process using metadata including user_data.
This plan uses amazon-ebs builder with source EC2 instance booted from
tmpfs. The following cloud-config bootcmd injects bash script before
/sbin/init run, then reboot instance itself. The script copies all of rootfs into tmpfs, then unmount root EBS, finally kicks /sbin/init on tmpfs to continue boot process. So provisioners can format
/dev/xvda and install systems into it.
Also this scripts change sshd listening port from 22 to 122 -- to make sure packer to connect instance after reboot. You have to specify
ssh_port to 22 on your packer configuration.
I'm using this trick on https://github.com/sorah/gentoo-build -- this works well for packer, out-of-the-box.