如何从ec2实例中获取实例ID? [英] How to get the instance id from within an ec2 instance?
本文介绍了如何从ec2实例中获取实例ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何从ec2实例中查找ec2实例的实例ID
?
How can I find out the instance id
of an ec2 instance from within the ec2 instance?
推荐答案
请参阅 EC2文档。
运行:
wget -q -O - http://169.254.169.254/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://169.254.169.254/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://169.254.169.254/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://169.254.169.254/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:'`"
您也可以使用 curl
而不是 wget
,具体取决于您的平台上安装了什么。
You may also use curl
instead of wget
, depending on what is installed on your platform.
这篇关于如何从ec2实例中获取实例ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文