找出实例ID从EC2机内 [英] Find out the instance id from within an ec2 machine
本文介绍了找出实例ID从EC2机内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何从我的什么实例ID是一个EC2机(用户根目录)中发现了什么?
How can I find out from within an ec2 machine (user root) what my instance id is?
推荐答案
请参阅关于这个问题的EC2文档。
运行:
wget -q -O - http://instance-data/latest/meta-data/instance-id
如果您需要通过编程方式访问该实例ID在脚本中,
If you need programatic access to the instance ID from within a script,
die() { status=$1; shift; echo "FATAL: $*"; exit $status; }
EC2_INSTANCE_ID="`wget -q -O - http://instance-data/latest/meta-data/instance-id || die \"wget instance-id has failed: $?\"`"
更高级的用一个例子(检索实例ID以及可用性区域和区域等):
An example of a more advanced use (retrieve instance ID as well as availability zone and region, etc.):
EC2_INSTANCE_ID="`wget -q -O - http://instance-data/latest/meta-data/instance-id || die \"wget instance-id has failed: $?\"`"
test -n "$EC2_INSTANCE_ID" || die 'cannot obtain instance-id'
EC2_AVAIL_ZONE="`wget -q -O - http://instance-data/latest/meta-data/placement/availability-zone || die \"wget availability-zone has failed: $?\"`"
test -n "$EC2_AVAIL_ZONE" || die 'cannot obtain availability-zone'
EC2_REGION="`echo \"$EC2_AVAIL_ZONE\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"
您也可以使用卷曲
而不是的wget
,这取决于安装在您的平台上。
You may also use curl
instead of wget
, depending on what is installed on your platform.
这篇关于找出实例ID从EC2机内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文