-1

Not sure if this is a duplicate, but I would like to get an IP address of the new span up instance of AWS Auto Scaling Group.

How can I achieve this?

I want either a lambda function or maybe a script. What I want is, once an instance is spin up, get the IP, send that IP to some server somewhere.

Shammir
  • 927
  • 4
  • 17
  • 32
  • Can you elaborate on the usecase? You mean in the AWS console? Or in some kind of script or lambda? Because ec2s you can just filter in the AWS console. – LRutten Sep 09 '21 at 08:56

2 Answers2

2

Easiest way would be to send this via the instance's cloud init script or startup script (user-data).

I imagine your instance would already have a cloud init script or a startup script which would run when the instance starts. You can add command in that script to fetch the ip from the instance metadata url, using below command:

curl http://169.254.169.254/latest/meta-data/public-ipv4

And then you can send this value to wherever you need, either by passing this value to an already created lambda function, or directly to the entity that needs this value.

1

Create a User data script, which would fetch the public IP of the instance and send it via curl to you destination.

To get the IP you could either hit the instance meta data service-

curl http://169.254.169.254/latest/meta-data/public-ipv4

or some other general public IP finder- curl ipinfo.io

Sending to a destination would simply mean crafting a curl payload and sending

curl -XPOST http://<destination>/ -d {ip: <IP here>}

Over all the script should look somewhat like-

ip=$(curl http://169.254.169.254/latest/meta-data/public-ipv4) 
curl -XPOST http://<destination>/ -d {'ip' : $ip}

Just Put this script into the user data for the Launch config for your ASG.

Kashish Soni
  • 161
  • 4