如何从ec2实例中获取实例ID?
如何从ec2实例中找出ec2实例的instance id ?
请参阅有关该主题的EC2文档。
跑:
wget -q -O - http://169.254.169.254/latest/meta-data/instance-id
如果您需要通过脚本访问实例ID,
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以及可用区和区域等):
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 ,具体取决于平台上安装的内容。
在Amazon Linux AMI上,您可以执行以下操作:
$ ec2-metadata -i
instance-id: i-1234567890abcdef0
或者,在Ubuntu和其他一些linux版本中, ec2metadata --instance-id (这个命令可能不是默认安装在ubuntu上的,但你可以使用sudo apt-get install cloud-utils来添加它)
顾名思义,您可以使用该命令获取其他有用的元数据。
在Ubuntu上,您可以:
sudo apt-get install cloud-utils
然后你可以:
EC2_INSTANCE_ID=$(ec2metadata --instance-id)
您可以通过这种方式获得与实例关联的大部分元数据:
ec2metadata --help
Syntax: /usr/bin/ec2metadata [options]
Query and display EC2 metadata.
If no options are provided, all options will be displayed
Options:
-h --help show this help
--kernel-id display the kernel id
--ramdisk-id display the ramdisk id
--reservation-id display the reservation id
--ami-id display the ami id
--ami-launch-index display the ami launch index
--ami-manifest-path display the ami manifest path
--ancestor-ami-ids display the ami ancestor id
--product-codes display the ami associated product codes
--availability-zone display the ami placement zone
--instance-id display the instance id
--instance-type display the instance type
--local-hostname display the local hostname
--public-hostname display the public hostname
--local-ipv4 display the local ipv4 ip address
--public-ipv4 display the public ipv4 ip address
--block-device-mapping display the block device id
--security-groups display the security groups
--mac display the instance mac address
--profile display the instance profile
--instance-action display the instance-action
--public-keys display the openssh public keys
--user-data display the user data (not actually metadata)
链接地址: http://www.djcxy.com/p/32429.html
上一篇: How to get the instance id from within an ec2 instance?
