環境
- AWS ClassicEC2
- python 2.6
※ 2.7もしくは3系を推奨
モジュールインストール
インストール pip
-
python 2.6系であれば
http://tdoc.info/blog/2014/01/15/pip.html
yum install python-pip
-
python 2.7系であれば
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" sudo python get-pip.py
インストール awscli
http://docs.aws.amazon.com/ja_jp/kinesis/latest/dev/kinesis-tutorial-cli-installation.html
pip install awscli
※ awsコマンドのパスが通ってなかった場合は通す
vi ~/.bash_profile
下記を追記
export PATH=$PATH:/usr/local/bin
設定を読み込み
source .bash_profile
環境設定
awsコマンド実行前のIAM値設定
aws configure
スクリプト作成とインスタンス起動時設定
/etc/init.d に登録する方針
vi /etc/init.d/set-eip
#!/bin/sh
# chkconfig: 2345 99 10
# description: SET EIP
INSTANCE_ID=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id`
EIP=54.248.xxx.xxx
case "$1" in
start)
echo "start!" > /var/log/set-eip.log
/usr/local/bin/aws ec2 associate-address --instance-id ${INSTANCE_ID} --public-ip ${EIP}
;;
stop)
echo "stop!" > /var/log/set-eip.log
;;
*) break ;;
esac
登録
chkconfig --add /etc/init.d/set-eip
chkconfig set-eip on
動作確認
インスタンス再起動。
再起動後、設定が反映されていることを確認。
参照
インスタンス再起動後のEIP自動割当て
http://dev.classmethod.jp/cloud/ec2shell/
https://dogmap.jp/2012/12/20/aws-advent-calendar-2012-day-20/