如何从ec2实例中找到ec2实例的实例id ?


当前回答

一个更现代的解决方案。

在Amazon Linux中已经安装了ec2-metadata命令。

从终点站

ec2-metadata -help

会给你可用的选项吗

ec2-metadata -i

将返回

instance-id: yourid

其他回答

在AWS Linux上:

Ec2-metadata——instance-id | cut -d " " -f

输出:

i - 33400429

在变量中使用:

ec2InstanceId=$(ec2-metadata --instance-id | cut -d " " -f 2);
ls "log/${ec2InstanceId}/";

PHP:

$instance = json_decode(file_get_contents('http://169.254.169.254/latest/dynamic/instance-identity/document));
$id = $instance['instanceId'];

编辑@John

对于powershell用户:

(New-Object System.Net.WebClient).DownloadString("http://169.254.169.254/latest/meta-data/instance-id")

所有与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

运行这个:

curl http://169.254.169.254/latest/meta-data/

您将能够看到aws提供的不同类型的属性。

使用此链接查看更多信息