Reputation: 121
I need to dynamically discover the ip4v address of my instance (during userdata script execution and use it to replace some XML. I've tried the following but it doesn't work. Presumably because networking isn't available at the time we the instance runs the userdata script?
Also, to add complexity, assume this instance has no external internet access (so other solutions suggesting curl http://checkip.amazonaws.com/
won't work).
sudo sed -i -e "s,<listenaddress address=\",<listenaddress address=\"$(wget -qO- http://instance-data/latest/meta-data/public-ipv4),g" /tmp/my.config.xml
I'm using the default Amazon AMI (Amazon Linux AMI 2017.03.1 (HVM), SSD Volume Type - ami-ed100689)
Any ideas?
Edit: Full userdata script
#!/bin/bash
# This script starts a extra small service with 512MB heap.
# USE ONLY FOR DEMOS
sudo useradd myuser
sudo groupadd myuser
sudo usermod -a -G myuser myuser
sudo yum update -y
sudo yum install java-1.8.0-openjdk wget -y
sudo wget https://files.mysite.com/downloads/service/whatever.jar -P /opt
sudo /usr/bin/java8 -jar /opt/whatever.jar -b 64 -t /opt/software
sudo chown -R myuser:myuser /opt/software
sudo cp /opt/software/init.d/serviceFile /etc/init.d
# Other SED commands on /etc/init.d/serviceFile which ARE working
sudo chkconfig --add /etc/init.d/serviceFile
# Start service to generate XML config file
sudo service serviceFile start
# Stop service to release file locks
sudo service serviceFile stop
# Lookup IP and do replacement <-- Problem here
sudo sed -i -e "s,<listenaddress address=\",<listenaddress address=\"$(wget -qO- http://instance-data/latest/meta-data/public-ipv4),g" /opt/software/conf/collector.config.xml
# Restart service
sudo service serviceFile start
Upvotes: 2
Views: 2159
Reputation: 473
curl http://169.254.169.254/latest/meta-data/public-ipv4
You can fetch a bunch of data about current instance from this ip
Upvotes: 5
Reputation: 52413
It is likely /tmp/my.config.xml
is created after the execution of the script. Remember everything in /tmp
is wiped out when an instance is launched.
I tried it with /home/ec2-user/my.config.xml
and it worked.
Upvotes: 0