Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get InstanceID via ec2 api?

I need a function wich tells me the instanceID, I was searching here for a function, but you always need the id... Yeah and this is the problem.

I'm not allowed to use the console, need to find it out via a script.

I saw the AWS.MetadataService documentation, but I can't handle it. I just can see single pieces but I don't know how to match them right, to geht what I want. At the moment I have this

var meta  = new AWS.MetadataService();

meta.request("http://169.254.169.254/latest/meta-data/", function(err, data){
    console.log(data);
});

But ofc this dont works... What needs to be in the path parameter?

like image 777
nova Avatar asked May 20 '16 15:05

nova


People also ask

How do I get PEM file from AWS?

In the left navigation pane, under Network & Security, choose Key Pairs. Choose Create Key Pair and name your key pair your AWS Management Console username (e.g. student01). Choose Create. A PEM file is downloaded in your browser.

Can I see Lightsail instances in EC2?

Instances created from the Lightsail console always display the Lightsail key in the Amazon EC2 console, even if the Lightsail key is removed from the instance.


1 Answers

If your script is running on the EC2 instance for which you want the ID, you can get the EC2 instance ID from the instance meta-data. This command will give you the EC2 instance ID (eg. i-12345678):

curl http://169.254.169.254/latest/meta-data/instance-id

Full docs for the meta-data can be found here:

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html

Update:

For something in Node, try this:

var meta  = new AWS.MetadataService();

meta.request("/latest/meta-data/instance-id", function(err, data){
    console.log(data);
});

Don't include the http:// and host parts. Just the final path.

like image 82
Matt Houser Avatar answered Oct 26 '22 07:10

Matt Houser