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!

asked 3 years ago1059 views
1 Answer
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

answered 3 years ago

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