如何从ec2实例中找到ec2实例的实例id ?
当前回答
PHP:
$instance = json_decode(file_get_contents('http://169.254.169.254/latest/dynamic/instance-identity/document));
$id = $instance['instanceId'];
编辑@John
其他回答
在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)
对于Windows实例:
(wget http://169.254.169.254/latest/meta-data/instance-id).Content
or
(ConvertFrom-Json (wget http://169.254.169.254/latest/dynamic/instance-identity/document).Content).instanceId
一个更现代的解决方案。
在Amazon Linux中已经安装了ec2-metadata命令。
从终点站
ec2-metadata -help
会给你可用的选项吗
ec2-metadata -i
将返回
instance-id: yourid
简单的一行
猫/ sys /设备/虚拟/ dmi / id / board_asset_tag
or
Curl_cli -s http://169.254.169.254/latest/meta-data/instance-id
来源:https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
如果你想在python中获取所有实例id列表,下面是代码:
import boto3
ec2=boto3.client('ec2')
instance_information = ec2.describe_instances()
for reservation in instance_information['Reservations']:
for instance in reservation['Instances']:
print(instance['InstanceId'])
推荐文章
- 如何查看所有地区所有正在运行的Amazon EC2实例?
- 如何从命令行使用多个AWS帐户?
- 如何搜索亚马逊s3桶?
- 拒绝访问;您需要(至少一个)SUPER特权来执行此操作
- 我如何使用通配符“cp”一组文件与AWS CLI
- 我如何获得亚马逊的AWS_ACCESS_KEY_ID ?
- 如何使所有对象在AWS S3桶公共默认?
- 为什么我应该使用亚马逊Kinesis而不是SNS-SQS?
- 如何重命名AWS S3 Bucket
- AWS ECS中的任务和服务之间有什么区别?
- 亚马逊SimpleDB vs亚马逊DynamoDB
- 亚马逊ECS和亚马逊EC2有什么区别?
- 我如何知道我在S3桶中存储了多少对象?
- S3 Bucket操作不应用于任何资源
- 将AWS凭证传递给Docker容器的最佳方法是什么?