How do I get ec2 Linux instance info while logged-in on the command-line (public dns, AMI, etc.)

12,188

Solution 1

I'm assuming you want to do this from the bash shell rather than with the boto python library or similar...

You should be able to query the metadata service on 169.254.169.254.

e.g.

AMI_ID=$(curl http://169.254.169.254/latest/meta-data/ami-id)

You can get a listing of what meta-data is available with:

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

This is documented at http://docs.amazonwebservices.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html

Note: I am more familiar with openstack's metadata service, but that was modelled on the Amazon EC2 service. The quantity and types of data available are different, but accessing it is the same.

Solution 2

You can use two methods to get the info of an EC2 instance:

  1. 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

  2. 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.

Solution 3

Personally, i just run facter. (Part of puppet)

ec2_ami_id => ami-ffffff
ec2_ami_launch_index => 0
ec2_ami_manifest_path => (unknown)
ec2_block_device_mapping_ebs1 => /dev/sda
ec2_block_device_mapping_ebs3 => /dev/sdf
ec2_block_device_mapping_ephemeral0 => /dev/sdb
ec2_block_device_mapping_ephemeral1 => /dev/sdc
ec2_block_device_mapping_ephemeral2 => /dev/sdd
ec2_block_device_mapping_ephemeral3 => /dev/sde
ec2_block_device_mapping_root => /dev/sda1
ec2_hostname => ip-xxx.xxx.xxx.xxx.ap-southeast-1.compute.internal
ec2_instance_id => i-e999fff4
ec2_instance_type => m1.medium
ec2_kernel_id => aki-a9999af4
ec2_local_hostname => ip-xxx.xxx.xxx.xxx.ap-southeast-1.compute.internal
ec2_local_ipv4 => xxx.xxx.xxx.xxx
ec2_placement_availability_zone => ap-southeast-1b
ec2_profile => default-paravirtual
ec2_public_hostname => ec2-xxx.xxx.xxx.xxx.ap-southeast-1.compute.amazonaws.com
ec2_public_ipv4 => xxx.xxx.xxx.xxx
Share:
12,188

Related videos on Youtube

Rob Bednark
Author by

Rob Bednark

Software Engineer resume LinkedIn Facebook Twitter github:robbednark Google+ Quora PortlandUpside.com email/Hangouts: rbednark (gmail.com) Skype ID: rbednark

Updated on September 18, 2022

Comments

  • Rob Bednark
    Rob Bednark over 1 year

    I want some way of identifying which instance that I'm on when I ssh into a Linux ec2 instance. I would like to be able to uniquely identify the instance from the command-line and correlate this to http://console.aws.amason.com

    Any of the following would be helpful:

      AMI
      Private DNS
      Public DNS
      Tags
    

    I'm using Ubuntu and Red Hat instances.

  • ceejayoz
    ceejayoz almost 12 years
    A note - facter gets this from the metadata service @CraigSanders mentions.