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!

demandé il y a 3 ans1087 vues
1 réponse
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

répondu il y a 3 ans

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions