aws memo

AWS関連の備忘録 (※本ブログの内容は個人的見解であり、所属組織及び企業の意見を代弁するものではありません。1年以上古いエントリは疑ってかかってください)

VM ImportしたCentOS6.5のAMIを作る前の手順

VM Importで持ってきたCentOS5.6のインスタンスからそのままAMIを作成してもよいが、いくつかEC2ぽくない部分があるので、AMI作成前にこれらを施す。

  • key pairのpublickeyデプロイスクリプトを仕込む
  • rootパスワードを起動時にランダム生成・セットするスクリプト
  • udev で eth0のMACアドレスを保持しないようにする
  • sshdで rootのパスワードログイン禁止、DNS checks無効化
  • ホストキーペアを削除
  • historyを削除
  • ~/.ssh/ 以下を削除
  • cloud-init の仕組みを入れる
  • 別ユーザ(例: ec2-user)を作成し、rootログインを禁止する

参考 : http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/building-shared-amis.html

手順

rootパスワードのランダム化を /etc/rc.d/rc.localに追記

# set a random pass on first boot
if [ -f /root/firstrun ]; then
  dd if=/dev/urandom count=50|md5sum|passwd --stdin root
  passwd -l root
  rm /root/firstrun
fi

このスクリプトを動作させるためにフラグファイルを作っておく

touch /root/firstrun 

publickeyの埋め込み処理を、 /etc/rc.d/rc.localに追記

if [ ! -d /root/.ssh ]; then
  mkdir -m 0700 -p /root/.ssh
  restorecon /root/.ssh
fi
# Get the root ssh key setup
ReTry=0
while [ ! -f /root/.ssh/authorized_keys ] && [ $ReTry -lt 5 ]; do
  sleep 2
  curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key > /root/.ssh/authorized_keys
  ReTry=$[Retry+1]
done
chmod 600 /root/.ssh/authorized_keys && restorecon /root/.ssh/authorized_keys

udevでのethX MACアドレス 自動保存の無効化

rm /etc/udev/rules.d/70-persistent-net.rules
cp /lib/udev/rules.d/75-persistent-net-generator.rules ~
vi /lib/udev/rules.d/75-persistent-net-generator.rules
diff  /lib/udev/rules.d/75-persistent-net-generator.rules ~/75-persistent-net-generator.rules 
93c93
< #DRIVERS=="?*", IMPORT{program}="write_net_rules"
---
> DRIVERS=="?*", IMPORT{program}="write_net_rules" 
reboot 

上記手順を終えて、create imageする