By using AWS re:Post, you agree to the Terms of Use
/Problem receiving IP 127.0.0.1 at service startup instead of local IP/

Problem receiving IP 127.0.0.1 at service startup instead of local IP

0

Context:

We've got a number of load balanced web servers running on Windows OS in AWS using C# .NET (5). We have a web server application as well as a Windows Service running on the same machine and we have problems with logging from the Windows Service.

Problem Description:

Since we have many servers running load balanced, we name the log stream with the private IP number in order to distinguish which machine that potentially has problems. This private IP is extracted at startup of the application (for both the Windows Service and the Web Server.) This is usually sucessfull, but yesterday we had an incident when one Windows Service log stream was labeled with 127.0.0.1 instead of the local IP number. Eventually I was able to pinpoint which server it was, restarted the windows service, which made the private IP number appear instead in the new log stream name.

?: Suggested reason with possible solution:

I'm guessing this is a race condition error. The machine has not received it's private IP number yet by AWS network before our service asked for it. If so we can wait for the real IP to appear just to make sure we get the right IP number in our log. I have three question related to this:

Questions:

  1. Do you see any other reason than the one I suggested why the IP number 127.0.0.1 appears?
  2. Is there a better solution available than the one I suggested?
  3. Is there a way, using an AWS API of some sort to get hold of the public IP for the server?

Here's the code how we extract the private IP address in this context:

var hostName = System.Net.Dns.GetHostName();
var ipAddresses = System.Net.Dns.GetHostAddresses(hostName);
var ipv4Address = ipAddresses.FirstOrDefault(ip => ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork);
2 Answers
0
Accepted Answer
  1. I think your hypothesis is correct.
  2. You could do as you suggest. But why not use the Instance Metadata Service to retrieve the instance id instead? It would be much more unique; and would avoid an issue where an instance is reusing an IP address from some time in the past.
  3. Using the instance id you can query the EC2 control plane; find the attached network interfaces; then find the associated public/elastic IP address. However, the servers are behind a load balancer so will (most likely) not have an individual public IP.
EXPERT
answered a month ago
    1. Great. Thanks for input.
    2. Interesting idea yes. However, I like the idea of the public IP more since it shortens the time for us to remote to the server if there is a problem.
    3. Thanks for the input. I'll look into this.
0

Hi.

You are using FirstOrDefault, have you checked if you can get only one?
You can exclude the loopback address as follows: https://docs.microsoft.com/en-us/dotnet/api/system.net.ipaddress.isloopback?view=net-6.0

ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork &&! ipAddresses.isLoopback (ip)
answered a month ago
  • Yes. I actually saw this possibility in terms of the following piece of code. Not sure if I understand all of it though.. :(

          public IPAddress GetLocalIp()
            {
                var firstUpInterface = NetworkInterface.GetAllNetworkInterfaces()
                    .OrderByDescending(c => c.Speed)
                    .FirstOrDefault(c => c.NetworkInterfaceType != NetworkInterfaceType.Loopback && c.OperationalStatus == OperationalStatus.Up);
    
                var props = firstUpInterface?.GetIPProperties();
    
                return props?.UnicastAddresses
                    .Where(c => c.Address.AddressFamily == AddressFamily.InterNetwork)
                    .Select(c => c.Address)
                    .FirstOrDefault();
            }

    However, I guess it does not solve the actual problem in retrieving the private IP adress since it does not exist yet according to my hypothesis.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions