Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify Amazon AWS EC2 instance?

Tags:

amazon-ec2

Is there a system file that authoritatively tells me if a host is an Amazon AWS EC2 instance?

Bonus point: without installing anything new, is there a command that will tell me some basic parameters of an EC2 instance?

Context: ~~~~~~~~ I have a script that gathers information.
If I run the script on an EC2 instance, some "standard" commands are not available, for example: dmidecode (this gives me practically nothing), or virt-what.

I am aware of /usr/bin/ec2-describe-instances, or wget for metadata script, or wget for individual components reported by metadata, but I don't want to install anything new, and I need the tool to describe the host itself (since the script runs locally), not to inquire another host, or pass a key that I must obtain from yet another script that I must install first. Maybe wget for a specific metadata info is the best tool I could use?

Thanks

like image 466
user3250493 Avatar asked Jan 18 '26 14:01

user3250493


1 Answers

I cannot guarantee this will work on any other AMI than Ubuntu 12.04 (I am using ami-8e987ef9) - please test yourself.

Here is what you might want to check out if you really want to avoid getting this info from the magic IP 169.254.169.254 and metadata as @Rico suggested:

### Datasource EC2
ubuntu@ip-10-33-59-70:~$ cat /var/lib/cloud/instance/datasource 
cloudinit.DataSourceEc2.DataSourceEc2: DataSourceEc2
ubuntu@ip-10-33-59-70:~$ cat /var/lib/cloud/data/previous-datasource 
cloudinit.DataSourceEc2.DataSourceEc2: DataSourceEc2

### Hostname
ubuntu@ip-10-33-59-70:~$ cat /var/lib/cloud/data/previous-hostname 
ip-10-33-59-70

### Instance ID
ubuntu@ip-10-33-59-70:~$ cat /var/lib/cloud/data/previous-instance-id 
i-280ace69

### Instance ID also (check out instance symlink)
ubuntu@ip-10-33-59-70:/var/lib/cloud$ ls -al | grep instance
lrwxrwxrwx  1 root root   22 Jan 29 22:00 instance -> ./instances/i-280ace69
drwxr-xr-x  3 root root 4096 Jan 29 22:00 instances

Maybe looking around /var/lib/cloud will give you some info that you need without using curl.

I would rather suggest using the magic IP with metadata though.

On Ubuntu 12.04 there's also /usr/bin/ec2metadata, a util written in Python that actually queries 169.254.169.254.

like image 62
Michal Gasek Avatar answered Jan 20 '26 22:01

Michal Gasek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!