How to find local IP address for ec2 machine in “Linux 2” environment?

0

In the Amazon Linux AMI environment, I was able to find the IP address of one of my elastic beanstalk EC2 machines by running this command:

export LOCAL_HOST_IP=`wget -qO- http://instance-data/latest/meta-data/local-ipv4`  

But after switching to the Amazon Linux 2 environment, it no longer works, even if I try the 169.x.x.x address:

/app $ wget -qO- http://instance-data/latest/meta-data/local-ipv4  
wget: bad address 'instance-data'  
/app $  
/app $ wget -qO- http://169.254.169.254/latest/meta-data/local-ipv4  
wget: server returned error: HTTP/1.0 401 Unauthorized  

I am unable to find info on this, e.g., this post has no info about “Linux 2” https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-instance-addressing.html

Are there additional permissions I need to set for my elastic beanstalk machines or is there a new way to do this? I need this information so can inform my app as to what hostname the the ELB will ping it at!

gefragt vor 3 Jahren1087 Aufrufe
1 Antwort
0

I’m seeing that this is related to IMDSv1 being disabled on my machines and that I need to either enable it or—better—use IMDSv2, which requires tokens, e.g.,

TOKEN=`curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600"`  
LOCAL_HOST_IP =`curl -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/local-ipv4`  

Docs for IMDSv2 https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html

beantwortet vor 3 Jahren

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen