LoginSignup
6
5

More than 5 years have passed since last update.

AWS EC2インスタンス起動時のElasticIP自動付与

Posted at

環境

  • AWS ClassicEC2
  • python 2.6
    ※ 2.7もしくは3系を推奨

モジュールインストール

インストール pip

インストール 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/

6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5