我想在启动时自动分配EIP给一个实例。 我知道我可以编写一个脚本来停止/启动实例,并使用EC2工具分配我想要的EIP,但这取决于我停止/启动服务器。 在Amazon停止/启动实例的EC2中断或硬件故障的情况下,EIP将不会被重新分配。
之前我问过一个人这个问题,他们暗地里提到,启动后可以通过框内的脚本来完成。 然后他们下线,所以我不能跟进。
有什么办法在启动的时候将EIP绑定到实例?
同意埃里克,这个选项在安全性方面并不明智。 另一个select是使另一台机器拥有凭证,负责响应来自其他机器的请求。 例如:具有我的凭证的机器是EC2-1。 您可能启动2台机器来运行您的Web服务器,EC2-2和EC2-3。 当他们引导时,他们可以“发信号”给EC2-1,EC2-1又会运行API调用,将EC2-2和EC2-3关联到两个弹性IP。 这样,您必须使EC2-1非常安全,而且您不会对其他机器造成危险。
最好,
西蒙娜
您可以使用类似的方法在服务器上的/etc/rc.local中执行此操作:
ec2-associate-address --private-key /root/private_key.pem --cert /root/public_key.pem <eip-address> -i `curl http://169.254.169.254/latest/meta-data/instance-id
使用VPC,那么你将不必担心这个问题。
这里是我写的一个bash,通过使用友好的Name =“tag”来改变任何VPC实例上的EIP,你也可以指定一个默认区域,或者添加到命令中。
#change vpc instance public IP address (EIP -> NIC|INSTANCE) #usage "changeip [instance friendly tag=Name] [region]" #example "changeip my.instnace us-west-1" #dafault region is us-west-1 (you must include --region for $region default) #for VPC instances only function changeip { if [[ ! $1 ]]; then echo 'Error : You must provide tag name for instance' echo 'Example: changeip [friendly name]' return fi if [[ $2 ]]; then region='--region '$2 echo 'Using region '$2 else region='--region us-west-1' #sets default region echo 'Using default '$region fi name=$1 instance=$(ec2-describe-instances $region | grep Name | grep $name | cut -f3) if [[ ! $instance =~ ^('i-'[A-Za-z0-9]*)$ ]]; then echo 'Error : Getting the instance id' echo $instance return fi echo 'Applying to '$1 '=> '$instance echo 'Please wait....' ip_new=$(ec2-allocate-address $region -d vpc | cut -f2) if [[ ! $ip_new =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then echo 'Error : Getting a new IP address' echo $ip_new return fi new_idas=$(ec2-describe-addresses $region $ip_new | cut -f 5) >> /dev/null if [[ ! $new_idas =~ ^('eipalloc-'[A-Za-z0-9]*)$ ]]; then echo 'Error : Getting New IP allocation id eipalloc' echo $new_idas return fi ip_old=$(ec2-describe-addresses $region | grep $instance | cut -f2) if [[ ! $ip_old =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then echo 'Error : Getting old IP address' echo $ip_old return fi id_dis=$(ec2-describe-addresses $region $ip_old | cut -f 6) if [[ ! $id_dis =~ ^('eipassoc-'[A-Za-z0-9]*)$ ]]; then echo 'Error : Dissasociating Old IP' echo $id_dis return fi id_release=$(ec2-describe-addresses $region $ip_old | cut -f 5) >> /dev/null if [[ ! $new_idas =~ ^('eipalloc-'[A-Za-z0-9]*)$ ]]; then echo 'Error : Release Old IP' echo $id_release return fi ec2-disassociate-address $region -a $id_dis >> /dev/null sleep 8 ec2-release-address $region -a $id_release >> /dev/null ec2-associate-address $region -i $instance -a $new_idas >> /dev/null echo 'SUCCESS!' echo 'Old = '$ip_old echo 'New = '$ip_new }