2

I was successfully able to attach another instance id from inside powershell if aws ec2 windows instance using CFT.

Can someone please help me how to get the instance id of the instance being created as I need to use inside the user data for ELB registration. So, in simpler words, i want to register the same instance that i am creating.

Below is my working command :-

Register-ELBInstanceWithLoadBalancer -LoadBalancerName ire798ELB -Instances i-b90d8d06 -Region us-east-1

But here i-b90d8d06 is some other instance id and not the one i am creating.

Mannu
  • 65
  • 6
  • 1
    Possible duplicate of [Find out the instance id from within an ec2 machine](http://stackoverflow.com/questions/625644/find-out-the-instance-id-from-within-an-ec2-machine) – Anthony Neace Oct 15 '15 at 21:30
  • You need to clarify your question. If you are asking how "instance x" can discover it's own instance ID, then your question is a duplicate. On the other hand, if you are running code on "instance y" that is spinning up a new "instance x" and you need to learn the instance ID of new "instance x" using code running on "instance y" then you need to be more clear with what you are asking, when you say "instance I am creating," because as it stands, your question is being misinterpreted. – Michael - sqlbot Oct 15 '15 at 23:53
  • It's also possible that you are being misinterpreted because you are trying to do something the hard way, and this actually is an x/y question of another sort: http://xyproblem.info – Michael - sqlbot Oct 15 '15 at 23:56

1 Answers1

5

See the EC2 documentation on Instance Metadata and User Data.

Run:

$id = (Invoke-WebRequest -Uri  http://169.254.169.254/latest/meta-data/instance-id).content

$id will contain the current instance id.

Rodrigo Murillo
  • 13,080
  • 2
  • 29
  • 50
  • Thanks Rordigo..This was exactly what i was looking for. It worked for me . Below is the code snippet in powershell that is working :- "$INSTANCE_ID=Invoke-RestMethod http://169.254.169.254/latest/meta-data/instance-id \n", "Register-ELBInstanceWithLoadBalancer -LoadBalancerName Tableau-QA-PERF-ELB -Instances $INSTANCE_ID -Region us-east-1\n", – Mannu Oct 16 '15 at 21:52