如何从ec2实例中找到ec2实例的实例id ?
当前回答
在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)
其他回答
最新的Java SDK有EC2MetadataUtils:
在Java中:
import com.amazonaws.util.EC2MetadataUtils;
String myId = EC2MetadataUtils.getInstanceId();
在Scala中:
import com.amazonaws.util.EC2MetadataUtils
val myid = EC2MetadataUtils.getInstanceId
所有与EC2资源相关的元数据都可以由EC2实例本身通过执行以下命令来访问:
旋度:
http://169.254.169.254/<api-version>/meta-data/<metadata-requested>
对于您的情况:"metadata-requested"应该是instance-id, "api-version"通常是可以使用的最新版本。
附加注意:您还可以使用上述命令获取与以下EC2属性相关的信息。
ami id, ami-launch-index, ami-manifest-path, block-device-mapping /, 主机名、 我/, instance-action, 实例id, 实例类型, local-hostname, local-ipv4, mac, 指标/, 网络/, 位置/, 配置文件, public-hostname, public-ipv4, 公钥/, 预订标识, 安全组, 服务/
欲了解更多详情,请点击此链接:https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html
你可以试试这个:
#!/bin/bash
aws_instance=$(wget -q -O- http://169.254.169.254/latest/meta-data/instance-id)
aws_region=$(wget -q -O- http://169.254.169.254/latest/meta-data/hostname)
aws_region=${aws_region#*.}
aws_region=${aws_region%%.*}
aws_zone=`ec2-describe-instances $aws_instance --region $aws_region`
aws_zone=`expr match "$aws_zone" ".*\($aws_region[a-z]\)"`
运行这个:
curl http://169.254.169.254/latest/meta-data/
您将能够看到aws提供的不同类型的属性。
使用此链接查看更多信息
简单的一行
猫/ 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
推荐文章
- 拒绝访问;您需要(至少一个)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容器的最佳方法是什么?
- 当权限为S3时,AccessDenied for ListObjects for S3 bucket:*
- 电子邮件地址未验证(AWS SES)
- 使用Boto3将S3对象作为字符串打开