Here are three ways to get the instance ID of an EC2 server using the command line.

Built-in

If you are logged into an Ubuntu machine.

ec2metadata --instance-id

Curl

curl command using an IP address.

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

curl command simplified.

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

Wget

If curl is not available, try wget.

wget -q -O - http://169.254.169.254/latest/meta-data/instance-id

Resources