On Amazon Linux AMIs you can do:
$ ec2-metadata -i instance-id: i-1234567890abcdef0
Or, on Ubuntu and some other linux flavours, ec2metadata --instance-id (This command may not be installed by default on ubuntu, but you can add it with sudo apt-get install cloud-utils)
As its name suggests, you can use the command to get other useful metadata too.