1

I have an ASP.Net Core application hosted in an AWS Load Balance setup with x number of instances.

I what to display which EC2 instance actually serving the request on the bottom of the web page.

Is there a Nuget package or an convenient API solution to achive this ?

There is a question on Stackoverflow since 2009: Find out the instance id from within an ec2 machine that provides a solution using wget to call http://169.254.169.254/latest/meta-data/instance-id to get the instance-id

I was hoping for a Nuget package or an API like solution to achieve this.

Update - Solution

The AWSSDK.Core (The Amazon Web Services SDK for .NET - Core Runtime) has the Amazon.Util.EC2InstanceMetadata class which has the InstanceId propery which allows clean cut access to the information I needed.

Thanks for pointing me in the right direction rodrigo-m.

6
  • 3
    looks like this is what you need Find out the instance id from within an ec2 machine
    – Set
    Dec 23, 2017 at 19:28
  • Is there no AWS API access to access this information ? Dec 23, 2017 at 23:12
  • Possible duplicate of Find out the instance id from within an ec2 machine Dec 24, 2017 at 1:27
  • 1
    @AnthonyNeace that answer is geared more for shell scripting, as it uses command line wget. The question here references the use of ASP.NET SDK . In the comments, the author requests a method that uses the AWS API. Dec 24, 2017 at 18:31
  • @RodrigoM EC2Metadata doesn't use the "AWS" API at all -- it is a wrapper to expose the internal EC2 Metadata Service in the AWS SDK. The documentation that you linked says as much. :) Anyway, I don't believe that detail sufficiently distinguishes this question from the canonical question. You're welcome to contribute your solution there if you believe it is beneficial to other readers. There are 26 answers there across various languages and I don't see one yet for .NET SDK's EC2Metadata, so your answer is probably superior in implementation to the current .NET answer. Dec 24, 2017 at 21:21

1 Answer 1

3

The EC2Metadata helper class for ASP.NET was announced in this AWS Developer Blog in 2013. The article describes how this utility class provides easy access to EC2 instance metadata:

A few months ago we added a helper utility to the SDK called EC2Metadata. This is a class that provides convenient access to EC2 Instance Metadata. The utility surfaces most instance data as static strings and some complex data as .NET structures. For instance, the following code sample illustrates how you can retrieve the current EC2 instance’s Id and network interfaces...

The article also provides examples for its use:

string instanceId = EC2Metadata.GetData("/instance-id");
Console.WriteLine("Current instance: {0}", instanceId);

With this class, consider using the following approach for retrieving and storing the EC2 instance ID in your ASP.NET Core application:

  1. Using the AWS SDK for .NET Version 3 API, call the EC2 Instance Metadata class. The EC2Metadata class exposes the InstanceId property, which returns the instance Id of the current instance.

  2. Store the instance metadata in the application scope of your application, so that it can be initialized once, at application start time, then referenced as needed for display to the client, or in application logs. See Introduction to session and application state in ASP.NET Core.

Using the application scope, you only need to retrieve this data once at application initialization. This application scope data will persist for the life of the application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.