Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get aws instance metadata remotely using CLI?

I am very new to AWS. I have a Windows Server EC2 instance. I installed AWS CLI on my laptop. Then I opened a CMD window, typed in "aws configure", put in the access key credentials, and was able to connect to the EC2.

From here, how do I get the http://169.254.169.254/latest/meta-data working? How do I retrieve some meta data?

like image 987
Silly Dude Avatar asked Jun 06 '17 11:06

Silly Dude


2 Answers

On your Laptop

On your local machine you only can use the cli to retrieve metadata about your instance. Simply use this aws cli command:

aws ec2 describe-instance-attribute --instance-id <your-ec_instance_id e.g. i-ab12345> --attribute instanceType --region <your_region e.g. eu-west-1>

Documentation: http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instance-attribute.html

On your EC2-Instance only: On your instance you can use the cli (like above) and the following:

PowerShell >3.0:

Invoke-RestMethod -uri http://169.254.169.254/latest/meta-data/instance-type

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

Or you can install "curl for windows" and run:

curl http://169.254.169.254/latest/meta-data/instance-type
like image 165
lwiedenhoeft Avatar answered Sep 18 '22 12:09

lwiedenhoeft


When running on an EC2 instance, you can query the metadata service, like so:

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

You can also use:

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

From outside the EC2 instance, you can use the awscli, like so:

aws ec2 describe-instances
    --instance-ids i-01234567890123456
    --query "Reservations[0].Instances[0].PublicIpAddress"
    --output text
like image 33
jarmod Avatar answered Sep 22 '22 12:09

jarmod