在EC2实例中有一个魔法IP 169.254.169.254。
通过访问这个ip,能获得许多与这个实例相关的信息,这些信息被称为meta-data。
meta-data版本历史(最后一行的latest是最常用的)
wget -q -O - http://169.254.169.254/
1.0
2007-01-19
2007-03-01
2007-08-29
2007-10-10
2007-12-15
2008-02-01
2008-09-01
2009-04-04
2011-01-01
2011-05-01
2012-01-12
2014-02-25
2014-11-05
latest
wget -q -O - http://169.254.169.254/latest/meta-data/
ami-id
ami-launch-index
ami-manifest-path
block-device-mapping/
hostname
instance-action
instance-id
instance-type
local-hostname
local-ipv4
mac
metrics/
network/
placement/
profile
public-hostname
public-ipv4
public-keys/
reservation-id
security-groups
services/
大部分的信息看了名字就能知道返回的是什么信息。
例如启动这个实例用的ami-id
wget -q -O - http://169.254.169.254/latest/meta-data/ami-id
ami-0d4cfd66
当前实例的instance id
wget -q -O - http://169.254.169.254/latest/meta-data/instance-id
i-6c4d3985
通过meta-data提供了另一种获取方式。
wget -q -O - http://169.254.169.254/latest/meta-data/local-ipv4
172.31.43.240
ifconfig
eth0 Link encap:Ethernet HWaddr 06:DC:B0:05:FE:C1
inet addr:172.31.43.240 Bcast:172.31.47.255 Mask:255.255.240.0
临时meta-data
有些meta-data信息不是一直可见,只有到具体事件发生后,才会出现。
例如,Spot Instance由于市场价格提升的原因,在2分钟内会被关闭的时候
http://169.254.169.254/latest/meta-data/spot/termination-time
这里将会返回具体的关机时间。
平常访问这个url会返回404错误
wget http://169.254.169.254/latest/meta-data/spot/termination-time
--2015-08-31 14:56:50-- http://169.254.169.254/latest/meta-data/spot/termination-time
正在连接 169.254.169.254:80... 已连接。
已发出 HTTP 请求,正在等待回应... 404 Not Found
2015-08-31 14:56:50 错误 404:Not Found。
当这个url返回时间值的时候,立即利用这2分钟时间做清理工作。