Script to dynamically get Private IP of an EC2 instance

Viewed 23

I am writing an ELK deployment script and one of the things I need it to do is to edit a configuration file and insert some lines. Is there a way to make it retrieve the private IP of whichever EC2 instance the script is being run on?

For instance,let's assume that my instance's private ip is 10.0.10.154 and my script has the following lines:

echo " host: 10.0.10.154:9200" | sudo tee -a /etc/elasticsearch/elasticsearch.yml

Is there a way to make my script dynamically retrieve and replace 10.0.10.154 with the private IP of whichever instance the script is being ran on?

Or, if each user creates a .profile file on their machine and and describe the private IP of the instance ELK_Private IP: 10.0.10.254 or whatever their iP is - how can I retrieve this value into my script and input into the yml config file?

1 Answers

This should give you local IP address :

curl -s http://169.254.169.254/latest/meta-data/local-ipv4
Related