简体   繁体   English

在实例本身中获取EC2实例属性的最简单方法

[英]Easiest way to get EC2 instance attributes within the instance itself

Can you guys show me how to retrieve instanceId, dns public name and type of current EC2 instance from where I'm running the code... 你们能告诉我如何从我运行代码的地方检索instanceId,dns公共名称和当前EC2实例的类型......

I'm playing with DescribeInstanceAttribute() , but it needs to supply the instanceId to the request, and I can't find how to get the id of currently running instance. 我正在使用DescribeInstanceAttribute() ,但它需要向请求提供instanceId,我找不到如何获取当前正在运行的实例的id。

There's a webservice that returns machine information. 有一个Web服务返回机器信息。 Access 访问

http://169.254.169.254/latest/meta-data/instance-id

To retrieve the instance id 检索实例ID

The Instance Metadata Documentation can be handy. 实例元数据文档非常方便。

There's a much easier method than making a web API call, the .NET SDK. 有一种比创建Web API调用(.NET SDK)更简单的方法。

See the SDK documentation for EC2InstanceMetadata here 请在此处查看EC2InstanceMetadataSDK文档

For example, if you need InstanceId you can use: 例如,如果您需要InstanceId,您可以使用:

Amazon.Util.EC2InstanceMetadata.InstanceId.ToString();

All the other properties are available in a similar manner. 所有其他属性都以类似的方式提供。

Note that the SDK used to have Amazon.EC2.Utils - this was deprecated in 2015 and moved to Amazon.Util namespace 请注意,SDK曾用于拥有Amazon.EC2.Utils - 这在2015年已弃用,并已移至Amazon.Util命名空间

您还可以使用AWS的Dot Net SDK ,例如Amazon.EC2.Util.EC2Metadata.InstanceId.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM