使用用户数据脚本自动为 ec2 Spot 实例分配弹性 IP

red*_*and 6 automation scripting amazon-ec2 amazon-web-services

是否可以使用“高级”“用户数据”脚本框将弹性 IP 地址自动分配给具有持久 Spot 请求的 Spot 实例?

我在想我可以制作一个像...

#!/bin/bash 

# Credentials
export AWS_ACCESS_KEY=(insert key here) 
export AWS_SECRET_KEY=(insert key here) 

# EC2 Instance ID
instanceid=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/instance-id` 

# Associate EIP with the captured instance ID.
ec2-associate-address -i $instanceid (insert EIP here)
Run Code Online (Sandbox Code Playgroud)

并在我完成我的现场请求之前将其粘贴在用户数据框中(使用 web gui)。对此有何想法或更好的方法?我不需要自动缩放,只需使用这个根据现货价格上下波动的实例,但我希望它每次回来时都保留相同的 IP。

Ale*_*ekh 2

最近,我还考虑自动化将弹性 IP 重新分配给我的现货实例的过程。经过一些互联网研究,我发现了一些关于该主题的可靠的操作资源:

  1. http://www.newvem.com/how-to-automate-elastic-ip-assignment-on-ec2-instance-restart-or-reboot
  2. http://www.idevelopment.info/data/AWS/AWS_Tips/AWS_Management/AWS_14.shtml
  3. https://boto.readthedocs.org/en/latest/ref/ec2.html(不完全是 HOWTO,但仍然可能有用)。

希望这可以帮助!