Posted at

[AWS][EC2] インスタンスを停止~起動しても同じEIPを取得する方法

More than 5 years have passed since last update.

EC2のインスタンスを停止 -> 起動すると、せっかく日もづけた、EIPが外れてしまうので起動時に設定したEIPを自動的にバインドし直すshellを作成してみたメモです


/etc/rc.local

# cat /etc/rc.local 

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.

touch /var/lock/subsys/local
# Update EIP
/usr/local/shell/bind_aws_eip.sh i-97068580 XXX.XXX.XXX.XXX > /tmp/bind_aws_eip.log 2>&1


bind_aws_eip.sh

# cat /usr/local/shell/bind_aws_eip.sh

#!/bin/bash

_AZ=ap-northeast-1
_IID=$1
_EIP=$2

export AWS_ACCESS_KEY='QKIAIGVVNBUVFV214HPHQ'
export AWS_SECRET_KEY='5isNk91VHB2WrabblsasGeU6m9sawYCgC5BWqb'
export JAVA_HOME="/usr"
export EC2_HOME="/opt/aws/apitools/ec2"

/opt/aws/bin/ec2-associate-address --region ${_AZ} -i ${_IID} ${_EIP}

/opt/aws/bin/ec2-describe-addresses --region ${_AZ}

exit 0

これで手動でEIPを設定しなくてもよくなるはず・・・です。