AWS sees to it that the Instance ID is available as a part of the EC2 meta data.
you can use the following command to retrieve the Instance ID.
EC2_instance_id=`wget -q -O - http://169.254.169.254/latest/meta-data/instance-id`
You can also use the above mentioned command inside of a script to retrieve the instance ID.