You can use two methods to get the info of an EC2 instance:
Access Instance Metadata and User Data from the EC2 API
From the CLI of your instance use the following URI to retrieve all the instance metadata.
http://169.254.169.254/latest/meta-data/
You will see a list of available categories. If you need the AMI Id make a curl request:
curl http://169.254.169.254/latest/meta-data/ami-id
Use the Instance metadata Query Tool
This is basically a wrapper for the first method, see https://aws.amazon.com/code/1825.
Some instance types come with this tool preinstalled, in others you have to download and install the tool.
Once the tool is present just execute:
ec2metadata
You should see a list of all instance properties, this is pretty handy to use that info programmatically.
If you want to identify the instance when you are logged in into the shell via SSH you can add the instance id to the CLI prompt using the User Data feature of EC2 when launching the instance.
Just add the following script to the user data field when launching the instance:
#cloud-config
apt_upgrade: false
preserve_hostname: true
runcmd:
- hostname "`ec2metadata --instance-id`"
- echo "127.0.0.1 `ec2metadata --instance-id`" >> /etc/hosts
- echo "`ec2metadata --instance-id`" > /etc/hostname
This script will execute at start time and change the hostname of the instance with the instance id.
You can use either of two methods described above and any of the metadata attributes to identify the instance.