简体   繁体   中英

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...

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.

There's a webservice that returns machine information. Access

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

To retrieve the instance id

The Instance Metadata Documentation can be handy.

There's a much easier method than making a web API call, the .NET SDK.

See the SDK documentation for EC2InstanceMetadata here

For example, if you need InstanceId you can use:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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