Get AWS Instance Info with ec2-metadata

AWS AWS

If you are just starting with AWS, you may not know that Amazon Linux images have a special command for confirming most useful information about EC2 instance: ec2-metadata.

Instance info with ec2-metadata

If you just run the command without options, you’ll get to see all the available meta info.

Here’s an example from the vps2.unixtutorial.org instance (give me a shout if you want access to it for learning Linux!):

[ec2-user@ip-10-10-0-245 ~]$ ec2-metadata
ami-id: ami-466768ac
ami-launch-index: 0
ami-manifest-path: (unknown)
ancestor-ami-ids: not available
block-device-mapping:
ami: /dev/xvda
root: /dev/xvda
instance-id: i-0fe057ab7e5ff2XYZ
instance-type: t2.nano
local-hostname: ip-10-10-0-245.eu-west-1.compute.internal
local-ipv4: 10.10.0.245
kernel-id: not available
placement: eu-west-1b
product-codes: not available
public-hostname:
public-ipv4: 52.209.10.113
public-keys:
keyname:unixtutorial
index:0
format:openssh-key
key:(begins from next line)
ssh-rsa //P6MR9UiNDVDOi7vZ7Um/O+nJwtfVNnqiPfzRFqvm11yIo1WmiyvC3Ilhcowqfd2WJBSb3gwuJVxZk9paBp+CvVU2i99OJO+ss10656g3hBgS2xlMatPWyM/Ab6uZOO0X6NbTL/kSbThsnSyZadue36Qt1pcPWoIp0cV unixtutorial
ramdisk-id: not available
reservation-id: r-0b1d0abebXYZ
security-groups: launch-wizard-15
user-data: not available

Command line options for ec2-metadata

There are also lots of command line options to show individual fields from this output.

Confirm AMI image ID with ec2-metadata

[root@ip-10-10-0-245 ~]# ec2-metadata -a
ami-id: ami-466768ac

Confirm Current EC2 Instance ID with ec2-metadata

[root@ip-10-10-0-245 ~]# ec2-metadata -i
instance-id: i-0fe057ab7e5ff2f1d

Confirm Internal IP addresses with ec2-metadata

[root@ip-10-10-0-245 ~]# ec2-metadata -o
local-ipv4: 10.10.0.245

Confirm external IP address with ec2-metadata

This will only work if you actually have external IPv4 address mapped to your instance.

[root@ip-10-10-0-245 ~]# ec2-metadata -v
public-ipv4: 52.209.10.113

Are you interested in more AWS related posts and Unix tutorials? Please let me know!

See Also




Keep Learning

Follow me on Facebook, Twitter or Telegram:
Recommended
I learn with Educative: Educative
IT Consultancy
I'm a principal consultant with Tech Stack Solutions. I help with cloud architectrure, AWS deployments and automated management of Unix/Linux infrastructure. Get in touch!

Recent Tweets